我的需求很简单哈:
需要在小程序上实现3d的效果 uni的项目 运行到微信小程序 就行了,不考虑其他平台的适配(uni插件库基本为0,试过oasis不行)
https://blog.csdn.net/hzqzzz/article/details/126428029
但是他的代码引入,对于小白的我来说,有点深奥,没看懂该引入哪些东西。
于是自己研究了一下
找到了那个库的大佬写的另外一个库,从里面提取了一部分


这位大佬的three.js库,里面很全
https://github.com/deepkolos/platformize
按照它的要求弄完可以看到很多东西(如果需求是微信原生开发,可以直接使用他里面的例子)
拷贝完之后 找到这个文件 platformize/examples/three-wechat/miniprogram/chunks/three.js
复制到自己的项目内 (对,就这一个文件就行了)
创建个页面index.vue,然后把下面代码直接全部复制就行了
运行到微信小程序,就完成了
没有其他需求且感觉获取文件麻烦的可直接看四、补充进行获取文件
<template>
<view class="" style=" width: 100vw;height: 100vh;">
<canvas style="width: 100vw; height: 100vh;" class="webgl" type="webgl" id="gl" @touchstart="onTX" @touchmove="onTX" @touchend="onTX"></canvas>
</view>
</template>
<script>
var three = require('../../chunks/three.js');
function _classCallCheck$8(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var Platform = function Platform() {
_classCallCheck$8(this, Platform);
};
function _classCallCheck$7(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var $Blob = function $Blob(parts) {
var options = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {
type: "image/jpeg"
};
_classCallCheck$7(this, $Blob);
this.parts = parts;
this.options = options;
// 安卓微信不支持image/jpg的解析, 需改为image/jpeg
options.type = options.type.replace("jpg", "jpeg");
};
/*
* base64-arraybuffer
* https://github.com/niklasvh/base64-arraybuffer
*
* Copyright (c) 2012 Niklas von Hertzen
* Licensed under the MIT license.
*/
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
// Use a lookup table to find the index.
var lookup = new Uint8Array(256);
for (var i = 0; i < chars.length; i++) {
lookup[chars.charCodeAt(i)] = i;
}
// 快一点
function encode(arrayBuffer) {
var base64 = "";
var bytes = new Uint8Array(arrayBuffer);
var byteLength = bytes.byteLength;
var byteRemainder = byteLength % 3;
var mainLength = byteLength - byteRemainder;
var a, b, c, d;
var chunk;
// Main loop deals with bytes in chunks of 3
for (var i = 0; i < mainLength; i = i + 3) {
// Combine the three bytes into a single integer
chunk = bytes[i] << 16 | bytes[i + 1] << 8 | bytes[i + 2];
// Use bitmasks to extract 6-bit segments from the triplet
a = (chunk & 16515072) >> 18; // 16515072 = (2^6 - 1) << 18
b = (chunk & 258048) >> 12; // 258048 = (2^6 - 1) << 12
c = (chunk & 4032) >> 6; // 4032 = (2^6 - 1) << 6
d = chunk & 63; // 63 = 2^6 - 1
// Convert the raw binary segments to the appropriate ASCII encoding
base64 += chars[a] + chars[b] + chars[c] + chars[d];
}
// Deal with the remaining bytes and padding
if (byteRemainder == 1) {
chunk = bytes[mainLength];
a = (chunk & 252) >> 2; // 252 = (2^6 - 1) << 2
// Set the 4 least significant bits to zero
b = (chunk & 3) << 4; // 3 = 2^2 - 1
base64 += chars[a] + chars[b] + "==";
} else if (byteRemainder == 2) {
chunk = bytes[mainLength] << 8 | bytes[mainLength + 1];
a = (chunk & 64512) >> 10; // 64512 = (2^6 - 1) << 10
b = (chunk & 1008) >> 4; // 1008 = (2^6 - 1) << 4
// Set the 2 least significant bits to zero
c = (chunk & 15) << 2; // 15 = 2^4 - 1
base64 += chars[a] + chars[b] + chars[c] + "=";
}
return base64;
}
function _classCallCheck$6(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _instanceof$2(left, right) {
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
return !!right[Symbol.hasInstance](left);
} else {
return left instanceof right;
}
}
var $URL = /*#__PURE__*/ function() {
function $URL() {
_classCallCheck$6(this, $URL);
}
var _proto = $URL.prototype;
_proto.createObjectURL = function createObjectURL(obj) {
if (_instanceof$2(obj, $Blob)) {
// 更好的方式,使用wx.fileSystemManager写入临时文件来获取url,但是需要手动管理临时文件
var base64 = encode(obj.parts[0]);
var url = "data:".concat(obj.options.type, ";base64,").concat(base64);
return url;
}
return "";
};
_proto.revokeObjectURL = function revokeObjectURL() {};
return $URL;
}();
/**
* A lookup table for atob(), which converts an ASCII character to the
* corresponding six-bit number.
*/
var keystr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function atobLookup(chr) {
var index = keystr.indexOf(chr);
// Throw exception if character is not in the lookup string; should not be hit in tests
return index < 0 ? undefined : index;
}
/**
* Implementation of atob() according to the HTML and Infra specs, except that
* instead of throwing INVALID_CHARACTER_ERR we return null.
*/
function atob(data) {
// Web IDL requires DOMStrings to just be converted using ECMAScript
// ToString, which in our case amounts to using a template literal.
data = "".concat(data);
// "Remove all ASCII whitespace from data."
data = data.replace(/[ \t\n\f\r]/g, "");
// "If data's length divides by 4 leaving no remainder, then: if data ends
// with one or two U+003D (=) code points, then remove them from data."
if (data.length % 4 === 0) {
data = data.replace(/==?$/, "");
}
// "If data's length divides by 4 leaving a remainder of 1, then return
// failure."
//
// "If data contains a code point that is not one of
//
// U+002B (+)
// U+002F (/)
// ASCII alphanumeric
//
// then return failure."
if (data.length % 4 === 1 || /[^+/0-9A-Za-z]/.test(data)) {
return "";
}
// "Let output be an empty byte sequence."
var output = "";
// "Let buffer be an empty buffer that can have bits appended to it."
//
// We append bits via left-shift and or. accumulatedBits is used to track
// when we've gotten to 24 bits.
var buffer = 0;
var accumulatedBits = 0;
// "Let position be a position variable for data, initially pointing at the
// start of data."
//
// "While position does not point past the end of data:"
for (var i = 0; i < data.length; i++) {
// "Find the code point pointed to by position in the second column of
// Table 1: The Base 64 Alphabet of RFC 4648. Let n be the number given in
// the first cell of the same row.
//
// "Append to buffer the six bits corresponding to n, most significant bit
// first."
//
// atobLookup() implements the table from RFC 4648.
buffer <<= 6;
// @ts-ignore
buffer |= atobLookup(data[i]);
accumulatedBits += 6;
// "If buffer has accumulated 24 bits, interpret them as three 8-bit
// big-endian numbers. Append three bytes with values equal to those
// numbers to output, in the same order, and then empty buffer."
if (accumulatedBits === 24) {
output += String.fromCharCode((buffer & 0xff0000) >> 16);
output += String.fromCharCode((buffer & 0xff00) >> 8);
output += String.fromCharCode(buffer & 0xff);
buffer = accumulatedBits = 0;
}
// "Advance position by 1."
}
// "If buffer is not empty, it contains either 12 or 18 bits. If it contains
// 12 bits, then discard the last four and interpret the remaining eight as
// an 8-bit big-endian number. If it contains 18 bits, then discard the last
// two and interpret the remaining 16 as two 8-bit big-endian numbers. Append
// the one or two bytes with values equal to those one or two numbers to
// output, in the same order."
if (accumulatedBits === 12) {
buffer >>= 4;
output += String.fromCharCode(buffer);
} else if (accumulatedBits === 18) {
buffer >>= 2;
output += String.fromCharCode((buffer & 0xff00) >> 8);
output += String.fromCharCode(buffer & 0xff);
}
// "Return output."
return output;
}
function _classCallCheck$5(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
var _events = new WeakMap();
var Touch = function Touch(touch) {
_classCallCheck$5(this, Touch);
// CanvasTouch{identifier, x, y}
// Touch{identifier, pageX, pageY, clientX, clientY, force}
this.identifier = touch.identifier;
this.force = touch.force === undefined ? 1 : touch.force;
this.pageX = touch.pageX === undefined ? touch.x : touch.pageX;
this.pageY = touch.pageY === undefined ? touch.y : touch.pageY;
this.clientX = touch.clientX === undefined ? touch.x : touch.clientX;
this.clientY = touch.clientY === undefined ? touch.y : touch.clientY;
this.screenX = this.pageX;
this.screenY = this.pageY;
};
var $EventTarget = /*#__PURE__*/ function() {
function $EventTarget() {
_classCallCheck$5(this, $EventTarget);
_events.set(this, {});
}
var _proto = $EventTarget.prototype;
_proto.addEventListener = function addEventListener(type, listener) {
var events = _events.get(this);
if (!events) {
events = {};
_events.set(this, events);
}
if (!events[type]) {
events[type] = [];
}
events[type].push(listener);
// if (options.capture) {
// // console.warn('EventTarget.addEventListener: options.capture is not implemented.')
// }
// if (options.once) {
// // console.warn('EventTarget.addEventListener: options.once is not implemented.')
// }
// if (options.passive) {
// // console.warn('EventTarget.addEventListener: options.passive is not implemented.')
// }
};
_proto.removeEventListener = function removeEventListener(type, listener) {
var events = _events.get(this);
if (events) {
var listeners = events[type];
if (listeners && listeners.length > 0) {
for (var i = listeners.length; i--; i > 0) {
if (listeners[i] === listener) {
listeners.splice(i, 1);
break;
}
}
}
}
};
_proto.dispatchEvent = function dispatchEvent() {
var event = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {
type: ""
};
if (typeof event.preventDefault !== "function") {
event.preventDefault = function() {};
}
if (typeof event.stopPropagation !== "function") {
event.stopPropagation = function() {};
}
var events = _events.get(this);
if (events) {
var listeners = events[event.type];
if (listeners) {
for (var i = 0; i < listeners.length; i++) {
listeners[i](event);
}
}
}
// @ts-ignore
if (typeof this["on".concat(event.type)] === "function") {
// @ts-ignore
this["on".concat(event.type)].call(this, event);
}
};
_proto.releasePointerCapture = function releasePointerCapture() {};
_proto.setPointerCapture = function setPointerCapture() {};
return $EventTarget;
}();
function copyProperties(target, source) {
var _iteratorNormalCompletion = true,
_didIteratorError = false,
_iteratorError = undefined;
try {
for (var _iterator = Object.getOwnPropertyNames(source)[Symbol.iterator](), _step; !(
_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var key = _step.value;
if (key !== "constructor" && key !== "prototype" && key !== "name") {
var desc = Object.getOwnPropertyDescriptor(source, key);
desc && Object.defineProperty(target, key, desc);
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return != null) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
function createImage(canvas) {
var img = canvas.createImage();
img.addEventListener = function(name, cb) {
return img["on".concat(name)] = cb.bind(img);
};
img.removeEventListener = function(name) {
return img["on".concat(name)] = null;
};
return img;
}
/**
* Module dependencies.
*/
/**
* Expose `parse`.
*/
/**
* Parse the given string of `xml`.
*
* @param {String} xml
* @return {Object}
* @api public
*/
function parse(xml) {
var document =
/**
* XML document.
*/
function document() {
return {
declaration: declaration(),
root: tag(),
isXML: true
};
};
var declaration =
/**
* Declaration.
*/
function declaration() {
var m = match(/^<\?xml\s*/);
if (!m) return;
// tag
var node = {
attributes: {},
children: []
};
// attributes
while (!(eos() || is("?>"))) {
var attr = attribute();
if (!attr) return node;
node.attributes[attr.name] = attr.value;
}
match(/\?>\s*/);
// remove DOCTYPE
// <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
// "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
match(/<!DOCTYPE[^>]*>\s/);
return node;
};
var content =
/**
* Text content.
*/
function content() {
var m = match(/^([^<]*)/);
if (m) return m[1];
return "";
};
var attribute =
/**
* Attribute.
*/
function attribute() {
var m = match(/([\w:-]+)\s*=\s*("[^"]*"|'[^']*'|\w+)\s*/);
if (!m) return;
return {
name: m[1],
value: strip(m[2])
};
};
var strip =
/**
* Strip quotes from `val`.
*/
function strip(val) {
return val.replace(/^['"]|['"]$/g, "");
};
var match =
/**
* Match `re` and advance the string.
*/
function match(re) {
var m = xml.match(re);
if (!m) return;
xml = xml.slice(m[0].length);
return m;
};
var eos =
/**
* End-of-source.
*/
function eos() {
return xml.length == 0;
};
var is =
/**
* Check for `prefix`.
*/
function is(prefix) {
return xml.indexOf(prefix) == 0;
};
xml = xml.trim();
// strip comments
xml = xml.replace(/<!--[\s\S]*?-->/g, "");
return document();
/**
* Tag.
*/
function tag() {
var m = match(/^<([\w-:.]+)\s*/);
if (!m) return;
// name
var node = {
name: m[1],
attributes: {},
children: []
};
// attributes
while (!(eos() || is(">") || is("?>") || is("/>"))) {
var attr = attribute();
if (!attr) return node;
node.attributes[attr.name] = attr.value;
}
// self closing tag
if (match(/^\s*\/>\s*/)) {
return node;
}
match(/\??>\s*/);
// @ts-ignore content
node.content = content();
// children
var child;
while (child = tag()) {
node.children.push(child);
}
// closing
match(/^<\/[\w-:.]+>\s*/);
return node;
}
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _classCallCheck$4(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _iterableToArrayLimit(arr, i) {
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
if (_i == null) return;
var _arr = [];
var _n = true;
var _d = false;
var _s, _e;
try {
for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) {
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally {
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally {
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableRest() {
throw new TypeError(
"Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."
);
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) ||
_nonIterableRest();
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(n);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function walkTree(node, processer) {
processer(node);
node.children.forEach(function(i) {
return walkTree(i, processer);
});
}
var $DOMParser = /*#__PURE__*/ function() {
function $DOMParser() {
_classCallCheck$4(this, $DOMParser);
}
var _proto = $DOMParser.prototype;
_proto.parseFromString = function parseFromString(str) {
var xml = parse(str);
var nodeBase = {
// @ts-ignore
hasAttribute: function hasAttribute(key) {
// @ts-ignore
return this.attributes[key] !== undefined;
},
// @ts-ignore
getAttribute: function getAttribute(key) {
// @ts-ignore
return this.attributes[key];
},
getElementsByTagName: function getElementsByTagName(tag) {
// 看了dae的文件结构,xml的节点不算庞大,所以还能接受
var result = [];
// @ts-ignore
this.childNodes.forEach(function(i) {
return walkTree(i, function(node) {
return tag === node.name && result.push(node);
});
});
return result;
}
};
// patch xml
xml.root && walkTree(xml.root, function(node) {
node.nodeType = 1;
node.nodeName = node.name;
node.style = new Proxy((node.attributes.style || "").split(";").reduce(function(acc,
curr) {
if (curr) {
var ref = _slicedToArray(curr.split(":"), 2),
key = ref[0],
value = ref[1];
acc[key.trim()] = value.trim();
}
return acc;
}, {}), {
get: function get(target, key) {
return target[key] || "";
}
});
node.textContent = node.content;
node.childNodes = node.children;
// @ts-ignore
node.__proto__ = nodeBase;
});
var out = {
documentElement: xml.root,
childNodes: [
xml.root
]
};
// @ts-ignore
out.__proto__ = nodeBase;
return out;
};
return $DOMParser;
}();
function _classCallCheck$3(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _instanceof$1(left, right) {
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
return !!right[Symbol.hasInstance](left);
} else {
return left instanceof right;
}
}
var $TextDecoder = /*#__PURE__*/ function() {
function $TextDecoder() {
_classCallCheck$3(this, $TextDecoder);
}
var _proto = $TextDecoder.prototype;
/**
* 不支持 UTF-8 code points 大于 1 字节
* @see https://stackoverflow.com/questions/17191945/conversion-between-utf-8-arraybuffer-and-string
* @param {Uint8Array|ArrayBuffer} uint8Array
*/
_proto.decode = function decode(input) {
var uint8Array = _instanceof$1(input, ArrayBuffer) ? new Uint8Array(input) : input;
// from threejs LoaderUtils.js
var s = "";
// Implicitly assumes little-endian.
for (var i = 0, il = uint8Array.length; i < il; i++) {
s += String.fromCharCode(uint8Array[i]);
}
try {
// merges multi-byte utf-8 characters.
return decodeURIComponent(escape(s));
} catch (e) {
// see #16358
return s;
}
// return String.fromCharCode.apply(null, uint8Array);
};
return $TextDecoder;
}();
var $performance = {
now: function now() {
return Date.now();
}
};
// @ts-nocheck
function _assertThisInitialized$2(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _classCallCheck$2(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Date.prototype.toString.call(Reflect.construct(Date, [], function() {}));
return true;
} catch (e) {
return false;
}
}
function _construct(Parent, args, Class) {
if (isNativeReflectConstruct()) {
_construct = Reflect.construct;
} else {
_construct = function _construct(Parent, args, Class) {
var a = [
null
];
a.push.apply(a, args);
var Constructor = Function.bind.apply(Parent, a);
var instance = new Constructor();
if (Class) _setPrototypeOf$2(instance, Class.prototype);
return instance;
};
}
return _construct.apply(null, arguments);
}
function _getPrototypeOf$2(o) {
_getPrototypeOf$2 = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf$2(o);
}
function _inherits$2(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf$2(subClass, superClass);
}
function _instanceof(left, right) {
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
return !!right[Symbol.hasInstance](left);
} else {
return left instanceof right;
}
}
function _isNativeFunction(fn) {
return Function.toString.call(fn).indexOf("[native code]") !== -1;
}
function _possibleConstructorReturn$2(self, call) {
if (call && (_typeof$2(call) === "object" || typeof call === "function")) {
return call;
}
return _assertThisInitialized$2(self);
}
function _setPrototypeOf$2(o, p) {
_setPrototypeOf$2 = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf$2(o, p);
}
var _typeof$2 = function(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
};
function _wrapNativeSuper(Class) {
var _cache = typeof Map === "function" ? new Map() : undefined;
_wrapNativeSuper = function _wrapNativeSuper(Class) {
if (Class === null || !_isNativeFunction(Class)) return Class;
if (typeof Class !== "function") {
throw new TypeError("Super expression must either be null or a function");
}
if (typeof _cache !== "undefined") {
if (_cache.has(Class)) return _cache.get(Class);
_cache.set(Class, Wrapper);
}
function Wrapper() {
return _construct(Class, arguments, _getPrototypeOf$2(this).constructor);
}
Wrapper.prototype = Object.create(Class.prototype, {
constructor: {
value: Wrapper,
enumerable: false,
writable: true,
configurable: true
}
});
return _setPrototypeOf$2(Wrapper, Class);
};
return _wrapNativeSuper(Class);
}
function _isNativeReflectConstruct$2() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
return true;
} catch (e) {
return false;
}
}
function _createSuper$2(Derived) {
var hasNativeReflectConstruct = _isNativeReflectConstruct$2();
return function _createSuperInternal() {
var Super = _getPrototypeOf$2(Derived),
result;
if (hasNativeReflectConstruct) {
var NewTarget = _getPrototypeOf$2(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn$2(this, result);
};
}
var _requestHeader = new WeakMap();
var _responseHeader = new WeakMap();
var _requestTask = new WeakMap();
function _triggerEvent(type) {
var event = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
event.target = event.target || this;
if (typeof this["on".concat(type)] === "function") {
this["on".concat(type)].call(this, event);
}
}
function _changeReadyState(readyState) {
var event = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
this.readyState = readyState;
event.readyState = readyState;
_triggerEvent.call(this, "readystatechange", event);
}
function _isRelativePath(url) {
return !/^(http|https|ftp|wxfile):\/\/.*/i.test(url);
}
var $XMLHttpRequest = /*#__PURE__*/ function(EventTarget) {
_inherits$2($XMLHttpRequest, EventTarget);
var _super = _createSuper$2($XMLHttpRequest);
function $XMLHttpRequest() {
_classCallCheck$2(this, $XMLHttpRequest);
var _this;
_this = _super.call(this);
_this.runtime = wx.getSystemInfoSync().platform;
/*
* TODO 这一批事件应该是在 XMLHttpRequestEventTarget.prototype 上面的
*/
_this.onabort = null;
_this.onerror = null;
_this.onload = null;
_this.onloadstart = null;
_this.onprogress = null;
_this.ontimeout = null;
_this.onloadend = null;
_this.onreadystatechange = null;
_this.readyState = 0;
_this.response = null;
_this.responseText = null;
_this.responseType = "text";
_this.dataType = "string";
_this.responseXML = null;
_this.status = 0;
_this.statusText = "";
_this.upload = {};
_this.withCredentials = false;
_requestHeader.set(_assertThisInitialized$2(_this), {
"content-type": "application/x-www-form-urlencoded"
});
_responseHeader.set(_assertThisInitialized$2(_this), {});
return _this;
}
var _proto = $XMLHttpRequest.prototype;
_proto.abort = function abort() {
var myRequestTask = _requestTask.get(this);
if (myRequestTask) {
myRequestTask.abort();
}
};
_proto.getAllResponseHeaders = function getAllResponseHeaders() {
var responseHeader = _responseHeader.get(this);
return Object.keys(responseHeader).map(function(header) {
return "".concat(header, ": ").concat(responseHeader[header]);
}).join("\n");
};
_proto.getResponseHeader = function getResponseHeader(header) {
return _responseHeader.get(this)[header];
};
_proto.open = function open(method, url /* async, user, password 这几个参数在小程序内不支持*/ ) {
this._method = method;
this._url = url;
_changeReadyState.call(this, $XMLHttpRequest.OPENED);
};
_proto.overrideMimeType = function overrideMimeType() {};
_proto.send = function send() {
var data = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : "";
var _this = this;
if (this.readyState !== $XMLHttpRequest.OPENED) {
throw new Error(
"Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED.");
} else {
var url = this._url;
var header = _requestHeader.get(this);
var responseType = this.responseType;
var dataType = this.dataType;
var relative = _isRelativePath(url);
var encoding;
if (responseType === "arraybuffer");
else {
encoding = "utf8";
}
if (responseType === "json") {
dataType = "json";
responseType = "text";
}
delete this.response;
this.response = null;
var resolved = false;
var onSuccess = function(param) {
var data = param.data,
statusCode = param.statusCode,
header = param.header;
// console.log('onSuccess', url);
if (resolved) return;
resolved = true;
statusCode = statusCode === undefined ? 200 : statusCode;
if (typeof data !== "string" && !_instanceof(data, ArrayBuffer) && dataType !== "json") {
try {
data = JSON.stringify(data);
} catch (e) {}
}
_this.status = statusCode;
if (header) {
_responseHeader.set(_this, header);
}
_triggerEvent.call(_this, "loadstart");
_changeReadyState.call(_this, $XMLHttpRequest.HEADERS_RECEIVED);
_changeReadyState.call(_this, $XMLHttpRequest.LOADING);
_this.response = data;
if (_instanceof(data, ArrayBuffer)) {
Object.defineProperty(_this, "responseText", {
enumerable: true,
configurable: true,
get: function get() {
throw "InvalidStateError : responseType is " + this.responseType;
}
});
} else {
_this.responseText = data;
}
_changeReadyState.call(_this, $XMLHttpRequest.DONE);
_triggerEvent.call(_this, "load");
_triggerEvent.call(_this, "loadend");
};
var onFail = function(param) {
var errMsg = param.errMsg;
// TODO 规范错误
if (resolved) return;
resolved = true;
if (errMsg.indexOf("abort") !== -1) {
_triggerEvent.call(_this, "abort");
} else {
_triggerEvent.call(_this, "error", {
message: errMsg
});
}
_triggerEvent.call(_this, "loadend");
if (relative) {
// 用户即使没监听error事件, 也给出相应的警告
console.warn(errMsg);
}
};
if (relative) {
var fs = wx.getFileSystemManager();
var options = {
filePath: url,
success: onSuccess,
fail: onFail
};
if (encoding) {
options["encoding"] = encoding;
}
fs.readFile(options);
return;
}
// IOS在某些情况下不会触发onSuccess...
var usePatch = responseType === "arraybuffer" && this.runtime === "ios" && $XMLHttpRequest
.useFetchPatch;
wx.request({
data: data,
url: url,
method: this._method.toUpperCase(),
header: header,
dataType: dataType,
responseType: responseType,
enableCache: false,
success: onSuccess,
// success: usePatch ? undefined : onSuccess,
fail: onFail
});
if (usePatch) {
setTimeout(function() {
wx.request({
data: data,
url: url,
method: this._method,
header: header,
dataType: dataType,
responseType: responseType,
enableCache: true,
success: onSuccess,
fail: onFail
});
}, $XMLHttpRequest.fetchPatchDelay);
}
}
};
_proto.setRequestHeader = function setRequestHeader(header, value) {
var myHeader = _requestHeader.get(this);
myHeader[header] = value;
_requestHeader.set(this, myHeader);
};
_proto.addEventListener = function addEventListener(type, listener) {
var _this = this;
if (typeof listener !== "function") {
return;
}
this["on" + type] = function() {
var event = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
event.target = event.target || _this;
listener.call(_this, event);
};
};
_proto.removeEventListener = function removeEventListener(type, listener) {
if (this["on" + type] === listener) {
this["on" + type] = null;
}
};
return $XMLHttpRequest;
}(_wrapNativeSuper($EventTarget));
// TODO 没法模拟 HEADERS_RECEIVED 和 LOADING 两个状态
$XMLHttpRequest.UNSEND = 0;
$XMLHttpRequest.OPENED = 1;
$XMLHttpRequest.HEADERS_RECEIVED = 2;
$XMLHttpRequest.LOADING = 3;
$XMLHttpRequest.DONE = 4;
// 某些情况下IOS会不success不触发。。。
$XMLHttpRequest.useFetchPatch = false;
$XMLHttpRequest.fetchPatchDelay = 200;
/// <reference types="@types/wechat-miniprogram" />
/// <reference types="offscreencanvas" />
function _assertThisInitialized$1(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _classCallCheck$1(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _getPrototypeOf$1(o) {
_getPrototypeOf$1 = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf$1(o);
}
function _inherits$1(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf$1(subClass, superClass);
}
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
var ownKeys = Object.keys(source);
if (typeof Object.getOwnPropertySymbols === "function") {
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
}));
}
ownKeys.forEach(function(key) {
_defineProperty(target, key, source[key]);
});
}
return target;
}
function _possibleConstructorReturn$1(self, call) {
if (call && (_typeof$1(call) === "object" || typeof call === "function")) {
return call;
}
return _assertThisInitialized$1(self);
}
function _setPrototypeOf$1(o, p) {
_setPrototypeOf$1 = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf$1(o, p);
}
var _typeof$1 = function(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
};
function _isNativeReflectConstruct$1() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
return true;
} catch (e) {
return false;
}
}
function _createSuper$1(Derived) {
var hasNativeReflectConstruct = _isNativeReflectConstruct$1();
return function _createSuperInternal() {
var Super = _getPrototypeOf$1(Derived),
result;
if (hasNativeReflectConstruct) {
var NewTarget = _getPrototypeOf$1(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn$1(this, result);
};
}
function OffscreenCanvas() {
// @ts-ignore
return wx.createOffscreenCanvas();
}
var WechatPlatform$1 = /*#__PURE__*/ function(Platform) {
_inherits$1(WechatPlatform, Platform);
var _super = _createSuper$1(WechatPlatform);
function WechatPlatform(canvas, width, height) {
_classCallCheck$1(this, WechatPlatform);
var _this;
_this = _super.call(this);
_this.enabledDeviceMotion = false;
_this.canvasRect = {
width: 0,
height: 0,
top: 0,
left: 0,
right: 0,
bottom: 0
};
var systemInfo = wx.getSystemInfoSync();
var isAndroid = systemInfo.platform === "android";
// @ts-ignore
_this.canvas = canvas;
_this.canvasW = width === undefined ? canvas.width : width;
_this.canvasH = height === undefined ? canvas.height : height;
_this.canvasRect.width = _this.canvasW;
_this.canvasRect.height = _this.canvasH;
var document = {
createElementNS: function createElementNS(_, type) {
if (type === "canvas") return canvas;
if (type === "img") return createImage(canvas);
},
createElement: function createElement(type) {
if (type === "canvas") return canvas;
if (type === "img") return createImage(canvas);
},
body: {}
};
var img = createImage(canvas);
var Image = function() {
return createImage(canvas);
};
var URL = new $URL();
var window = {
innerWidth: systemInfo.windowWidth,
innerHeight: systemInfo.windowHeight,
devicePixelRatio: systemInfo.pixelRatio,
AudioContext: function AudioContext() {},
requestAnimationFrame: _this.canvas.requestAnimationFrame,
cancelAnimationFrame: _this.canvas.cancelAnimationFrame,
DeviceOrientationEvent: {
requestPermission: function requestPermission() {
return Promise.resolve("granted");
}
},
URL: URL,
Image: Image,
DOMParser: $DOMParser,
TextDecoder: $TextDecoder,
Blob: $Blob,
performance: $performance
};
[
canvas,
document,
window,
document.body
].forEach(function(i) {
// @ts-ignore
var old = i.__proto__;
// @ts-ignore
i.__proto__ = {};
// @ts-ignore
i.__proto__.__proto__ = old;
// @ts-ignore
copyProperties(i.__proto__, $EventTarget.prototype);
});
_this.polyfill = {
window: window,
document: document,
// @ts-expect-error
Blob: $Blob,
// @ts-expect-error
DOMParser: $DOMParser,
// @ts-expect-error
TextDecoder: $TextDecoder,
// @ts-expect-error
XMLHttpRequest: $XMLHttpRequest,
// @ts-expect-error
OffscreenCanvas: OffscreenCanvas,
// @ts-expect-error
URL: URL,
Image: Image,
HTMLImageElement: img.constructor,
atob: atob,
global: window,
createImageBitmap: undefined,
cancelAnimationFrame: window.cancelAnimationFrame,
requestAnimationFrame: window.requestAnimationFrame,
performance: window.performance
};
_this.patchCanvas();
_this.onDeviceMotionChange = function(e) {
e.type = "deviceorientation";
if (isAndroid) {
e.alpha *= -1;
e.beta *= -1;
e.gamma *= -1;
}
window.dispatchEvent(e);
};
return _this;
}
var _proto = WechatPlatform.prototype;
_proto.patchCanvas = function patchCanvas() {
var _this = this;
var ref = this,
canvasH = ref.canvasH,
canvasW = ref.canvasW,
canvas = ref.canvas;
Object.defineProperty(this.canvas, "style", {
get: function get() {
return {
width: this.width + "px",
height: this.height + "px"
};
}
});
Object.defineProperty(this.canvas, "clientHeight", {
get: function get() {
return canvasH || this.height;
}
});
Object.defineProperty(this.canvas, "clientWidth", {
get: function get() {
return canvasW || this.width;
}
});
// @ts-ignore
canvas.ownerDocument = this.document;
// @ts-ignore
canvas.getBoundingClientRect = function() {
return _this.canvasRect;
};
// @ts-ignore
canvas._getContext = this.canvas.getContext;
canvas.getContext = function getContext() {
var _canvas;
if (arguments[0] !== "webgl") return null;
// @ts-ignore
return (_canvas = canvas)._getContext.apply(_canvas, arguments);
};
};
// 某些情况下IOS会不success不触发。。。
_proto.patchXHR = function patchXHR() {
$XMLHttpRequest.useFetchPatch = true;
return this;
};
_proto.enableDeviceOrientation = function enableDeviceOrientation(interval) {
var _this = this;
return new Promise(function(resolve, reject) {
wx.onDeviceMotionChange(_this.onDeviceMotionChange);
wx.startDeviceMotionListening({
interval: interval,
success: function(e) {
resolve(e);
_this.enabledDeviceMotion = true;
},
fail: reject
});
});
};
_proto.disableDeviceOrientation = function disableDeviceOrientation() {
var _this = this;
return new Promise(function(resolve, reject) {
wx.offDeviceMotionChange(_this.onDeviceMotionChange);
_this.enabledDeviceMotion && wx.stopDeviceMotionListening({
success: function() {
resolve(true);
_this.enabledDeviceMotion = false;
},
fail: reject
});
});
};
_proto.dispatchTouchEvent = function dispatchTouchEvent() {
var e = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {
touches: [],
changedTouches: [],
timeStamp: 0,
type: ""
};
var target = _objectSpread({}, this);
var changedTouches = e.changedTouches.map(function(touch) {
return new Touch(touch);
});
var event = {
changedTouches: changedTouches,
touches: e.touches.map(function(touch) {
return new Touch(touch);
}),
targetTouches: Array.prototype.slice.call(e.touches.map(function(touch) {
return new Touch(touch);
})),
timeStamp: e.timeStamp,
target: target,
currentTarget: target,
type: e.type,
cancelBubble: false,
cancelable: false
};
this.canvas.dispatchEvent(event);
if (changedTouches.length) {
var touch = changedTouches[0];
var pointerEvent = {
pageX: touch.pageX,
pageY: touch.pageY,
offsetX: touch.pageX,
offsetY: touch.pageY,
pointerId: touch.identifier,
type: {
touchstart: "pointerdown",
touchmove: "pointermove",
touchend: "pointerup"
} [e.type] || "",
pointerType: "touch"
};
this.canvas.dispatchEvent(pointerEvent);
}
};
_proto.dispose = function dispose() {
this.disableDeviceOrientation();
// 缓解ios内存泄漏, 前后进出页面多几次,降低pixelRatio也可行
this.canvas.width = 0;
this.canvas.height = 0;
// @ts-ignore
if (this.canvas) this.canvas.ownerDocument = null;
// @ts-ignore
this.onDeviceMotionChange = null;
// @ts-ignore
this.canvas = null;
};
return WechatPlatform;
}(Platform);
/// <reference types="wechat-miniprogram" />
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _possibleConstructorReturn(self, call) {
if (call && (_typeof(call) === "object" || typeof call === "function")) {
return call;
}
return _assertThisInitialized(self);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
var _typeof = function(obj) {
"@swc/helpers - typeof";
return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj;
};
function _isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function() {}));
return true;
} catch (e) {
return false;
}
}
function _createSuper(Derived) {
var hasNativeReflectConstruct = _isNativeReflectConstruct();
return function _createSuperInternal() {
var Super = _getPrototypeOf(Derived),
result;
if (hasNativeReflectConstruct) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
var WechatPlatform = /*#__PURE__*/ function(WechatPlatformBase) {
_inherits(WechatPlatform, WechatPlatformBase);
var _super = _createSuper(WechatPlatform);
function WechatPlatform(canvas, width, height) {
_classCallCheck(this, WechatPlatform);
var _this;
_this = _super.call(this, canvas, width, height);
_this.polyfill.$defaultWebGLExtensions = {};
return _this;
}
return WechatPlatform;
}(WechatPlatform$1);
class GLTFLoader extends three.Loader {
constructor(manager) {
super(manager);
this.dracoLoader = null;
this.ktx2Loader = null;
this.meshoptDecoder = null;
this.pluginCallbacks = [];
this.register(function(parser) {
return new GLTFMaterialsClearcoatExtension(parser);
});
this.register(function(parser) {
return new GLTFTextureBasisUExtension(parser);
});
this.register(function(parser) {
return new GLTFTextureWebPExtension(parser);
});
this.register(function(parser) {
return new GLTFMaterialsTransmissionExtension(parser);
});
this.register(function(parser) {
return new GLTFMaterialsVolumeExtension(parser);
});
this.register(function(parser) {
return new GLTFMaterialsIorExtension(parser);
});
this.register(function(parser) {
return new GLTFMaterialsSpecularExtension(parser);
});
this.register(function(parser) {
return new GLTFLightsExtension(parser);
});
this.register(function(parser) {
return new GLTFMeshoptCompression(parser);
});
}
load(url, onLoad, onProgress, onError) {
const scope = this;
let resourcePath;
if (this.resourcePath !== '') {
resourcePath = this.resourcePath;
} else if (this.path !== '') {
resourcePath = this.path;
} else {
resourcePath = three.LoaderUtils.extractUrlBase(url);
}
// Tells the LoadingManager to track an extra item, which resolves after
// the model is fully loaded. This means the count of items loaded will
// be incorrect, but ensures manager.onLoad() does not fire early.
this.manager.itemStart(url);
const _onError = function(e) {
if (onError) {
onError(e);
} else {
console.error(e);
}
scope.manager.itemError(url);
scope.manager.itemEnd(url);
};
const loader = new three.FileLoader(this.manager);
loader.setPath(this.path);
loader.setResponseType('arraybuffer');
loader.setRequestHeader(this.requestHeader);
loader.setWithCredentials(this.withCredentials);
loader.load(url, function(data) {
try {
scope.parse(data, resourcePath, function(gltf) {
onLoad(gltf);
scope.manager.itemEnd(url);
}, _onError);
} catch (e) {
_onError(e);
}
}, onProgress, _onError);
}
setDRACOLoader(dracoLoader) {
this.dracoLoader = dracoLoader;
return this;
}
setDDSLoader() {
throw new Error(
'THREE.GLTFLoader: "MSFT_texture_dds" no longer supported. Please update to "KHR_texture_basisu".'
);
}
setKTX2Loader(ktx2Loader) {
this.ktx2Loader = ktx2Loader;
return this;
}
setMeshoptDecoder(meshoptDecoder) {
this.meshoptDecoder = meshoptDecoder;
return this;
}
register(callback) {
if (this.pluginCallbacks.indexOf(callback) === -1) {
this.pluginCallbacks.push(callback);
}
return this;
}
unregister(callback) {
if (this.pluginCallbacks.indexOf(callback) !== -1) {
this.pluginCallbacks.splice(this.pluginCallbacks.indexOf(callback), 1);
}
return this;
}
parse(data, path, onLoad, onError) {
let content;
const extensions = {};
const plugins = {};
if (typeof data === 'string') {
content = data;
} else {
const magic = three.LoaderUtils.decodeText(new Uint8Array(data, 0, 4));
if (magic === BINARY_EXTENSION_HEADER_MAGIC) {
try {
extensions[EXTENSIONS.KHR_BINARY_GLTF] = new GLTFBinaryExtension(data);
} catch (error) {
if (onError) onError(error);
return;
}
content = extensions[EXTENSIONS.KHR_BINARY_GLTF].content;
} else {
content = three.LoaderUtils.decodeText(new Uint8Array(data));
}
}
const json = JSON.parse(content);
if (json.asset === undefined || json.asset.version[0] < 2) {
if (onError) onError(new Error(
'THREE.GLTFLoader: Unsupported asset. glTF versions >=2.0 are supported.'));
return;
}
const parser = new GLTFParser(json, {
path: path || this.resourcePath || '',
crossOrigin: this.crossOrigin,
requestHeader: this.requestHeader,
manager: this.manager,
ktx2Loader: this.ktx2Loader,
meshoptDecoder: this.meshoptDecoder
});
parser.fileLoader.setRequestHeader(this.requestHeader);
for (let i = 0; i < this.pluginCallbacks.length; i++) {
const plugin = this.pluginCallbacks[i](parser);
plugins[plugin.name] = plugin;
// Workaround to avoid determining as unknown extension
// in addUnknownExtensionsToUserData().
// Remove this workaround if we move all the existing
// extension handlers to plugin system
extensions[plugin.name] = true;
}
if (json.extensionsUsed) {
for (let i = 0; i < json.extensionsUsed.length; ++i) {
const extensionName = json.extensionsUsed[i];
const extensionsRequired = json.extensionsRequired || [];
switch (extensionName) {
case EXTENSIONS.KHR_MATERIALS_UNLIT:
extensions[extensionName] = new GLTFMaterialsUnlitExtension();
break;
case EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS:
extensions[extensionName] = new GLTFMaterialsPbrSpecularGlossinessExtension();
break;
case EXTENSIONS.KHR_DRACO_MESH_COMPRESSION:
extensions[extensionName] = new GLTFDracoMeshCompressionExtension(json, this
.dracoLoader);
break;
case EXTENSIONS.KHR_TEXTURE_TRANSFORM:
extensions[extensionName] = new GLTFTextureTransformExtension();
break;
case EXTENSIONS.KHR_MESH_QUANTIZATION:
extensions[extensionName] = new GLTFMeshQuantizationExtension();
break;
default:
if (extensionsRequired.indexOf(extensionName) >= 0 && plugins[extensionName] ===
undefined) {
console.warn('THREE.GLTFLoader: Unknown extension "' + extensionName + '".');
}
}
}
}
parser.setExtensions(extensions);
parser.setPlugins(plugins);
parser.parse(onLoad, onError);
}
}
/* GLTFREGISTRY */
function GLTFRegistry() {
let objects = {};
return {
get: function(key) {
return objects[key];
},
add: function(key, object) {
objects[key] = object;
},
remove: function(key) {
delete objects[key];
},
removeAll: function() {
objects = {};
}
};
}
/*********************************/
/********** EXTENSIONS ***********/
/*********************************/
const EXTENSIONS = {
KHR_BINARY_GLTF: 'KHR_binary_glTF',
KHR_DRACO_MESH_COMPRESSION: 'KHR_draco_mesh_compression',
KHR_LIGHTS_PUNCTUAL: 'KHR_lights_punctual',
KHR_MATERIALS_CLEARCOAT: 'KHR_materials_clearcoat',
KHR_MATERIALS_IOR: 'KHR_materials_ior',
KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS: 'KHR_materials_pbrSpecularGlossiness',
KHR_MATERIALS_SPECULAR: 'KHR_materials_specular',
KHR_MATERIALS_TRANSMISSION: 'KHR_materials_transmission',
KHR_MATERIALS_UNLIT: 'KHR_materials_unlit',
KHR_MATERIALS_VOLUME: 'KHR_materials_volume',
KHR_TEXTURE_BASISU: 'KHR_texture_basisu',
KHR_TEXTURE_TRANSFORM: 'KHR_texture_transform',
KHR_MESH_QUANTIZATION: 'KHR_mesh_quantization',
EXT_TEXTURE_WEBP: 'EXT_texture_webp',
EXT_MESHOPT_COMPRESSION: 'EXT_meshopt_compression'
};
/**
* Punctual Lights Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual
*/
class GLTFLightsExtension {
constructor(parser) {
this.parser = parser;
this.name = EXTENSIONS.KHR_LIGHTS_PUNCTUAL;
// Object3D instance caches
this.cache = {
refs: {},
uses: {}
};
}
_markDefs() {
const parser = this.parser;
const nodeDefs = this.parser.json.nodes || [];
for (let nodeIndex = 0, nodeLength = nodeDefs.length; nodeIndex < nodeLength; nodeIndex++) {
const nodeDef = nodeDefs[nodeIndex];
if (nodeDef.extensions &&
nodeDef.extensions[this.name] &&
nodeDef.extensions[this.name].light !== undefined) {
parser._addNodeRef(this.cache, nodeDef.extensions[this.name].light);
}
}
}
_loadLight(lightIndex) {
const parser = this.parser;
const cacheKey = 'light:' + lightIndex;
let dependency = parser.cache.get(cacheKey);
if (dependency) return dependency;
const json = parser.json;
const extensions = (json.extensions && json.extensions[this.name]) || {};
const lightDefs = extensions.lights || [];
const lightDef = lightDefs[lightIndex];
let lightNode;
const color = new three.Color(0xffffff);
if (lightDef.color !== undefined) color.fromArray(lightDef.color);
const range = lightDef.range !== undefined ? lightDef.range : 0;
switch (lightDef.type) {
case 'directional':
lightNode = new three.DirectionalLight(color);
lightNode.target.position.set(0, 0, -1);
lightNode.add(lightNode.target);
break;
case 'point':
lightNode = new three.PointLight(color);
lightNode.distance = range;
break;
case 'spot':
lightNode = new three.SpotLight(color);
lightNode.distance = range;
// Handle spotlight properties.
lightDef.spot = lightDef.spot || {};
lightDef.spot.innerConeAngle = lightDef.spot.innerConeAngle !== undefined ? lightDef.spot
.innerConeAngle : 0;
lightDef.spot.outerConeAngle = lightDef.spot.outerConeAngle !== undefined ? lightDef.spot
.outerConeAngle : Math.PI / 4.0;
lightNode.angle = lightDef.spot.outerConeAngle;
lightNode.penumbra = 1.0 - lightDef.spot.innerConeAngle / lightDef.spot.outerConeAngle;
lightNode.target.position.set(0, 0, -1);
lightNode.add(lightNode.target);
break;
default:
throw new Error('THREE.GLTFLoader: Unexpected light type: ' + lightDef.type);
}
// Some lights (e.g. spot) default to a position other than the origin. Reset the position
// here, because node-level parsing will only override position if explicitly specified.
lightNode.position.set(0, 0, 0);
lightNode.decay = 2;
if (lightDef.intensity !== undefined) lightNode.intensity = lightDef.intensity;
lightNode.name = parser.createUniqueName(lightDef.name || ('light_' + lightIndex));
dependency = Promise.resolve(lightNode);
parser.cache.add(cacheKey, dependency);
return dependency;
}
createNodeAttachment(nodeIndex) {
const self = this;
const parser = this.parser;
const json = parser.json;
const nodeDef = json.nodes[nodeIndex];
const lightDef = (nodeDef.extensions && nodeDef.extensions[this.name]) || {};
const lightIndex = lightDef.light;
if (lightIndex === undefined) return null;
return this._loadLight(lightIndex).then(function(light) {
return parser._getNodeRef(self.cache, lightIndex, light);
});
}
}
/**
* Unlit Materials Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit
*/
class GLTFMaterialsUnlitExtension {
constructor() {
this.name = EXTENSIONS.KHR_MATERIALS_UNLIT;
}
getMaterialType() {
return three.MeshBasicMaterial;
}
extendParams(materialParams, materialDef, parser) {
const pending = [];
materialParams.color = new three.Color(1.0, 1.0, 1.0);
materialParams.opacity = 1.0;
const metallicRoughness = materialDef.pbrMetallicRoughness;
if (metallicRoughness) {
if (Array.isArray(metallicRoughness.baseColorFactor)) {
const array = metallicRoughness.baseColorFactor;
materialParams.color.fromArray(array);
materialParams.opacity = array[3];
}
if (metallicRoughness.baseColorTexture !== undefined) {
pending.push(parser.assignTexture(materialParams, 'map', metallicRoughness.baseColorTexture));
}
}
return Promise.all(pending);
}
}
/**
* Clearcoat Materials Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_clearcoat
*/
class GLTFMaterialsClearcoatExtension {
constructor(parser) {
this.parser = parser;
this.name = EXTENSIONS.KHR_MATERIALS_CLEARCOAT;
}
getMaterialType(materialIndex) {
const parser = this.parser;
const materialDef = parser.json.materials[materialIndex];
if (!materialDef.extensions || !materialDef.extensions[this.name]) return null;
return three.MeshPhysicalMaterial;
}
extendMaterialParams(materialIndex, materialParams) {
const parser = this.parser;
const materialDef = parser.json.materials[materialIndex];
if (!materialDef.extensions || !materialDef.extensions[this.name]) {
return Promise.resolve();
}
const pending = [];
const extension = materialDef.extensions[this.name];
if (extension.clearcoatFactor !== undefined) {
materialParams.clearcoat = extension.clearcoatFactor;
}
if (extension.clearcoatTexture !== undefined) {
pending.push(parser.assignTexture(materialParams, 'clearcoatMap', extension.clearcoatTexture));
}
if (extension.clearcoatRoughnessFactor !== undefined) {
materialParams.clearcoatRoughness = extension.clearcoatRoughnessFactor;
}
if (extension.clearcoatRoughnessTexture !== undefined) {
pending.push(parser.assignTexture(materialParams, 'clearcoatRoughnessMap', extension
.clearcoatRoughnessTexture));
}
if (extension.clearcoatNormalTexture !== undefined) {
pending.push(parser.assignTexture(materialParams, 'clearcoatNormalMap', extension
.clearcoatNormalTexture));
if (extension.clearcoatNormalTexture.scale !== undefined) {
const scale = extension.clearcoatNormalTexture.scale;
materialParams.clearcoatNormalScale = new three.Vector2(scale, scale);
}
}
return Promise.all(pending);
}
}
/**
* Transmission Materials Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_transmission
* Draft: https://github.com/KhronosGroup/glTF/pull/1698
*/
class GLTFMaterialsTransmissionExtension {
constructor(parser) {
this.parser = parser;
this.name = EXTENSIONS.KHR_MATERIALS_TRANSMISSION;
}
getMaterialType(materialIndex) {
const parser = this.parser;
const materialDef = parser.json.materials[materialIndex];
if (!materialDef.extensions || !materialDef.extensions[this.name]) return null;
return three.MeshPhysicalMaterial;
}
extendMaterialParams(materialIndex, materialParams) {
const parser = this.parser;
const materialDef = parser.json.materials[materialIndex];
if (!materialDef.extensions || !materialDef.extensions[this.name]) {
return Promise.resolve();
}
const pending = [];
const extension = materialDef.extensions[this.name];
if (extension.transmissionFactor !== undefined) {
materialParams.transmission = extension.transmissionFactor;
}
if (extension.transmissionTexture !== undefined) {
pending.push(parser.assignTexture(materialParams, 'transmissionMap', extension.transmissionTexture));
}
return Promise.all(pending);
}
}
/**
* Materials Volume Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_volume
*/
class GLTFMaterialsVolumeExtension {
constructor(parser) {
this.parser = parser;
this.name = EXTENSIONS.KHR_MATERIALS_VOLUME;
}
getMaterialType(materialIndex) {
const parser = this.parser;
const materialDef = parser.json.materials[materialIndex];
if (!materialDef.extensions || !materialDef.extensions[this.name]) return null;
return three.MeshPhysicalMaterial;
}
extendMaterialParams(materialIndex, materialParams) {
const parser = this.parser;
const materialDef = parser.json.materials[materialIndex];
if (!materialDef.extensions || !materialDef.extensions[this.name]) {
return Promise.resolve();
}
const pending = [];
const extension = materialDef.extensions[this.name];
materialParams.thickness = extension.thicknessFactor !== undefined ? extension.thicknessFactor : 0;
if (extension.thicknessTexture !== undefined) {
pending.push(parser.assignTexture(materialParams, 'thicknessMap', extension.thicknessTexture));
}
materialParams.attenuationDistance = extension.attenuationDistance || 0;
const colorArray = extension.attenuationColor || [1, 1, 1];
materialParams.attenuationTint = new three.Color(colorArray[0], colorArray[1], colorArray[2]);
return Promise.all(pending);
}
}
/**
* Materials ior Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_ior
*/
class GLTFMaterialsIorExtension {
constructor(parser) {
this.parser = parser;
this.name = EXTENSIONS.KHR_MATERIALS_IOR;
}
getMaterialType(materialIndex) {
const parser = this.parser;
const materialDef = parser.json.materials[materialIndex];
if (!materialDef.extensions || !materialDef.extensions[this.name]) return null;
return three.MeshPhysicalMaterial;
}
extendMaterialParams(materialIndex, materialParams) {
const parser = this.parser;
const materialDef = parser.json.materials[materialIndex];
if (!materialDef.extensions || !materialDef.extensions[this.name]) {
return Promise.resolve();
}
const extension = materialDef.extensions[this.name];
materialParams.ior = extension.ior !== undefined ? extension.ior : 1.5;
return Promise.resolve();
}
}
/**
* Materials specular Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_specular
*/
class GLTFMaterialsSpecularExtension {
constructor(parser) {
this.parser = parser;
this.name = EXTENSIONS.KHR_MATERIALS_SPECULAR;
}
getMaterialType(materialIndex) {
const parser = this.parser;
const materialDef = parser.json.materials[materialIndex];
if (!materialDef.extensions || !materialDef.extensions[this.name]) return null;
return three.MeshPhysicalMaterial;
}
extendMaterialParams(materialIndex, materialParams) {
const parser = this.parser;
const materialDef = parser.json.materials[materialIndex];
if (!materialDef.extensions || !materialDef.extensions[this.name]) {
return Promise.resolve();
}
const pending = [];
const extension = materialDef.extensions[this.name];
materialParams.specularIntensity = extension.specularFactor !== undefined ? extension.specularFactor : 1.0;
if (extension.specularTexture !== undefined) {
pending.push(parser.assignTexture(materialParams, 'specularIntensityMap', extension.specularTexture));
}
const colorArray = extension.specularColorFactor || [1, 1, 1];
materialParams.specularTint = new three.Color(colorArray[0], colorArray[1], colorArray[2]);
if (extension.specularColorTexture !== undefined) {
pending.push(parser.assignTexture(materialParams, 'specularTintMap', extension.specularColorTexture)
.then(function(texture) {
texture.encoding = three.sRGBEncoding;
}));
}
return Promise.all(pending);
}
}
/**
* BasisU Texture Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_texture_basisu
*/
class GLTFTextureBasisUExtension {
constructor(parser) {
this.parser = parser;
this.name = EXTENSIONS.KHR_TEXTURE_BASISU;
}
loadTexture(textureIndex) {
const parser = this.parser;
const json = parser.json;
const textureDef = json.textures[textureIndex];
if (!textureDef.extensions || !textureDef.extensions[this.name]) {
return null;
}
const extension = textureDef.extensions[this.name];
const source = json.images[extension.source];
const loader = parser.options.ktx2Loader;
if (!loader) {
if (json.extensionsRequired && json.extensionsRequired.indexOf(this.name) >= 0) {
throw new Error('THREE.GLTFLoader: setKTX2Loader must be called before loading KTX2 textures');
} else {
// Assumes that the extension is optional and that a fallback texture is present
return null;
}
}
return parser.loadTextureImage(textureIndex, source, loader);
}
}
/**
* WebP Texture Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/EXT_texture_webp
*/
class GLTFTextureWebPExtension {
constructor(parser) {
this.parser = parser;
this.name = EXTENSIONS.EXT_TEXTURE_WEBP;
this.isSupported = null;
}
loadTexture(textureIndex) {
const name = this.name;
const parser = this.parser;
const json = parser.json;
const textureDef = json.textures[textureIndex];
if (!textureDef.extensions || !textureDef.extensions[name]) {
return null;
}
const extension = textureDef.extensions[name];
const source = json.images[extension.source];
let loader = parser.textureLoader;
if (source.uri) {
const handler = parser.options.manager.getHandler(source.uri);
if (handler !== null) loader = handler;
}
return this.detectSupport().then(function(isSupported) {
if (isSupported) return parser.loadTextureImage(textureIndex, source, loader);
if (json.extensionsRequired && json.extensionsRequired.indexOf(name) >= 0) {
throw new Error('THREE.GLTFLoader: WebP required by asset but unsupported.');
}
// Fall back to PNG or JPEG.
return parser.loadTexture(textureIndex);
});
}
detectSupport() {
if (!this.isSupported) {
this.isSupported = new Promise(function(resolve) {
const image = new three.PlatformManager.polyfill.Image();
// Lossy test image. Support for lossy images doesn't guarantee support for all
// WebP images, unfortunately.
image.src =
'data:image/webp;base64,UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA';
image.onload = image.onerror = function() {
resolve(image.height === 1);
};
});
}
return this.isSupported;
}
}
/**
* meshopt BufferView Compression Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/EXT_meshopt_compression
*/
class GLTFMeshoptCompression {
constructor(parser) {
this.name = EXTENSIONS.EXT_MESHOPT_COMPRESSION;
this.parser = parser;
}
loadBufferView(index) {
const json = this.parser.json;
const bufferView = json.bufferViews[index];
if (bufferView.extensions && bufferView.extensions[this.name]) {
const extensionDef = bufferView.extensions[this.name];
const buffer = this.parser.getDependency('buffer', extensionDef.buffer);
const decoder = this.parser.options.meshoptDecoder;
if (!decoder || !decoder.supported) {
if (json.extensionsRequired && json.extensionsRequired.indexOf(this.name) >= 0) {
throw new Error(
'THREE.GLTFLoader: setMeshoptDecoder must be called before loading compressed files');
} else {
// Assumes that the extension is optional and that fallback buffer data is present
return null;
}
}
return Promise.all([buffer, decoder.ready]).then(function(res) {
const byteOffset = extensionDef.byteOffset || 0;
const byteLength = extensionDef.byteLength || 0;
const count = extensionDef.count;
const stride = extensionDef.byteStride;
const result = new ArrayBuffer(count * stride);
const source = new Uint8Array(res[0], byteOffset, byteLength);
decoder.decodeGltfBuffer(new Uint8Array(result), count, stride, source, extensionDef.mode,
extensionDef.filter);
return result;
});
} else {
return null;
}
}
}
/* BINARY EXTENSION */
const BINARY_EXTENSION_HEADER_MAGIC = 'glTF';
const BINARY_EXTENSION_HEADER_LENGTH = 12;
const BINARY_EXTENSION_CHUNK_TYPES = {
JSON: 0x4E4F534A,
BIN: 0x004E4942
};
class GLTFBinaryExtension {
constructor(data) {
this.name = EXTENSIONS.KHR_BINARY_GLTF;
this.content = null;
this.body = null;
const headerView = new DataView(data, 0, BINARY_EXTENSION_HEADER_LENGTH);
this.header = {
magic: three.LoaderUtils.decodeText(new Uint8Array(data.slice(0, 4))),
version: headerView.getUint32(4, true),
length: headerView.getUint32(8, true)
};
if (this.header.magic !== BINARY_EXTENSION_HEADER_MAGIC) {
throw new Error('THREE.GLTFLoader: Unsupported glTF-Binary header.');
} else if (this.header.version < 2.0) {
throw new Error('THREE.GLTFLoader: Legacy binary file detected.');
}
const chunkContentsLength = this.header.length - BINARY_EXTENSION_HEADER_LENGTH;
const chunkView = new DataView(data, BINARY_EXTENSION_HEADER_LENGTH);
let chunkIndex = 0;
while (chunkIndex < chunkContentsLength) {
const chunkLength = chunkView.getUint32(chunkIndex, true);
chunkIndex += 4;
const chunkType = chunkView.getUint32(chunkIndex, true);
chunkIndex += 4;
if (chunkType === BINARY_EXTENSION_CHUNK_TYPES.JSON) {
const contentArray = new Uint8Array(data, BINARY_EXTENSION_HEADER_LENGTH + chunkIndex,
chunkLength);
this.content = three.LoaderUtils.decodeText(contentArray);
} else if (chunkType === BINARY_EXTENSION_CHUNK_TYPES.BIN) {
const byteOffset = BINARY_EXTENSION_HEADER_LENGTH + chunkIndex;
this.body = data.slice(byteOffset, byteOffset + chunkLength);
}
// Clients must ignore chunks with unknown types.
chunkIndex += chunkLength;
}
if (this.content === null) {
throw new Error('THREE.GLTFLoader: JSON content not found.');
}
}
}
/**
* DRACO Mesh Compression Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression
*/
class GLTFDracoMeshCompressionExtension {
constructor(json, dracoLoader) {
if (!dracoLoader) {
throw new Error('THREE.GLTFLoader: No DRACOLoader instance provided.');
}
this.name = EXTENSIONS.KHR_DRACO_MESH_COMPRESSION;
this.json = json;
this.dracoLoader = dracoLoader;
this.dracoLoader.preload();
}
decodePrimitive(primitive, parser) {
const json = this.json;
const dracoLoader = this.dracoLoader;
const bufferViewIndex = primitive.extensions[this.name].bufferView;
const gltfAttributeMap = primitive.extensions[this.name].attributes;
const threeAttributeMap = {};
const attributeNormalizedMap = {};
const attributeTypeMap = {};
for (const attributeName in gltfAttributeMap) {
const threeAttributeName = ATTRIBUTES[attributeName] || attributeName.toLowerCase();
threeAttributeMap[threeAttributeName] = gltfAttributeMap[attributeName];
}
for (const attributeName in primitive.attributes) {
const threeAttributeName = ATTRIBUTES[attributeName] || attributeName.toLowerCase();
if (gltfAttributeMap[attributeName] !== undefined) {
const accessorDef = json.accessors[primitive.attributes[attributeName]];
const componentType = WEBGL_COMPONENT_TYPES[accessorDef.componentType];
attributeTypeMap[threeAttributeName] = componentType;
attributeNormalizedMap[threeAttributeName] = accessorDef.normalized === true;
}
}
return parser.getDependency('bufferView', bufferViewIndex).then(function(bufferView) {
return new Promise(function(resolve) {
dracoLoader.decodeDracoFile(bufferView, function(geometry) {
for (const attributeName in geometry.attributes) {
const attribute = geometry.attributes[attributeName];
const normalized = attributeNormalizedMap[attributeName];
if (normalized !== undefined) attribute.normalized = normalized;
}
resolve(geometry);
}, threeAttributeMap, attributeTypeMap);
});
});
}
}
/**
* Texture Transform Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_texture_transform
*/
class GLTFTextureTransformExtension {
constructor() {
this.name = EXTENSIONS.KHR_TEXTURE_TRANSFORM;
}
extendTexture(texture, transform) {
if (transform.texCoord !== undefined) {
console.warn('THREE.GLTFLoader: Custom UV sets in "' + this.name + '" extension not yet supported.');
}
if (transform.offset === undefined && transform.rotation === undefined && transform.scale === undefined) {
// See https://github.com/mrdoob/three.js/issues/21819.
return texture;
}
texture = texture.clone();
if (transform.offset !== undefined) {
texture.offset.fromArray(transform.offset);
}
if (transform.rotation !== undefined) {
texture.rotation = transform.rotation;
}
if (transform.scale !== undefined) {
texture.repeat.fromArray(transform.scale);
}
texture.needsUpdate = true;
return texture;
}
}
/**
* Specular-Glossiness Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_pbrSpecularGlossiness
*/
/**
* A sub class of StandardMaterial with some of the functionality
* changed via the `onBeforeCompile` callback
* @pailhead
*/
class GLTFMeshStandardSGMaterial extends three.MeshStandardMaterial {
constructor(params) {
super();
this.isGLTFSpecularGlossinessMaterial = true;
//various chunks that need replacing
const specularMapParsFragmentChunk = [
'#ifdef USE_SPECULARMAP',
' uniform sampler2D specularMap;',
'#endif'
].join('\n');
const glossinessMapParsFragmentChunk = [
'#ifdef USE_GLOSSINESSMAP',
' uniform sampler2D glossinessMap;',
'#endif'
].join('\n');
const specularMapFragmentChunk = [
'vec3 specularFactor = specular;',
'#ifdef USE_SPECULARMAP',
' vec4 texelSpecular = texture2D( specularMap, vUv );',
' texelSpecular = sRGBToLinear( texelSpecular );',
' // reads channel RGB, compatible with a glTF Specular-Glossiness (RGBA) texture',
' specularFactor *= texelSpecular.rgb;',
'#endif'
].join('\n');
const glossinessMapFragmentChunk = [
'float glossinessFactor = glossiness;',
'#ifdef USE_GLOSSINESSMAP',
' vec4 texelGlossiness = texture2D( glossinessMap, vUv );',
' // reads channel A, compatible with a glTF Specular-Glossiness (RGBA) texture',
' glossinessFactor *= texelGlossiness.a;',
'#endif'
].join('\n');
const lightPhysicalFragmentChunk = [
'PhysicalMaterial material;',
'material.diffuseColor = diffuseColor.rgb * ( 1. - max( specularFactor.r, max( specularFactor.g, specularFactor.b ) ) );',
'vec3 dxy = max( abs( dFdx( geometryNormal ) ), abs( dFdy( geometryNormal ) ) );',
'float geometryRoughness = max( max( dxy.x, dxy.y ), dxy.z );',
'material.roughness = max( 1.0 - glossinessFactor, 0.0525 ); // 0.0525 corresponds to the base mip of a 256 cubemap.',
'material.roughness += geometryRoughness;',
'material.roughness = min( material.roughness, 1.0 );',
'material.specularColor = specularFactor;',
].join('\n');
const uniforms = {
specular: {
value: new three.Color().setHex(0xffffff)
},
glossiness: {
value: 1
},
specularMap: {
value: null
},
glossinessMap: {
value: null
}
};
this._extraUniforms = uniforms;
this.onBeforeCompile = function(shader) {
for (const uniformName in uniforms) {
shader.uniforms[uniformName] = uniforms[uniformName];
}
shader.fragmentShader = shader.fragmentShader
.replace('uniform float roughness;', 'uniform vec3 specular;')
.replace('uniform float metalness;', 'uniform float glossiness;')
.replace('#include <roughnessmap_pars_fragment>', specularMapParsFragmentChunk)
.replace('#include <metalnessmap_pars_fragment>', glossinessMapParsFragmentChunk)
.replace('#include <roughnessmap_fragment>', specularMapFragmentChunk)
.replace('#include <metalnessmap_fragment>', glossinessMapFragmentChunk)
.replace('#include <lights_physical_fragment>', lightPhysicalFragmentChunk);
};
Object.defineProperties(this, {
specular: {
get: function() {
return uniforms.specular.value;
},
set: function(v) {
uniforms.specular.value = v;
}
},
specularMap: {
get: function() {
return uniforms.specularMap.value;
},
set: function(v) {
uniforms.specularMap.value = v;
if (v) {
this.defines.USE_SPECULARMAP =
''; // USE_UV is set by the renderer for specular maps
} else {
delete this.defines.USE_SPECULARMAP;
}
}
},
glossiness: {
get: function() {
return uniforms.glossiness.value;
},
set: function(v) {
uniforms.glossiness.value = v;
}
},
glossinessMap: {
get: function() {
return uniforms.glossinessMap.value;
},
set: function(v) {
uniforms.glossinessMap.value = v;
if (v) {
this.defines.USE_GLOSSINESSMAP = '';
this.defines.USE_UV = '';
} else {
delete this.defines.USE_GLOSSINESSMAP;
delete this.defines.USE_UV;
}
}
}
});
delete this.metalness;
delete this.roughness;
delete this.metalnessMap;
delete this.roughnessMap;
this.setValues(params);
}
copy(source) {
super.copy(source);
this.specularMap = source.specularMap;
this.specular.copy(source.specular);
this.glossinessMap = source.glossinessMap;
this.glossiness = source.glossiness;
delete this.metalness;
delete this.roughness;
delete this.metalnessMap;
delete this.roughnessMap;
return this;
}
}
class GLTFMaterialsPbrSpecularGlossinessExtension {
constructor() {
this.name = EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS;
this.specularGlossinessParams = [
'color',
'map',
'lightMap',
'lightMapIntensity',
'aoMap',
'aoMapIntensity',
'emissive',
'emissiveIntensity',
'emissiveMap',
'bumpMap',
'bumpScale',
'normalMap',
'normalMapType',
'displacementMap',
'displacementScale',
'displacementBias',
'specularMap',
'specular',
'glossinessMap',
'glossiness',
'alphaMap',
'envMap',
'envMapIntensity',
'refractionRatio',
];
}
getMaterialType() {
return GLTFMeshStandardSGMaterial;
}
extendParams(materialParams, materialDef, parser) {
const pbrSpecularGlossiness = materialDef.extensions[this.name];
materialParams.color = new three.Color(1.0, 1.0, 1.0);
materialParams.opacity = 1.0;
const pending = [];
if (Array.isArray(pbrSpecularGlossiness.diffuseFactor)) {
const array = pbrSpecularGlossiness.diffuseFactor;
materialParams.color.fromArray(array);
materialParams.opacity = array[3];
}
if (pbrSpecularGlossiness.diffuseTexture !== undefined) {
pending.push(parser.assignTexture(materialParams, 'map', pbrSpecularGlossiness.diffuseTexture));
}
materialParams.emissive = new three.Color(0.0, 0.0, 0.0);
materialParams.glossiness = pbrSpecularGlossiness.glossinessFactor !== undefined ? pbrSpecularGlossiness
.glossinessFactor : 1.0;
materialParams.specular = new three.Color(1.0, 1.0, 1.0);
if (Array.isArray(pbrSpecularGlossiness.specularFactor)) {
materialParams.specular.fromArray(pbrSpecularGlossiness.specularFactor);
}
if (pbrSpecularGlossiness.specularGlossinessTexture !== undefined) {
const specGlossMapDef = pbrSpecularGlossiness.specularGlossinessTexture;
pending.push(parser.assignTexture(materialParams, 'glossinessMap', specGlossMapDef));
pending.push(parser.assignTexture(materialParams, 'specularMap', specGlossMapDef));
}
return Promise.all(pending);
}
createMaterial(materialParams) {
const material = new GLTFMeshStandardSGMaterial(materialParams);
material.fog = true;
material.color = materialParams.color;
material.map = materialParams.map === undefined ? null : materialParams.map;
material.lightMap = null;
material.lightMapIntensity = 1.0;
material.aoMap = materialParams.aoMap === undefined ? null : materialParams.aoMap;
material.aoMapIntensity = 1.0;
material.emissive = materialParams.emissive;
material.emissiveIntensity = 1.0;
material.emissiveMap = materialParams.emissiveMap === undefined ? null : materialParams.emissiveMap;
material.bumpMap = materialParams.bumpMap === undefined ? null : materialParams.bumpMap;
material.bumpScale = 1;
material.normalMap = materialParams.normalMap === undefined ? null : materialParams.normalMap;
material.normalMapType = three.TangentSpaceNormalMap;
if (materialParams.normalScale) material.normalScale = materialParams.normalScale;
material.displacementMap = null;
material.displacementScale = 1;
material.displacementBias = 0;
material.specularMap = materialParams.specularMap === undefined ? null : materialParams.specularMap;
material.specular = materialParams.specular;
material.glossinessMap = materialParams.glossinessMap === undefined ? null : materialParams.glossinessMap;
material.glossiness = materialParams.glossiness;
material.alphaMap = null;
material.envMap = materialParams.envMap === undefined ? null : materialParams.envMap;
material.envMapIntensity = 1.0;
material.refractionRatio = 0.98;
return material;
}
}
/**
* Mesh Quantization Extension
*
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_mesh_quantization
*/
class GLTFMeshQuantizationExtension {
constructor() {
this.name = EXTENSIONS.KHR_MESH_QUANTIZATION;
}
}
/*********************************/
/********** INTERPOLATION ********/
/*********************************/
// Spline Interpolation
// Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#appendix-c-spline-interpolation
class GLTFCubicSplineInterpolant extends three.Interpolant {
constructor(parameterPositions, sampleValues, sampleSize, resultBuffer) {
super(parameterPositions, sampleValues, sampleSize, resultBuffer);
}
copySampleValue_(index) {
// Copies a sample value to the result buffer. See description of glTF
// CUBICSPLINE values layout in interpolate_() function below.
const result = this.resultBuffer,
values = this.sampleValues,
valueSize = this.valueSize,
offset = index * valueSize * 3 + valueSize;
for (let i = 0; i !== valueSize; i++) {
result[i] = values[offset + i];
}
return result;
}
}
GLTFCubicSplineInterpolant.prototype.beforeStart_ = GLTFCubicSplineInterpolant.prototype.copySampleValue_;
GLTFCubicSplineInterpolant.prototype.afterEnd_ = GLTFCubicSplineInterpolant.prototype.copySampleValue_;
GLTFCubicSplineInterpolant.prototype.interpolate_ = function(i1, t0, t, t1) {
const result = this.resultBuffer;
const values = this.sampleValues;
const stride = this.valueSize;
const stride2 = stride * 2;
const stride3 = stride * 3;
const td = t1 - t0;
const p = (t - t0) / td;
const pp = p * p;
const ppp = pp * p;
const offset1 = i1 * stride3;
const offset0 = offset1 - stride3;
const s2 = -2 * ppp + 3 * pp;
const s3 = ppp - pp;
const s0 = 1 - s2;
const s1 = s3 - pp + p;
// Layout of keyframe output values for CUBICSPLINE animations:
// [ inTangent_1, splineVertex_1, outTangent_1, inTangent_2, splineVertex_2, ... ]
for (let i = 0; i !== stride; i++) {
const p0 = values[offset0 + i + stride]; // splineVertex_k
const m0 = values[offset0 + i + stride2] * td; // outTangent_k * (t_k+1 - t_k)
const p1 = values[offset1 + i + stride]; // splineVertex_k+1
const m1 = values[offset1 + i] * td; // inTangent_k+1 * (t_k+1 - t_k)
result[i] = s0 * p0 + s1 * m0 + s2 * p1 + s3 * m1;
}
return result;
};
const _q = new three.Quaternion();
class GLTFCubicSplineQuaternionInterpolant extends GLTFCubicSplineInterpolant {
interpolate_(i1, t0, t, t1) {
const result = super.interpolate_(i1, t0, t, t1);
_q.fromArray(result).normalize().toArray(result);
return result;
}
}
/*********************************/
/********** INTERNALS ************/
/*********************************/
/* CONSTANTS */
const WEBGL_CONSTANTS = {
FLOAT: 5126,
//FLOAT_MAT2: 35674,
FLOAT_MAT3: 35675,
FLOAT_MAT4: 35676,
FLOAT_VEC2: 35664,
FLOAT_VEC3: 35665,
FLOAT_VEC4: 35666,
LINEAR: 9729,
REPEAT: 10497,
SAMPLER_2D: 35678,
POINTS: 0,
LINES: 1,
LINE_LOOP: 2,
LINE_STRIP: 3,
TRIANGLES: 4,
TRIANGLE_STRIP: 5,
TRIANGLE_FAN: 6,
UNSIGNED_BYTE: 5121,
UNSIGNED_SHORT: 5123
};
const WEBGL_COMPONENT_TYPES = {
5120: Int8Array,
5121: Uint8Array,
5122: Int16Array,
5123: Uint16Array,
5125: Uint32Array,
5126: Float32Array
};
const WEBGL_FILTERS = {
9728: three.NearestFilter,
9729: three.LinearFilter,
9984: three.NearestMipmapNearestFilter,
9985: three.LinearMipmapNearestFilter,
9986: three.NearestMipmapLinearFilter,
9987: three.LinearMipmapLinearFilter
};
const WEBGL_WRAPPINGS = {
33071: three.ClampToEdgeWrapping,
33648: three.MirroredRepeatWrapping,
10497: three.RepeatWrapping
};
const WEBGL_TYPE_SIZES = {
'SCALAR': 1,
'VEC2': 2,
'VEC3': 3,
'VEC4': 4,
'MAT2': 4,
'MAT3': 9,
'MAT4': 16
};
const ATTRIBUTES = {
POSITION: 'position',
NORMAL: 'normal',
TANGENT: 'tangent',
TEXCOORD_0: 'uv',
TEXCOORD_1: 'uv2',
COLOR_0: 'color',
WEIGHTS_0: 'skinWeight',
JOINTS_0: 'skinIndex',
};
const PATH_PROPERTIES = {
scale: 'scale',
translation: 'position',
rotation: 'quaternion',
weights: 'morphTargetInfluences'
};
const INTERPOLATION = {
CUBICSPLINE: undefined, // We use a custom interpolant (GLTFCubicSplineInterpolation) for CUBICSPLINE tracks. Each
// keyframe track will be initialized with a default interpolation type, then modified.
LINEAR: three.InterpolateLinear,
STEP: three.InterpolateDiscrete
};
const ALPHA_MODES = {
OPAQUE: 'OPAQUE',
MASK: 'MASK',
BLEND: 'BLEND'
};
/* UTILITY FUNCTIONS */
function resolveURL(url, path) {
// Invalid URL
if (typeof url !== 'string' || url === '') return '';
// Host Relative URL
if (/^https?:\/\//i.test(path) && /^\//.test(url)) {
path = path.replace(/(^https?:\/\/[^\/]+).*/i, '$1');
}
// Absolute URL http://,https://,//
if (/^(https?:)?\/\//i.test(url)) return url;
// Data URI
if (/^data:.*,.*$/i.test(url)) return url;
// Blob URL
if (/^blob:.*$/i.test(url)) return url;
// Relative URL
return path + url;
}
/**
* Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#default-material
*/
function createDefaultMaterial(cache) {
if (cache['DefaultMaterial'] === undefined) {
cache['DefaultMaterial'] = new three.MeshStandardMaterial({
color: 0xFFFFFF,
emissive: 0x000000,
metalness: 1,
roughness: 1,
transparent: false,
depthTest: true,
side: three.FrontSide
});
}
return cache['DefaultMaterial'];
}
function addUnknownExtensionsToUserData(knownExtensions, object, objectDef) {
// Add unknown glTF extensions to an object's userData.
for (const name in objectDef.extensions) {
if (knownExtensions[name] === undefined) {
object.userData.gltfExtensions = object.userData.gltfExtensions || {};
object.userData.gltfExtensions[name] = objectDef.extensions[name];
}
}
}
/**
* @param {Object3D|Material|BufferGeometry} object
* @param {GLTF.definition} gltfDef
*/
function assignExtrasToUserData(object, gltfDef) {
if (gltfDef.extras !== undefined) {
if (typeof gltfDef.extras === 'object') {
Object.assign(object.userData, gltfDef.extras);
} else {
console.warn('THREE.GLTFLoader: Ignoring primitive type .extras, ' + gltfDef.extras);
}
}
}
/**
* Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#morph-targets
*
* @param {BufferGeometry} geometry
* @param {Array<GLTF.Target>} targets
* @param {GLTFParser} parser
* @return {Promise<BufferGeometry>}
*/
function addMorphTargets(geometry, targets, parser) {
let hasMorphPosition = false;
let hasMorphNormal = false;
for (let i = 0, il = targets.length; i < il; i++) {
const target = targets[i];
if (target.POSITION !== undefined) hasMorphPosition = true;
if (target.NORMAL !== undefined) hasMorphNormal = true;
if (hasMorphPosition && hasMorphNormal) break;
}
if (!hasMorphPosition && !hasMorphNormal) return Promise.resolve(geometry);
const pendingPositionAccessors = [];
const pendingNormalAccessors = [];
for (let i = 0, il = targets.length; i < il; i++) {
const target = targets[i];
if (hasMorphPosition) {
const pendingAccessor = target.POSITION !== undefined ?
parser.getDependency('accessor', target.POSITION) :
geometry.attributes.position;
pendingPositionAccessors.push(pendingAccessor);
}
if (hasMorphNormal) {
const pendingAccessor = target.NORMAL !== undefined ?
parser.getDependency('accessor', target.NORMAL) :
geometry.attributes.normal;
pendingNormalAccessors.push(pendingAccessor);
}
}
return Promise.all([
Promise.all(pendingPositionAccessors),
Promise.all(pendingNormalAccessors)
]).then(function(accessors) {
const morphPositions = accessors[0];
const morphNormals = accessors[1];
if (hasMorphPosition) geometry.morphAttributes.position = morphPositions;
if (hasMorphNormal) geometry.morphAttributes.normal = morphNormals;
geometry.morphTargetsRelative = true;
return geometry;
});
}
/**
* @param {Mesh} mesh
* @param {GLTF.Mesh} meshDef
*/
function updateMorphTargets(mesh, meshDef) {
mesh.updateMorphTargets();
if (meshDef.weights !== undefined) {
for (let i = 0, il = meshDef.weights.length; i < il; i++) {
mesh.morphTargetInfluences[i] = meshDef.weights[i];
}
}
// .extras has user-defined data, so check that .extras.targetNames is an array.
if (meshDef.extras && Array.isArray(meshDef.extras.targetNames)) {
const targetNames = meshDef.extras.targetNames;
if (mesh.morphTargetInfluences.length === targetNames.length) {
mesh.morphTargetDictionary = {};
for (let i = 0, il = targetNames.length; i < il; i++) {
mesh.morphTargetDictionary[targetNames[i]] = i;
}
} else {
console.warn('THREE.GLTFLoader: Invalid extras.targetNames length. Ignoring names.');
}
}
}
function createPrimitiveKey(primitiveDef) {
const dracoExtension = primitiveDef.extensions && primitiveDef.extensions[EXTENSIONS.KHR_DRACO_MESH_COMPRESSION];
let geometryKey;
if (dracoExtension) {
geometryKey = 'draco:' + dracoExtension.bufferView +
':' + dracoExtension.indices +
':' + createAttributesKey(dracoExtension.attributes);
} else {
geometryKey = primitiveDef.indices + ':' + createAttributesKey(primitiveDef.attributes) + ':' + primitiveDef
.mode;
}
return geometryKey;
}
function createAttributesKey(attributes) {
let attributesKey = '';
const keys = Object.keys(attributes).sort();
for (let i = 0, il = keys.length; i < il; i++) {
attributesKey += keys[i] + ':' + attributes[keys[i]] + ';';
}
return attributesKey;
}
function getNormalizedComponentScale(constructor) {
// Reference:
// https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_mesh_quantization#encoding-quantized-data
switch (constructor) {
case Int8Array:
return 1 / 127;
case Uint8Array:
return 1 / 255;
case Int16Array:
return 1 / 32767;
case Uint16Array:
return 1 / 65535;
default:
throw new Error('THREE.GLTFLoader: Unsupported normalized accessor component type.');
}
}
/* GLTF PARSER */
class GLTFParser {
constructor(json = {}, options = {}) {
this.json = json;
this.extensions = {};
this.plugins = {};
this.options = options;
// loader object cache
this.cache = new GLTFRegistry();
// associations between Three.js objects and glTF elements
this.associations = new Map();
// BufferGeometry caching
this.primitiveCache = {};
// Object3D instance caches
this.meshCache = {
refs: {},
uses: {}
};
this.cameraCache = {
refs: {},
uses: {}
};
this.lightCache = {
refs: {},
uses: {}
};
this.textureCache = {};
// Track node names, to ensure no duplicates
this.nodeNamesUsed = {};
// Use an ImageBitmapLoader if imageBitmaps are supported. Moves much of the
// expensive work of uploading a texture to the GPU off the main thread.
if (typeof three.PlatformManager.polyfill.createImageBitmap !== 'undefined' && /Firefox/.test(three
.PlatformManager.polyfill.navigator.userAgent) === false) {
this.textureLoader = new three.ImageBitmapLoader(this.options.manager);
} else {
this.textureLoader = new three.TextureLoader(this.options.manager);
}
this.textureLoader.setCrossOrigin(this.options.crossOrigin);
this.textureLoader.setRequestHeader(this.options.requestHeader);
this.fileLoader = new three.FileLoader(this.options.manager);
this.fileLoader.setResponseType('arraybuffer');
if (this.options.crossOrigin === 'use-credentials') {
this.fileLoader.setWithCredentials(true);
}
}
setExtensions(extensions) {
this.extensions = extensions;
}
setPlugins(plugins) {
this.plugins = plugins;
}
parse(onLoad, onError) {
const parser = this;
const json = this.json;
const extensions = this.extensions;
// Clear the loader cache
this.cache.removeAll();
// Mark the special nodes/meshes in json for efficient parse
this._invokeAll(function(ext) {
return ext._markDefs && ext._markDefs();
});
Promise.all(this._invokeAll(function(ext) {
return ext.beforeRoot && ext.beforeRoot();
})).then(function() {
return Promise.all([
parser.getDependencies('scene'),
parser.getDependencies('animation'),
parser.getDependencies('camera'),
]);
}).then(function(dependencies) {
const result = {
scene: dependencies[0][json.scene || 0],
scenes: dependencies[0],
animations: dependencies[1],
cameras: dependencies[2],
asset: json.asset,
parser: parser,
userData: {}
};
addUnknownExtensionsToUserData(extensions, result, json);
assignExtrasToUserData(result, json);
Promise.all(parser._invokeAll(function(ext) {
return ext.afterRoot && ext.afterRoot(result);
})).then(function() {
onLoad(result);
});
}).catch(onError);
}
/**
* Marks the special nodes/meshes in json for efficient parse.
*/
_markDefs() {
const nodeDefs = this.json.nodes || [];
const skinDefs = this.json.skins || [];
const meshDefs = this.json.meshes || [];
// Nothing in the node definition indicates whether it is a Bone or an
// Object3D. Use the skins' joint references to mark bones.
for (let skinIndex = 0, skinLength = skinDefs.length; skinIndex < skinLength; skinIndex++) {
const joints = skinDefs[skinIndex].joints;
for (let i = 0, il = joints.length; i < il; i++) {
nodeDefs[joints[i]].isBone = true;
}
}
// Iterate over all nodes, marking references to shared resources,
// as well as skeleton joints.
for (let nodeIndex = 0, nodeLength = nodeDefs.length; nodeIndex < nodeLength; nodeIndex++) {
const nodeDef = nodeDefs[nodeIndex];
if (nodeDef.mesh !== undefined) {
this._addNodeRef(this.meshCache, nodeDef.mesh);
// Nothing in the mesh definition indicates whether it is
// a SkinnedMesh or Mesh. Use the node's mesh reference
// to mark SkinnedMesh if node has skin.
if (nodeDef.skin !== undefined) {
meshDefs[nodeDef.mesh].isSkinnedMesh = true;
}
}
if (nodeDef.camera !== undefined) {
this._addNodeRef(this.cameraCache, nodeDef.camera);
}
}
}
/**
* Counts references to shared node / Object3D resources. These resources
* can be reused, or "instantiated", at multiple nodes in the scene
* hierarchy. Mesh, Camera, and Light instances are instantiated and must
* be marked. Non-scenegraph resources (like Materials, Geometries, and
* Textures) can be reused directly and are not marked here.
*
* Example: CesiumMilkTruck sample model reuses "Wheel" meshes.
*/
_addNodeRef(cache, index) {
if (index === undefined) return;
if (cache.refs[index] === undefined) {
cache.refs[index] = cache.uses[index] = 0;
}
cache.refs[index]++;
}
/** Returns a reference to a shared resource, cloning it if necessary. */
_getNodeRef(cache, index, object) {
if (cache.refs[index] <= 1) return object;
const ref = object.clone();
// Propagates mappings to the cloned object, prevents mappings on the
// original object from being lost.
const updateMappings = (original, clone) => {
const mappings = this.associations.get(original);
if (mappings != null) {
this.associations.set(clone, mappings);
}
for (const [i, child] of original.children.entries()) {
updateMappings(child, clone.children[i]);
}
};
updateMappings(object, ref);
ref.name += '_instance_' + (cache.uses[index]++);
return ref;
}
_invokeOne(func) {
const extensions = Object.values(this.plugins);
extensions.push(this);
for (let i = 0; i < extensions.length; i++) {
const result = func(extensions[i]);
if (result) return result;
}
return null;
}
_invokeAll(func) {
const extensions = Object.values(this.plugins);
extensions.unshift(this);
const pending = [];
for (let i = 0; i < extensions.length; i++) {
const result = func(extensions[i]);
if (result) pending.push(result);
}
return pending;
}
/**
* Requests the specified dependency asynchronously, with caching.
* @param {string} type
* @param {number} index
* @return {Promise<Object3D|Material|THREE.Texture|AnimationClip|ArrayBuffer|Object>}
*/
getDependency(type, index) {
const cacheKey = type + ':' + index;
let dependency = this.cache.get(cacheKey);
if (!dependency) {
switch (type) {
case 'scene':
dependency = this.loadScene(index);
break;
case 'node':
dependency = this.loadNode(index);
break;
case 'mesh':
dependency = this._invokeOne(function(ext) {
return ext.loadMesh && ext.loadMesh(index);
});
break;
case 'accessor':
dependency = this.loadAccessor(index);
break;
case 'bufferView':
dependency = this._invokeOne(function(ext) {
return ext.loadBufferView && ext.loadBufferView(index);
});
break;
case 'buffer':
dependency = this.loadBuffer(index);
break;
case 'material':
dependency = this._invokeOne(function(ext) {
return ext.loadMaterial && ext.loadMaterial(index);
});
break;
case 'texture':
dependency = this._invokeOne(function(ext) {
return ext.loadTexture && ext.loadTexture(index);
});
break;
case 'skin':
dependency = this.loadSkin(index);
break;
case 'animation':
dependency = this.loadAnimation(index);
break;
case 'camera':
dependency = this.loadCamera(index);
break;
default:
throw new Error('Unknown type: ' + type);
}
this.cache.add(cacheKey, dependency);
}
return dependency;
}
/**
* Requests all dependencies of the specified type asynchronously, with caching.
* @param {string} type
* @return {Promise<Array<Object>>}
*/
getDependencies(type) {
let dependencies = this.cache.get(type);
if (!dependencies) {
const parser = this;
const defs = this.json[type + (type === 'mesh' ? 'es' : 's')] || [];
dependencies = Promise.all(defs.map(function(def, index) {
return parser.getDependency(type, index);
}));
this.cache.add(type, dependencies);
}
return dependencies;
}
/**
* Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#buffers-and-buffer-views
* @param {number} bufferIndex
* @return {Promise<ArrayBuffer>}
*/
loadBuffer(bufferIndex) {
const bufferDef = this.json.buffers[bufferIndex];
const loader = this.fileLoader;
if (bufferDef.type && bufferDef.type !== 'arraybuffer') {
throw new Error('THREE.GLTFLoader: ' + bufferDef.type + ' buffer type is not supported.');
}
// If present, GLB container is required to be the first buffer.
if (bufferDef.uri === undefined && bufferIndex === 0) {
return Promise.resolve(this.extensions[EXTENSIONS.KHR_BINARY_GLTF].body);
}
const options = this.options;
return new Promise(function(resolve, reject) {
loader.load(resolveURL(bufferDef.uri, options.path), resolve, undefined, function() {
reject(new Error('THREE.GLTFLoader: Failed to load buffer "' + bufferDef.uri +
'".'));
});
});
}
/**
* Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#buffers-and-buffer-views
* @param {number} bufferViewIndex
* @return {Promise<ArrayBuffer>}
*/
loadBufferView(bufferViewIndex) {
const bufferViewDef = this.json.bufferViews[bufferViewIndex];
return this.getDependency('buffer', bufferViewDef.buffer).then(function(buffer) {
const byteLength = bufferViewDef.byteLength || 0;
const byteOffset = bufferViewDef.byteOffset || 0;
return buffer.slice(byteOffset, byteOffset + byteLength);
});
}
/**
* Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#accessors
* @param {number} accessorIndex
* @return {Promise<BufferAttribute|InterleavedBufferAttribute>}
*/
loadAccessor(accessorIndex) {
const parser = this;
const json = this.json;
const accessorDef = this.json.accessors[accessorIndex];
if (accessorDef.bufferView === undefined && accessorDef.sparse === undefined) {
// Ignore empty accessors, which may be used to declare runtime
// information about attributes coming from another source (e.g. Draco
// compression extension).
return Promise.resolve(null);
}
const pendingBufferViews = [];
if (accessorDef.bufferView !== undefined) {
pendingBufferViews.push(this.getDependency('bufferView', accessorDef.bufferView));
} else {
pendingBufferViews.push(null);
}
if (accessorDef.sparse !== undefined) {
pendingBufferViews.push(this.getDependency('bufferView', accessorDef.sparse.indices.bufferView));
pendingBufferViews.push(this.getDependency('bufferView', accessorDef.sparse.values.bufferView));
}
return Promise.all(pendingBufferViews).then(function(bufferViews) {
const bufferView = bufferViews[0];
const itemSize = WEBGL_TYPE_SIZES[accessorDef.type];
const TypedArray = WEBGL_COMPONENT_TYPES[accessorDef.componentType];
// For VEC3: itemSize is 3, elementBytes is 4, itemBytes is 12.
const elementBytes = TypedArray.BYTES_PER_ELEMENT;
const itemBytes = elementBytes * itemSize;
const byteOffset = accessorDef.byteOffset || 0;
const byteStride = accessorDef.bufferView !== undefined ? json.bufferViews[accessorDef
.bufferView].byteStride : undefined;
const normalized = accessorDef.normalized === true;
let array, bufferAttribute;
// The buffer is not interleaved if the stride is the item size in bytes.
if (byteStride && byteStride !== itemBytes) {
// Each "slice" of the buffer, as defined by 'count' elements of 'byteStride' bytes, gets its own InterleavedBuffer
// This makes sure that IBA.count reflects accessor.count properly
const ibSlice = Math.floor(byteOffset / byteStride);
const ibCacheKey = 'InterleavedBuffer:' + accessorDef.bufferView + ':' + accessorDef
.componentType + ':' + ibSlice + ':' + accessorDef.count;
let ib = parser.cache.get(ibCacheKey);
if (!ib) {
array = new TypedArray(bufferView, ibSlice * byteStride, accessorDef.count *
byteStride / elementBytes);
// Integer parameters to IB/IBA are in array elements, not bytes.
ib = new three.InterleavedBuffer(array, byteStride / elementBytes);
parser.cache.add(ibCacheKey, ib);
}
bufferAttribute = new three.InterleavedBufferAttribute(ib, itemSize, (byteOffset %
byteStride) / elementBytes, normalized);
} else {
if (bufferView === null) {
array = new TypedArray(accessorDef.count * itemSize);
} else {
array = new TypedArray(bufferView, byteOffset, accessorDef.count * itemSize);
}
bufferAttribute = new three.BufferAttribute(array, itemSize, normalized);
}
// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#sparse-accessors
if (accessorDef.sparse !== undefined) {
const itemSizeIndices = WEBGL_TYPE_SIZES.SCALAR;
const TypedArrayIndices = WEBGL_COMPONENT_TYPES[accessorDef.sparse.indices.componentType];
const byteOffsetIndices = accessorDef.sparse.indices.byteOffset || 0;
const byteOffsetValues = accessorDef.sparse.values.byteOffset || 0;
const sparseIndices = new TypedArrayIndices(bufferViews[1], byteOffsetIndices, accessorDef
.sparse.count * itemSizeIndices);
const sparseValues = new TypedArray(bufferViews[2], byteOffsetValues, accessorDef.sparse
.count * itemSize);
if (bufferView !== null) {
// Avoid modifying the original ArrayBuffer, if the bufferView wasn't initialized with zeroes.
bufferAttribute = new three.BufferAttribute(bufferAttribute.array.slice(),
bufferAttribute.itemSize, bufferAttribute.normalized);
}
for (let i = 0, il = sparseIndices.length; i < il; i++) {
const index = sparseIndices[i];
bufferAttribute.setX(index, sparseValues[i * itemSize]);
if (itemSize >= 2) bufferAttribute.setY(index, sparseValues[i * itemSize + 1]);
if (itemSize >= 3) bufferAttribute.setZ(index, sparseValues[i * itemSize + 2]);
if (itemSize >= 4) bufferAttribute.setW(index, sparseValues[i * itemSize + 3]);
if (itemSize >= 5) throw new Error(
'THREE.GLTFLoader: Unsupported itemSize in sparse BufferAttribute.');
}
}
return bufferAttribute;
});
}
/**
* Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#textures
* @param {number} textureIndex
* @return {Promise<THREE.Texture>}
*/
loadTexture(textureIndex) {
const json = this.json;
const options = this.options;
const textureDef = json.textures[textureIndex];
const source = json.images[textureDef.source];
let loader = this.textureLoader;
if (source.uri) {
const handler = options.manager.getHandler(source.uri);
if (handler !== null) loader = handler;
}
return this.loadTextureImage(textureIndex, source, loader);
}
loadTextureImage(textureIndex, source, loader) {
const parser = this;
const json = this.json;
const options = this.options;
const textureDef = json.textures[textureIndex];
const cacheKey = (source.uri || source.bufferView) + ':' + textureDef.sampler;
if (this.textureCache[cacheKey]) {
// See https://github.com/mrdoob/three.js/issues/21559.
return this.textureCache[cacheKey];
}
const URL = three.PlatformManager.polyfill.URL || self.webkitURL;
let sourceURI = source.uri || '';
let isObjectURL = false;
if (source.bufferView !== undefined) {
// Load binary image data from bufferView, if provided.
sourceURI = parser.getDependency('bufferView', source.bufferView).then(function(bufferView) {
isObjectURL = true;
const blob = new three.PlatformManager.polyfill.Blob([bufferView], {
type: source.mimeType
});
sourceURI = URL.createObjectURL(blob);
return sourceURI;
});
} else if (source.uri === undefined) {
throw new Error('THREE.GLTFLoader: Image ' + textureIndex + ' is missing URI and bufferView');
}
const promise = Promise.resolve(sourceURI).then(function(sourceURI) {
return new Promise(function(resolve, reject) {
let onLoad = resolve;
if (loader.isImageBitmapLoader === true) {
onLoad = function(imageBitmap) {
const texture = new three.Texture(imageBitmap);
texture.needsUpdate = true;
resolve(texture);
};
}
loader.load(resolveURL(sourceURI, options.path), onLoad, undefined, reject);
});
}).then(function(texture) {
// Clean up resources and configure Texture.
if (isObjectURL === true) {
URL.revokeObjectURL(sourceURI);
}
texture.flipY = false;
if (textureDef.name) texture.name = textureDef.name;
const samplers = json.samplers || {};
const sampler = samplers[textureDef.sampler] || {};
texture.magFilter = WEBGL_FILTERS[sampler.magFilter] || three.LinearFilter;
texture.minFilter = WEBGL_FILTERS[sampler.minFilter] || three.LinearMipmapLinearFilter;
texture.wrapS = WEBGL_WRAPPINGS[sampler.wrapS] || three.RepeatWrapping;
texture.wrapT = WEBGL_WRAPPINGS[sampler.wrapT] || three.RepeatWrapping;
parser.associations.set(texture, {
textures: textureIndex
});
return texture;
}).catch(function() {
console.error('THREE.GLTFLoader: Couldn\'t load texture', sourceURI);
return null;
});
this.textureCache[cacheKey] = promise;
return promise;
}
/**
* Asynchronously assigns a texture to the given material parameters.
* @param {Object} materialParams
* @param {string} mapName
* @param {Object} mapDef
* @return {Promise<Texture>}
*/
assignTexture(materialParams, mapName, mapDef) {
const parser = this;
return this.getDependency('texture', mapDef.index).then(function(texture) {
// Materials sample aoMap from UV set 1 and other maps from UV set 0 - this can't be configured
// However, we will copy UV set 0 to UV set 1 on demand for aoMap
if (mapDef.texCoord !== undefined && mapDef.texCoord != 0 && !(mapName === 'aoMap' && mapDef
.texCoord == 1)) {
console.warn('THREE.GLTFLoader: Custom UV set ' + mapDef.texCoord + ' for texture ' +
mapName + ' not yet supported.');
}
if (parser.extensions[EXTENSIONS.KHR_TEXTURE_TRANSFORM]) {
const transform = mapDef.extensions !== undefined ? mapDef.extensions[EXTENSIONS
.KHR_TEXTURE_TRANSFORM] : undefined;
if (transform) {
const gltfReference = parser.associations.get(texture);
texture = parser.extensions[EXTENSIONS.KHR_TEXTURE_TRANSFORM].extendTexture(texture,
transform);
parser.associations.set(texture, gltfReference);
}
}
materialParams[mapName] = texture;
return texture;
});
}
/**
* Assigns final material to a Mesh, Line, or Points instance. The instance
* already has a material (generated from the glTF material options alone)
* but reuse of the same glTF material may require multiple threejs materials
* to accommodate different primitive types, defines, etc. New materials will
* be created if necessary, and reused from a cache.
* @param {Object3D} mesh Mesh, Line, or Points instance.
*/
assignFinalMaterial(mesh) {
const geometry = mesh.geometry;
let material = mesh.material;
const useDerivativeTangents = geometry.attributes.tangent === undefined;
const useVertexColors = geometry.attributes.color !== undefined;
const useFlatShading = geometry.attributes.normal === undefined;
if (mesh.isPoints) {
const cacheKey = 'PointsMaterial:' + material.uuid;
let pointsMaterial = this.cache.get(cacheKey);
if (!pointsMaterial) {
pointsMaterial = new three.PointsMaterial();
three.Material.prototype.copy.call(pointsMaterial, material);
pointsMaterial.color.copy(material.color);
pointsMaterial.map = material.map;
pointsMaterial.sizeAttenuation = false; // glTF spec says points should be 1px
this.cache.add(cacheKey, pointsMaterial);
}
material = pointsMaterial;
} else if (mesh.isLine) {
const cacheKey = 'LineBasicMaterial:' + material.uuid;
let lineMaterial = this.cache.get(cacheKey);
if (!lineMaterial) {
lineMaterial = new three.LineBasicMaterial();
three.Material.prototype.copy.call(lineMaterial, material);
lineMaterial.color.copy(material.color);
this.cache.add(cacheKey, lineMaterial);
}
material = lineMaterial;
}
// Clone the material if it will be modified
if (useDerivativeTangents || useVertexColors || useFlatShading) {
let cacheKey = 'ClonedMaterial:' + material.uuid + ':';
if (material.isGLTFSpecularGlossinessMaterial) cacheKey += 'specular-glossiness:';
if (useDerivativeTangents) cacheKey += 'derivative-tangents:';
if (useVertexColors) cacheKey += 'vertex-colors:';
if (useFlatShading) cacheKey += 'flat-shading:';
let cachedMaterial = this.cache.get(cacheKey);
if (!cachedMaterial) {
cachedMaterial = material.clone();
if (useVertexColors) cachedMaterial.vertexColors = true;
if (useFlatShading) cachedMaterial.flatShading = true;
if (useDerivativeTangents) {
// https://github.com/mrdoob/three.js/issues/11438#issuecomment-507003995
if (cachedMaterial.normalScale) cachedMaterial.normalScale.y *= -1;
if (cachedMaterial.clearcoatNormalScale) cachedMaterial.clearcoatNormalScale.y *= -1;
}
this.cache.add(cacheKey, cachedMaterial);
this.associations.set(cachedMaterial, this.associations.get(material));
}
material = cachedMaterial;
}
// workarounds for mesh and geometry
if (material.aoMap && geometry.attributes.uv2 === undefined && geometry.attributes.uv !== undefined) {
geometry.setAttribute('uv2', geometry.attributes.uv);
}
mesh.material = material;
}
getMaterialType( /* materialIndex */ ) {
return three.MeshStandardMaterial;
}
/**
* Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#materials
* @param {number} materialIndex
* @return {Promise<Material>}
*/
loadMaterial(materialIndex) {
const parser = this;
const json = this.json;
const extensions = this.extensions;
const materialDef = json.materials[materialIndex];
let materialType;
const materialParams = {};
const materialExtensions = materialDef.extensions || {};
const pending = [];
if (materialExtensions[EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS]) {
const sgExtension = extensions[EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS];
materialType = sgExtension.getMaterialType();
pending.push(sgExtension.extendParams(materialParams, materialDef, parser));
} else if (materialExtensions[EXTENSIONS.KHR_MATERIALS_UNLIT]) {
const kmuExtension = extensions[EXTENSIONS.KHR_MATERIALS_UNLIT];
materialType = kmuExtension.getMaterialType();
pending.push(kmuExtension.extendParams(materialParams, materialDef, parser));
} else {
// Specification:
// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#metallic-roughness-material
const metallicRoughness = materialDef.pbrMetallicRoughness || {};
materialParams.color = new three.Color(1.0, 1.0, 1.0);
materialParams.opacity = 1.0;
if (Array.isArray(metallicRoughness.baseColorFactor)) {
const array = metallicRoughness.baseColorFactor;
materialParams.color.fromArray(array);
materialParams.opacity = array[3];
}
if (metallicRoughness.baseColorTexture !== undefined) {
pending.push(parser.assignTexture(materialParams, 'map', metallicRoughness.baseColorTexture));
}
materialParams.metalness = metallicRoughness.metallicFactor !== undefined ? metallicRoughness
.metallicFactor : 1.0;
materialParams.roughness = metallicRoughness.roughnessFactor !== undefined ? metallicRoughness
.roughnessFactor : 1.0;
if (metallicRoughness.metallicRoughnessTexture !== undefined) {
pending.push(parser.assignTexture(materialParams, 'metalnessMap', metallicRoughness
.metallicRoughnessTexture));
pending.push(parser.assignTexture(materialParams, 'roughnessMap', metallicRoughness
.metallicRoughnessTexture));
}
materialType = this._invokeOne(function(ext) {
return ext.getMaterialType && ext.getMaterialType(materialIndex);
});
pending.push(Promise.all(this._invokeAll(function(ext) {
return ext.extendMaterialParams && ext.extendMaterialParams(materialIndex,
materialParams);
})));
}
if (materialDef.doubleSided === true) {
materialParams.side = three.DoubleSide;
}
const alphaMode = materialDef.alphaMode || ALPHA_MODES.OPAQUE;
if (alphaMode === ALPHA_MODES.BLEND) {
materialParams.transparent = true;
// See: https://github.com/mrdoob/three.js/issues/17706
materialParams.depthWrite = false;
} else {
materialParams.format = three.RGBFormat;
materialParams.transparent = false;
if (alphaMode === ALPHA_MODES.MASK) {
materialParams.alphaTest = materialDef.alphaCutoff !== undefined ? materialDef.alphaCutoff : 0.5;
}
}
if (materialDef.normalTexture !== undefined && materialType !== three.MeshBasicMaterial) {
pending.push(parser.assignTexture(materialParams, 'normalMap', materialDef.normalTexture));
materialParams.normalScale = new three.Vector2(1, 1);
if (materialDef.normalTexture.scale !== undefined) {
const scale = materialDef.normalTexture.scale;
materialParams.normalScale.set(scale, scale);
}
}
if (materialDef.occlusionTexture !== undefined && materialType !== three.MeshBasicMaterial) {
pending.push(parser.assignTexture(materialParams, 'aoMap', materialDef.occlusionTexture));
if (materialDef.occlusionTexture.strength !== undefined) {
materialParams.aoMapIntensity = materialDef.occlusionTexture.strength;
}
}
if (materialDef.emissiveFactor !== undefined && materialType !== three.MeshBasicMaterial) {
materialParams.emissive = new three.Color().fromArray(materialDef.emissiveFactor);
}
if (materialDef.emissiveTexture !== undefined && materialType !== three.MeshBasicMaterial) {
pending.push(parser.assignTexture(materialParams, 'emissiveMap', materialDef.emissiveTexture));
}
return Promise.all(pending).then(function() {
let material;
if (materialType === GLTFMeshStandardSGMaterial) {
material = extensions[EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS].createMaterial(
materialParams);
} else {
material = new materialType(materialParams);
}
if (materialDef.name) material.name = materialDef.name;
// baseColorTexture, emissiveTexture, and specularGlossinessTexture use sRGB encoding.
if (material.map) material.map.encoding = three.sRGBEncoding;
if (material.emissiveMap) material.emissiveMap.encoding = three.sRGBEncoding;
assignExtrasToUserData(material, materialDef);
parser.associations.set(material, {
materials: materialIndex
});
if (materialDef.extensions) addUnknownExtensionsToUserData(extensions, material, materialDef);
return material;
});
}
/** When Object3D instances are targeted by animation, they need unique names. */
createUniqueName(originalName) {
const sanitizedName = three.PropertyBinding.sanitizeNodeName(originalName || '');
let name = sanitizedName;
for (let i = 1; this.nodeNamesUsed[name]; ++i) {
name = sanitizedName + '_' + i;
}
this.nodeNamesUsed[name] = true;
return name;
}
/**
* Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#geometry
*
* Creates BufferGeometries from primitives.
*
* @param {Array<GLTF.Primitive>} primitives
* @return {Promise<Array<BufferGeometry>>}
*/
loadGeometries(primitives) {
const parser = this;
const extensions = this.extensions;
const cache = this.primitiveCache;
function createDracoPrimitive(primitive) {
return extensions[EXTENSIONS.KHR_DRACO_MESH_COMPRESSION]
.decodePrimitive(primitive, parser)
.then(function(geometry) {
return addPrimitiveAttributes(geometry, primitive, parser);
});
}
const pending = [];
for (let i = 0, il = primitives.length; i < il; i++) {
const primitive = primitives[i];
const cacheKey = createPrimitiveKey(primitive);
// See if we've already created this geometry
const cached = cache[cacheKey];
if (cached) {
// Use the cached geometry if it exists
pending.push(cached.promise);
} else {
let geometryPromise;
if (primitive.extensions && primitive.extensions[EXTENSIONS.KHR_DRACO_MESH_COMPRESSION]) {
// Use DRACO geometry if available
geometryPromise = createDracoPrimitive(primitive);
} else {
// Otherwise create a new geometry
geometryPromise = addPrimitiveAttributes(new three.BufferGeometry(), primitive, parser);
}
// Cache this geometry
cache[cacheKey] = {
primitive: primitive,
promise: geometryPromise
};
pending.push(geometryPromise);
}
}
return Promise.all(pending);
}
/**
* Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#meshes
* @param {number} meshIndex
* @return {Promise<Group|Mesh|SkinnedMesh>}
*/
loadMesh(meshIndex) {
const parser = this;
const json = this.json;
const extensions = this.extensions;
const meshDef = json.meshes[meshIndex];
const primitives = meshDef.primitives;
const pending = [];
for (let i = 0, il = primitives.length; i < il; i++) {
const material = primitives[i].material === undefined ?
createDefaultMaterial(this.cache) :
this.getDependency('material', primitives[i].material);
pending.push(material);
}
pending.push(parser.loadGeometries(primitives));
return Promise.all(pending).then(function(results) {
const materials = results.slice(0, results.length - 1);
const geometries = results[results.length - 1];
const meshes = [];
for (let i = 0, il = geometries.length; i < il; i++) {
const geometry = geometries[i];
const primitive = primitives[i];
// 1. create Mesh
let mesh;
const material = materials[i];
if (primitive.mode === WEBGL_CONSTANTS.TRIANGLES ||
primitive.mode === WEBGL_CONSTANTS.TRIANGLE_STRIP ||
primitive.mode === WEBGL_CONSTANTS.TRIANGLE_FAN ||
primitive.mode === undefined) {
// .isSkinnedMesh isn't in glTF spec. See ._markDefs()
mesh = meshDef.isSkinnedMesh === true ?
new three.SkinnedMesh(geometry, material) :
new three.Mesh(geometry, material);
if (mesh.isSkinnedMesh === true && !mesh.geometry.attributes.skinWeight.normalized) {
// we normalize floating point skin weight array to fix malformed assets (see #15319)
// it's important to skip this for non-float32 data since normalizeSkinWeights assumes non-normalized inputs
mesh.normalizeSkinWeights();
}
if (primitive.mode === WEBGL_CONSTANTS.TRIANGLE_STRIP) {
mesh.geometry = toTrianglesDrawMode(mesh.geometry, three.TriangleStripDrawMode);
} else if (primitive.mode === WEBGL_CONSTANTS.TRIANGLE_FAN) {
mesh.geometry = toTrianglesDrawMode(mesh.geometry, three.TriangleFanDrawMode);
}
} else if (primitive.mode === WEBGL_CONSTANTS.LINES) {
mesh = new three.LineSegments(geometry, material);
} else if (primitive.mode === WEBGL_CONSTANTS.LINE_STRIP) {
mesh = new three.Line(geometry, material);
} else if (primitive.mode === WEBGL_CONSTANTS.LINE_LOOP) {
mesh = new three.LineLoop(geometry, material);
} else if (primitive.mode === WEBGL_CONSTANTS.POINTS) {
mesh = new three.Points(geometry, material);
} else {
throw new Error('THREE.GLTFLoader: Primitive mode unsupported: ' + primitive.mode);
}
if (Object.keys(mesh.geometry.morphAttributes).length > 0) {
updateMorphTargets(mesh, meshDef);
}
mesh.name = parser.createUniqueName(meshDef.name || ('mesh_' + meshIndex));
assignExtrasToUserData(mesh, meshDef);
if (primitive.extensions) addUnknownExtensionsToUserData(extensions, mesh, primitive);
parser.assignFinalMaterial(mesh);
meshes.push(mesh);
}
for (let i = 0, il = meshes.length; i < il; i++) {
parser.associations.set(meshes[i], {
meshes: meshIndex,
primitives: i
});
}
if (meshes.length === 1) {
return meshes[0];
}
const group = new three.Group();
parser.associations.set(group, {
meshes: meshIndex
});
for (let i = 0, il = meshes.length; i < il; i++) {
group.add(meshes[i]);
}
return group;
});
}
/**
* Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#cameras
* @param {number} cameraIndex
* @return {Promise<THREE.Camera>}
*/
loadCamera(cameraIndex) {
let camera;
const cameraDef = this.json.cameras[cameraIndex];
const params = cameraDef[cameraDef.type];
if (!params) {
console.warn('THREE.GLTFLoader: Missing camera parameters.');
return;
}
if (cameraDef.type === 'perspective') {
camera = new three.PerspectiveCamera(three.MathUtils.radToDeg(params.yfov), params.aspectRatio || 1,
params.znear || 1, params.zfar || 2e6);
} else if (cameraDef.type === 'orthographic') {
camera = new three.OrthographicCamera(-params.xmag, params.xmag, params.ymag, -params.ymag, params
.znear, params.zfar);
}
if (cameraDef.name) camera.name = this.createUniqueName(cameraDef.name);
assignExtrasToUserData(camera, cameraDef);
return Promise.resolve(camera);
}
/**
* Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins
* @param {number} skinIndex
* @return {Promise<Object>}
*/
loadSkin(skinIndex) {
const skinDef = this.json.skins[skinIndex];
const skinEntry = {
joints: skinDef.joints
};
if (skinDef.inverseBindMatrices === undefined) {
return Promise.resolve(skinEntry);
}
return this.getDependency('accessor', skinDef.inverseBindMatrices).then(function(accessor) {
skinEntry.inverseBindMatrices = accessor;
return skinEntry;
});
}
/**
* Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#animations
* @param {number} animationIndex
* @return {Promise<AnimationClip>}
*/
loadAnimation(animationIndex) {
const json = this.json;
const animationDef = json.animations[animationIndex];
const pendingNodes = [];
const pendingInputAccessors = [];
const pendingOutputAccessors = [];
const pendingSamplers = [];
const pendingTargets = [];
for (let i = 0, il = animationDef.channels.length; i < il; i++) {
const channel = animationDef.channels[i];
const sampler = animationDef.samplers[channel.sampler];
const target = channel.target;
const name = target.node !== undefined ? target.node : target.id; // NOTE: target.id is deprecated.
const input = animationDef.parameters !== undefined ? animationDef.parameters[sampler.input] : sampler
.input;
const output = animationDef.parameters !== undefined ? animationDef.parameters[sampler.output] :
sampler.output;
pendingNodes.push(this.getDependency('node', name));
pendingInputAccessors.push(this.getDependency('accessor', input));
pendingOutputAccessors.push(this.getDependency('accessor', output));
pendingSamplers.push(sampler);
pendingTargets.push(target);
}
return Promise.all([
Promise.all(pendingNodes),
Promise.all(pendingInputAccessors),
Promise.all(pendingOutputAccessors),
Promise.all(pendingSamplers),
Promise.all(pendingTargets)
]).then(function(dependencies) {
const nodes = dependencies[0];
const inputAccessors = dependencies[1];
const outputAccessors = dependencies[2];
const samplers = dependencies[3];
const targets = dependencies[4];
const tracks = [];
for (let i = 0, il = nodes.length; i < il; i++) {
const node = nodes[i];
const inputAccessor = inputAccessors[i];
const outputAccessor = outputAccessors[i];
const sampler = samplers[i];
const target = targets[i];
if (node === undefined) continue;
node.updateMatrix();
node.matrixAutoUpdate = true;
let TypedKeyframeTrack;
switch (PATH_PROPERTIES[target.path]) {
case PATH_PROPERTIES.weights:
TypedKeyframeTrack = three.NumberKeyframeTrack;
break;
case PATH_PROPERTIES.rotation:
TypedKeyframeTrack = three.QuaternionKeyframeTrack;
break;
case PATH_PROPERTIES.position:
case PATH_PROPERTIES.scale:
default:
TypedKeyframeTrack = three.VectorKeyframeTrack;
break;
}
const targetName = node.name ? node.name : node.uuid;
const interpolation = sampler.interpolation !== undefined ? INTERPOLATION[sampler
.interpolation] : three.InterpolateLinear;
const targetNames = [];
if (PATH_PROPERTIES[target.path] === PATH_PROPERTIES.weights) {
// Node may be a Group (glTF mesh with several primitives) or a Mesh.
node.traverse(function(object) {
if (object.isMesh === true && object.morphTargetInfluences) {
targetNames.push(object.name ? object.name : object.uuid);
}
});
} else {
targetNames.push(targetName);
}
let outputArray = outputAccessor.array;
if (outputAccessor.normalized) {
const scale = getNormalizedComponentScale(outputArray.constructor);
const scaled = new Float32Array(outputArray.length);
for (let j = 0, jl = outputArray.length; j < jl; j++) {
scaled[j] = outputArray[j] * scale;
}
outputArray = scaled;
}
for (let j = 0, jl = targetNames.length; j < jl; j++) {
const track = new TypedKeyframeTrack(
targetNames[j] + '.' + PATH_PROPERTIES[target.path],
inputAccessor.array,
outputArray,
interpolation
);
// Override interpolation with custom factory method.
if (sampler.interpolation === 'CUBICSPLINE') {
track.createInterpolant = function InterpolantFactoryMethodGLTFCubicSpline(
result) {
// A CUBICSPLINE keyframe in glTF has three output values for each input value,
// representing inTangent, splineVertex, and outTangent. As a result, track.getValueSize()
// must be divided by three to get the interpolant's sampleSize argument.
const interpolantType = (this instanceof three.QuaternionKeyframeTrack) ?
GLTFCubicSplineQuaternionInterpolant : GLTFCubicSplineInterpolant;
return new interpolantType(this.times, this.values, this.getValueSize() /
3, result);
};
// Mark as CUBICSPLINE. `track.getInterpolation()` doesn't support custom interpolants.
track.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline = true;
}
tracks.push(track);
}
}
const name = animationDef.name ? animationDef.name : 'animation_' + animationIndex;
return new three.AnimationClip(name, undefined, tracks);
});
}
createNodeMesh(nodeIndex) {
const json = this.json;
const parser = this;
const nodeDef = json.nodes[nodeIndex];
if (nodeDef.mesh === undefined) return null;
return parser.getDependency('mesh', nodeDef.mesh).then(function(mesh) {
const node = parser._getNodeRef(parser.meshCache, nodeDef.mesh, mesh);
// if weights are provided on the node, override weights on the mesh.
if (nodeDef.weights !== undefined) {
node.traverse(function(o) {
if (!o.isMesh) return;
for (let i = 0, il = nodeDef.weights.length; i < il; i++) {
o.morphTargetInfluences[i] = nodeDef.weights[i];
}
});
}
return node;
});
}
/**
* Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#nodes-and-hierarchy
* @param {number} nodeIndex
* @return {Promise<Object3D>}
*/
loadNode(nodeIndex) {
const json = this.json;
const extensions = this.extensions;
const parser = this;
const nodeDef = json.nodes[nodeIndex];
// reserve node's name before its dependencies, so the root has the intended name.
const nodeName = nodeDef.name ? parser.createUniqueName(nodeDef.name) : '';
return (function() {
const pending = [];
const meshPromise = parser._invokeOne(function(ext) {
return ext.createNodeMesh && ext.createNodeMesh(nodeIndex);
});
if (meshPromise) {
pending.push(meshPromise);
}
if (nodeDef.camera !== undefined) {
pending.push(parser.getDependency('camera', nodeDef.camera).then(function(camera) {
return parser._getNodeRef(parser.cameraCache, nodeDef.camera, camera);
}));
}
parser._invokeAll(function(ext) {
return ext.createNodeAttachment && ext.createNodeAttachment(nodeIndex);
}).forEach(function(promise) {
pending.push(promise);
});
return Promise.all(pending);
}()).then(function(objects) {
let node;
// .isBone isn't in glTF spec. See ._markDefs
if (nodeDef.isBone === true) {
node = new three.Bone();
} else if (objects.length > 1) {
node = new three.Group();
} else if (objects.length === 1) {
node = objects[0];
} else {
node = new three.Object3D();
}
if (node !== objects[0]) {
for (let i = 0, il = objects.length; i < il; i++) {
node.add(objects[i]);
}
}
if (nodeDef.name) {
node.userData.name = nodeDef.name;
node.name = nodeName;
}
assignExtrasToUserData(node, nodeDef);
if (nodeDef.extensions) addUnknownExtensionsToUserData(extensions, node, nodeDef);
if (nodeDef.matrix !== undefined) {
const matrix = new three.Matrix4();
matrix.fromArray(nodeDef.matrix);
node.applyMatrix4(matrix);
} else {
if (nodeDef.translation !== undefined) {
node.position.fromArray(nodeDef.translation);
}
if (nodeDef.rotation !== undefined) {
node.quaternion.fromArray(nodeDef.rotation);
}
if (nodeDef.scale !== undefined) {
node.scale.fromArray(nodeDef.scale);
}
}
if (!parser.associations.has(node)) {
parser.associations.set(node, {});
}
parser.associations.get(node).nodes = nodeIndex;
return node;
});
}
/**
* Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#scenes
* @param {number} sceneIndex
* @return {Promise<Group>}
*/
loadScene(sceneIndex) {
const json = this.json;
const extensions = this.extensions;
const sceneDef = this.json.scenes[sceneIndex];
const parser = this;
// Loader returns Group, not Scene.
// See: https://github.com/mrdoob/three.js/issues/18342#issuecomment-578981172
const scene = new three.Group();
if (sceneDef.name) scene.name = parser.createUniqueName(sceneDef.name);
assignExtrasToUserData(scene, sceneDef);
if (sceneDef.extensions) addUnknownExtensionsToUserData(extensions, scene, sceneDef);
const nodeIds = sceneDef.nodes || [];
const pending = [];
for (let i = 0, il = nodeIds.length; i < il; i++) {
pending.push(buildNodeHierarchy(nodeIds[i], scene, json, parser));
}
return Promise.all(pending).then(function() {
// Removes dangling associations, associations that reference a node that
// didn't make it into the scene.
const reduceAssociations = (node) => {
const reducedAssociations = new Map();
for (const [key, value] of parser.associations) {
if (key instanceof three.Material || key instanceof three.Texture) {
reducedAssociations.set(key, value);
}
}
node.traverse((node) => {
const mappings = parser.associations.get(node);
if (mappings != null) {
reducedAssociations.set(node, mappings);
}
});
return reducedAssociations;
};
parser.associations = reduceAssociations(scene);
return scene;
});
}
}
function buildNodeHierarchy(nodeId, parentObject, json, parser) {
const nodeDef = json.nodes[nodeId];
return parser.getDependency('node', nodeId).then(function(node) {
if (nodeDef.skin === undefined) return node;
// build skeleton here as well
let skinEntry;
return parser.getDependency('skin', nodeDef.skin).then(function(skin) {
skinEntry = skin;
const pendingJoints = [];
for (let i = 0, il = skinEntry.joints.length; i < il; i++) {
pendingJoints.push(parser.getDependency('node', skinEntry.joints[i]));
}
return Promise.all(pendingJoints);
}).then(function(jointNodes) {
node.traverse(function(mesh) {
if (!mesh.isMesh) return;
const bones = [];
const boneInverses = [];
for (let j = 0, jl = jointNodes.length; j < jl; j++) {
const jointNode = jointNodes[j];
if (jointNode) {
bones.push(jointNode);
const mat = new three.Matrix4();
if (skinEntry.inverseBindMatrices !== undefined) {
mat.fromArray(skinEntry.inverseBindMatrices.array, j * 16);
}
boneInverses.push(mat);
} else {
console.warn('THREE.GLTFLoader: Joint "%s" could not be found.',
skinEntry.joints[j]);
}
}
mesh.bind(new three.Skeleton(bones, boneInverses), mesh.matrixWorld);
});
return node;
});
}).then(function(node) {
// build node hierachy
parentObject.add(node);
const pending = [];
if (nodeDef.children) {
const children = nodeDef.children;
for (let i = 0, il = children.length; i < il; i++) {
const child = children[i];
pending.push(buildNodeHierarchy(child, node, json, parser));
}
}
return Promise.all(pending);
});
}
/**
* @param {BufferGeometry} geometry
* @param {GLTF.Primitive} primitiveDef
* @param {GLTFParser} parser
*/
function computeBounds(geometry, primitiveDef, parser) {
const attributes = primitiveDef.attributes;
const box = new three.Box3();
if (attributes.POSITION !== undefined) {
const accessor = parser.json.accessors[attributes.POSITION];
const min = accessor.min;
const max = accessor.max;
// glTF requires 'min' and 'max', but VRM (which extends glTF) currently ignores that requirement.
if (min !== undefined && max !== undefined) {
box.set(
new three.Vector3(min[0], min[1], min[2]),
new three.Vector3(max[0], max[1], max[2])
);
if (accessor.normalized) {
const boxScale = getNormalizedComponentScale(WEBGL_COMPONENT_TYPES[accessor.componentType]);
box.min.multiplyScalar(boxScale);
box.max.multiplyScalar(boxScale);
}
} else {
console.warn('THREE.GLTFLoader: Missing min/max properties for accessor POSITION.');
return;
}
} else {
return;
}
const targets = primitiveDef.targets;
if (targets !== undefined) {
const maxDisplacement = new three.Vector3();
const vector = new three.Vector3();
for (let i = 0, il = targets.length; i < il; i++) {
const target = targets[i];
if (target.POSITION !== undefined) {
const accessor = parser.json.accessors[target.POSITION];
const min = accessor.min;
const max = accessor.max;
// glTF requires 'min' and 'max', but VRM (which extends glTF) currently ignores that requirement.
if (min !== undefined && max !== undefined) {
// we need to get max of absolute components because target weight is [-1,1]
vector.setX(Math.max(Math.abs(min[0]), Math.abs(max[0])));
vector.setY(Math.max(Math.abs(min[1]), Math.abs(max[1])));
vector.setZ(Math.max(Math.abs(min[2]), Math.abs(max[2])));
if (accessor.normalized) {
const boxScale = getNormalizedComponentScale(WEBGL_COMPONENT_TYPES[accessor.componentType]);
vector.multiplyScalar(boxScale);
}
// Note: this assumes that the sum of all weights is at most 1. This isn't quite correct - it's more conservative
// to assume that each target can have a max weight of 1. However, for some use cases - notably, when morph targets
// are used to implement key-frame animations and as such only two are active at a time - this results in very large
// boxes. So for now we make a box that's sometimes a touch too small but is hopefully mostly of reasonable size.
maxDisplacement.max(vector);
} else {
console.warn('THREE.GLTFLoader: Missing min/max properties for accessor POSITION.');
}
}
}
// As per comment above this box isn't conservative, but has a reasonable size for a very large number of morph targets.
box.expandByVector(maxDisplacement);
}
geometry.boundingBox = box;
const sphere = new three.Sphere();
box.getCenter(sphere.center);
sphere.radius = box.min.distanceTo(box.max) / 2;
geometry.boundingSphere = sphere;
}
/**
* @param {BufferGeometry} geometry
* @param {GLTF.Primitive} primitiveDef
* @param {GLTFParser} parser
* @return {Promise<BufferGeometry>}
*/
function addPrimitiveAttributes(geometry, primitiveDef, parser) {
const attributes = primitiveDef.attributes;
const pending = [];
function assignAttributeAccessor(accessorIndex, attributeName) {
return parser.getDependency('accessor', accessorIndex)
.then(function(accessor) {
geometry.setAttribute(attributeName, accessor);
});
}
for (const gltfAttributeName in attributes) {
const threeAttributeName = ATTRIBUTES[gltfAttributeName] || gltfAttributeName.toLowerCase();
// Skip attributes already provided by e.g. Draco extension.
if (threeAttributeName in geometry.attributes) continue;
pending.push(assignAttributeAccessor(attributes[gltfAttributeName], threeAttributeName));
}
if (primitiveDef.indices !== undefined && !geometry.index) {
const accessor = parser.getDependency('accessor', primitiveDef.indices).then(function(accessor) {
geometry.setIndex(accessor);
});
pending.push(accessor);
}
assignExtrasToUserData(geometry, primitiveDef);
computeBounds(geometry, primitiveDef, parser);
return Promise.all(pending).then(function() {
return primitiveDef.targets !== undefined ?
addMorphTargets(geometry, primitiveDef.targets, parser) :
geometry;
});
}
/**
* @param {BufferGeometry} geometry
* @param {Number} drawMode
* @return {BufferGeometry}
*/
function toTrianglesDrawMode(geometry, drawMode) {
let index = geometry.getIndex();
// generate index if not present
if (index === null) {
const indices = [];
const position = geometry.getAttribute('position');
if (position !== undefined) {
for (let i = 0; i < position.count; i++) {
indices.push(i);
}
geometry.setIndex(indices);
index = geometry.getIndex();
} else {
console.error(
'THREE.GLTFLoader.toTrianglesDrawMode(): Undefined position attribute. Processing not possible.');
return geometry;
}
}
//
const numberOfTriangles = index.count - 2;
const newIndices = [];
if (drawMode === three.TriangleFanDrawMode) {
// gl.TRIANGLE_FAN
for (let i = 1; i <= numberOfTriangles; i++) {
newIndices.push(index.getX(0));
newIndices.push(index.getX(i));
newIndices.push(index.getX(i + 1));
}
} else {
// gl.TRIANGLE_STRIP
for (let i = 0; i < numberOfTriangles; i++) {
if (i % 2 === 0) {
newIndices.push(index.getX(i));
newIndices.push(index.getX(i + 1));
newIndices.push(index.getX(i + 2));
} else {
newIndices.push(index.getX(i + 2));
newIndices.push(index.getX(i + 1));
newIndices.push(index.getX(i));
}
}
}
if ((newIndices.length / 3) !== numberOfTriangles) {
console.error('THREE.GLTFLoader.toTrianglesDrawMode(): Unable to generate correct amount of triangles.');
}
// build final geometry
const newGeometry = geometry.clone();
newGeometry.setIndex(newIndices);
return newGeometry;
}
// This set of controls performs orbiting, dollying (zooming), and panning.
// Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
//
// Orbit - left mouse / touch: one-finger move
// Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
// Pan - right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move
const _changeEvent = {
type: 'change'
};
const _startEvent = {
type: 'start'
};
const _endEvent = {
type: 'end'
};
class OrbitControls extends three.EventDispatcher {
constructor(object, domElement) {
super();
if (domElement === undefined) console.warn(
'THREE.OrbitControls: The second parameter "domElement" is now mandatory.');
if (domElement === three.PlatformManager.polyfill.document) console.error(
'THREE.OrbitControls: "document" should not be used as the target "domElement". Please use "renderer.domElement" instead.'
);
this.object = object;
this.domElement = domElement;
this.domElement.style.touchAction = 'none'; // disable touch scroll
// Set to false to disable this control
this.enabled = true;
// "target" sets the location of focus, where the object orbits around
this.target = new three.Vector3();
// How far you can dolly in and out ( PerspectiveCamera only )
this.minDistance = 0;
this.maxDistance = Infinity;
// How far you can zoom in and out ( OrthographicCamera only )
this.minZoom = 0;
this.maxZoom = Infinity;
// How far you can orbit vertically, upper and lower limits.
// Range is 0 to Math.PI radians.
this.minPolarAngle = 0; // radians
this.maxPolarAngle = Math.PI; // radians
// How far you can orbit horizontally, upper and lower limits.
// If set, the interval [ min, max ] must be a sub-interval of [ - 2 PI, 2 PI ], with ( max - min < 2 PI )
this.minAzimuthAngle = -Infinity; // radians
this.maxAzimuthAngle = Infinity; // radians
// Set to true to enable damping (inertia)
// If damping is enabled, you must call controls.update() in your animation loop
this.enableDamping = false;
this.dampingFactor = 0.05;
// This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
// Set to false to disable zooming
this.enableZoom = true;
this.zoomSpeed = 1.0;
// Set to false to disable rotating
this.enableRotate = true;
this.rotateSpeed = 1.0;
// Set to false to disable panning
this.enablePan = true;
this.panSpeed = 1.0;
this.screenSpacePanning = true; // if false, pan orthogonal to world-space direction camera.up
this.keyPanSpeed = 7.0; // pixels moved per arrow key push
// Set to true to automatically rotate around the target
// If auto-rotate is enabled, you must call controls.update() in your animation loop
this.autoRotate = false;
this.autoRotateSpeed = 2.0; // 30 seconds per orbit when fps is 60
// The four arrow keys
this.keys = {
LEFT: 'ArrowLeft',
UP: 'ArrowUp',
RIGHT: 'ArrowRight',
BOTTOM: 'ArrowDown'
};
// Mouse buttons
this.mouseButtons = {
LEFT: three.MOUSE.ROTATE,
MIDDLE: three.MOUSE.DOLLY,
RIGHT: three.MOUSE.PAN
};
// Touch fingers
this.touches = {
ONE: three.TOUCH.ROTATE,
TWO: three.TOUCH.DOLLY_PAN
};
// for reset
this.target0 = this.target.clone();
this.position0 = this.object.position.clone();
this.zoom0 = this.object.zoom;
// the target DOM element for key events
this._domElementKeyEvents = null;
//
// public methods
//
this.getPolarAngle = function() {
return spherical.phi;
};
this.getAzimuthalAngle = function() {
return spherical.theta;
};
this.getDistance = function() {
return this.object.position.distanceTo(this.target);
};
this.listenToKeyEvents = function(domElement) {
domElement.addEventListener('keydown', onKeyDown);
this._domElementKeyEvents = domElement;
};
this.saveState = function() {
scope.target0.copy(scope.target);
scope.position0.copy(scope.object.position);
scope.zoom0 = scope.object.zoom;
};
this.reset = function() {
scope.target.copy(scope.target0);
scope.object.position.copy(scope.position0);
scope.object.zoom = scope.zoom0;
scope.object.updateProjectionMatrix();
scope.dispatchEvent(_changeEvent);
scope.update();
state = STATE.NONE;
};
// this method is exposed, but perhaps it would be better if we can make it private...
this.update = function() {
const offset = new three.Vector3();
// so camera.up is the orbit axis
const quat = new three.Quaternion().setFromUnitVectors(object.up, new three.Vector3(0, 1, 0));
const quatInverse = quat.clone().invert();
const lastPosition = new three.Vector3();
const lastQuaternion = new three.Quaternion();
const twoPI = 2 * Math.PI;
return function update() {
const position = scope.object.position;
offset.copy(position).sub(scope.target);
// rotate offset to "y-axis-is-up" space
offset.applyQuaternion(quat);
// angle from z-axis around y-axis
spherical.setFromVector3(offset);
if (scope.autoRotate && state === STATE.NONE) {
rotateLeft(getAutoRotationAngle());
}
if (scope.enableDamping) {
spherical.theta += sphericalDelta.theta * scope.dampingFactor;
spherical.phi += sphericalDelta.phi * scope.dampingFactor;
} else {
spherical.theta += sphericalDelta.theta;
spherical.phi += sphericalDelta.phi;
}
// restrict theta to be between desired limits
let min = scope.minAzimuthAngle;
let max = scope.maxAzimuthAngle;
if (isFinite(min) && isFinite(max)) {
if (min < -Math.PI) min += twoPI;
else if (min > Math.PI) min -= twoPI;
if (max < -Math.PI) max += twoPI;
else if (max > Math.PI) max -= twoPI;
if (min <= max) {
spherical.theta = Math.max(min, Math.min(max, spherical.theta));
} else {
spherical.theta = (spherical.theta > (min + max) / 2) ?
Math.max(min, spherical.theta) :
Math.min(max, spherical.theta);
}
}
// restrict phi to be between desired limits
spherical.phi = Math.max(scope.minPolarAngle, Math.min(scope.maxPolarAngle, spherical
.phi));
spherical.makeSafe();
spherical.radius *= scale;
// restrict radius to be between desired limits
spherical.radius = Math.max(scope.minDistance, Math.min(scope.maxDistance, spherical
.radius));
// move target to panned location
if (scope.enableDamping === true) {
scope.target.addScaledVector(panOffset, scope.dampingFactor);
} else {
scope.target.add(panOffset);
}
offset.setFromSpherical(spherical);
// rotate offset back to "camera-up-vector-is-up" space
offset.applyQuaternion(quatInverse);
position.copy(scope.target).add(offset);
scope.object.lookAt(scope.target);
if (scope.enableDamping === true) {
sphericalDelta.theta *= (1 - scope.dampingFactor);
sphericalDelta.phi *= (1 - scope.dampingFactor);
panOffset.multiplyScalar(1 - scope.dampingFactor);
} else {
sphericalDelta.set(0, 0, 0);
panOffset.set(0, 0, 0);
}
scale = 1;
// update condition is:
// min(camera displacement, camera rotation in radians)^2 > EPS
// using small-angle approximation cos(x/2) = 1 - x^2 / 8
if (zoomChanged ||
lastPosition.distanceToSquared(scope.object.position) > EPS ||
8 * (1 - lastQuaternion.dot(scope.object.quaternion)) > EPS) {
scope.dispatchEvent(_changeEvent);
lastPosition.copy(scope.object.position);
lastQuaternion.copy(scope.object.quaternion);
zoomChanged = false;
return true;
}
return false;
};
}();
this.dispose = function() {
scope.domElement.removeEventListener('contextmenu', onContextMenu);
scope.domElement.removeEventListener('pointerdown', onPointerDown);
scope.domElement.removeEventListener('pointercancel', onPointerCancel);
scope.domElement.removeEventListener('wheel', onMouseWheel);
scope.domElement.removeEventListener('pointermove', onPointerMove);
scope.domElement.removeEventListener('pointerup', onPointerUp);
if (scope._domElementKeyEvents !== null) {
scope._domElementKeyEvents.removeEventListener('keydown', onKeyDown);
}
//scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
};
//
// internals
//
const scope = this;
const STATE = {
NONE: -1,
ROTATE: 0,
DOLLY: 1,
PAN: 2,
TOUCH_ROTATE: 3,
TOUCH_PAN: 4,
TOUCH_DOLLY_PAN: 5,
TOUCH_DOLLY_ROTATE: 6
};
let state = STATE.NONE;
const EPS = 0.000001;
// current position in spherical coordinates
const spherical = new three.Spherical();
const sphericalDelta = new three.Spherical();
let scale = 1;
const panOffset = new three.Vector3();
let zoomChanged = false;
const rotateStart = new three.Vector2();
const rotateEnd = new three.Vector2();
const rotateDelta = new three.Vector2();
const panStart = new three.Vector2();
const panEnd = new three.Vector2();
const panDelta = new three.Vector2();
const dollyStart = new three.Vector2();
const dollyEnd = new three.Vector2();
const dollyDelta = new three.Vector2();
const pointers = [];
const pointerPositions = {};
function getAutoRotationAngle() {
return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
}
function getZoomScale() {
return Math.pow(0.95, scope.zoomSpeed);
}
function rotateLeft(angle) {
sphericalDelta.theta -= angle;
}
function rotateUp(angle) {
sphericalDelta.phi -= angle;
}
const panLeft = function() {
const v = new three.Vector3();
return function panLeft(distance, objectMatrix) {
v.setFromMatrixColumn(objectMatrix, 0); // get X column of objectMatrix
v.multiplyScalar(-distance);
panOffset.add(v);
};
}();
const panUp = function() {
const v = new three.Vector3();
return function panUp(distance, objectMatrix) {
if (scope.screenSpacePanning === true) {
v.setFromMatrixColumn(objectMatrix, 1);
} else {
v.setFromMatrixColumn(objectMatrix, 0);
v.crossVectors(scope.object.up, v);
}
v.multiplyScalar(distance);
panOffset.add(v);
};
}();
// deltaX and deltaY are in pixels; right and down are positive
const pan = function() {
const offset = new three.Vector3();
return function pan(deltaX, deltaY) {
const element = scope.domElement;
if (scope.object.isPerspectiveCamera) {
// perspective
const position = scope.object.position;
offset.copy(position).sub(scope.target);
let targetDistance = offset.length();
// half of the fov is center to top of screen
targetDistance *= Math.tan((scope.object.fov / 2) * Math.PI / 180.0);
// we use only clientHeight here so aspect ratio does not distort speed
panLeft(2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix);
panUp(2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix);
} else if (scope.object.isOrthographicCamera) {
// orthographic
panLeft(deltaX * (scope.object.right - scope.object.left) / scope.object.zoom /
element.clientWidth, scope.object.matrix);
panUp(deltaY * (scope.object.top - scope.object.bottom) / scope.object.zoom /
element.clientHeight, scope.object.matrix);
} else {
// camera neither orthographic nor perspective
console.warn(
'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.'
);
scope.enablePan = false;
}
};
}();
function dollyOut(dollyScale) {
if (scope.object.isPerspectiveCamera) {
scale /= dollyScale;
} else if (scope.object.isOrthographicCamera) {
scope.object.zoom = Math.max(scope.minZoom, Math.min(scope.maxZoom, scope.object.zoom *
dollyScale));
scope.object.updateProjectionMatrix();
zoomChanged = true;
} else {
console.warn(
'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.');
scope.enableZoom = false;
}
}
function dollyIn(dollyScale) {
if (scope.object.isPerspectiveCamera) {
scale *= dollyScale;
} else if (scope.object.isOrthographicCamera) {
scope.object.zoom = Math.max(scope.minZoom, Math.min(scope.maxZoom, scope.object.zoom /
dollyScale));
scope.object.updateProjectionMatrix();
zoomChanged = true;
} else {
console.warn(
'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.');
scope.enableZoom = false;
}
}
//
// event callbacks - update the object state
//
function handleMouseDownRotate(event) {
rotateStart.set(event.clientX, event.clientY);
}
function handleMouseDownDolly(event) {
dollyStart.set(event.clientX, event.clientY);
}
function handleMouseDownPan(event) {
panStart.set(event.clientX, event.clientY);
}
function handleMouseMoveRotate(event) {
rotateEnd.set(event.clientX, event.clientY);
rotateDelta.subVectors(rotateEnd, rotateStart).multiplyScalar(scope.rotateSpeed);
const element = scope.domElement;
rotateLeft(2 * Math.PI * rotateDelta.x / element.clientHeight); // yes, height
rotateUp(2 * Math.PI * rotateDelta.y / element.clientHeight);
rotateStart.copy(rotateEnd);
scope.update();
}
function handleMouseMoveDolly(event) {
dollyEnd.set(event.clientX, event.clientY);
dollyDelta.subVectors(dollyEnd, dollyStart);
if (dollyDelta.y > 0) {
dollyOut(getZoomScale());
} else if (dollyDelta.y < 0) {
dollyIn(getZoomScale());
}
dollyStart.copy(dollyEnd);
scope.update();
}
function handleMouseMovePan(event) {
panEnd.set(event.clientX, event.clientY);
panDelta.subVectors(panEnd, panStart).multiplyScalar(scope.panSpeed);
pan(panDelta.x, panDelta.y);
panStart.copy(panEnd);
scope.update();
}
function handleMouseWheel(event) {
if (event.deltaY < 0) {
dollyIn(getZoomScale());
} else if (event.deltaY > 0) {
dollyOut(getZoomScale());
}
scope.update();
}
function handleKeyDown(event) {
let needsUpdate = false;
switch (event.code) {
case scope.keys.UP:
pan(0, scope.keyPanSpeed);
needsUpdate = true;
break;
case scope.keys.BOTTOM:
pan(0, -scope.keyPanSpeed);
needsUpdate = true;
break;
case scope.keys.LEFT:
pan(scope.keyPanSpeed, 0);
needsUpdate = true;
break;
case scope.keys.RIGHT:
pan(-scope.keyPanSpeed, 0);
needsUpdate = true;
break;
}
if (needsUpdate) {
// prevent the browser from scrolling on cursor keys
event.preventDefault();
scope.update();
}
}
function handleTouchStartRotate() {
if (pointers.length === 1) {
rotateStart.set(pointers[0].pageX, pointers[0].pageY);
} else {
const x = 0.5 * (pointers[0].pageX + pointers[1].pageX);
const y = 0.5 * (pointers[0].pageY + pointers[1].pageY);
rotateStart.set(x, y);
}
}
function handleTouchStartPan() {
if (pointers.length === 1) {
panStart.set(pointers[0].pageX, pointers[0].pageY);
} else {
const x = 0.5 * (pointers[0].pageX + pointers[1].pageX);
const y = 0.5 * (pointers[0].pageY + pointers[1].pageY);
panStart.set(x, y);
}
}
function handleTouchStartDolly() {
const dx = pointers[0].pageX - pointers[1].pageX;
const dy = pointers[0].pageY - pointers[1].pageY;
const distance = Math.sqrt(dx * dx + dy * dy);
dollyStart.set(0, distance);
}
function handleTouchStartDollyPan() {
if (scope.enableZoom) handleTouchStartDolly();
if (scope.enablePan) handleTouchStartPan();
}
function handleTouchStartDollyRotate() {
if (scope.enableZoom) handleTouchStartDolly();
if (scope.enableRotate) handleTouchStartRotate();
}
function handleTouchMoveRotate(event) {
if (pointers.length == 1) {
rotateEnd.set(event.pageX, event.pageY);
} else {
const position = getSecondPointerPosition(event);
const x = 0.5 * (event.pageX + position.x);
const y = 0.5 * (event.pageY + position.y);
rotateEnd.set(x, y);
}
rotateDelta.subVectors(rotateEnd, rotateStart).multiplyScalar(scope.rotateSpeed);
const element = scope.domElement;
rotateLeft(2 * Math.PI * rotateDelta.x / element.clientHeight); // yes, height
rotateUp(2 * Math.PI * rotateDelta.y / element.clientHeight);
rotateStart.copy(rotateEnd);
}
function handleTouchMovePan(event) {
if (pointers.length === 1) {
panEnd.set(event.pageX, event.pageY);
} else {
const position = getSecondPointerPosition(event);
const x = 0.5 * (event.pageX + position.x);
const y = 0.5 * (event.pageY + position.y);
panEnd.set(x, y);
}
panDelta.subVectors(panEnd, panStart).multiplyScalar(scope.panSpeed);
pan(panDelta.x, panDelta.y);
panStart.copy(panEnd);
}
function handleTouchMoveDolly(event) {
const position = getSecondPointerPosition(event);
const dx = event.pageX - position.x;
const dy = event.pageY - position.y;
const distance = Math.sqrt(dx * dx + dy * dy);
dollyEnd.set(0, distance);
dollyDelta.set(0, Math.pow(dollyEnd.y / dollyStart.y, scope.zoomSpeed));
dollyOut(dollyDelta.y);
dollyStart.copy(dollyEnd);
}
function handleTouchMoveDollyPan(event) {
if (scope.enableZoom) handleTouchMoveDolly(event);
if (scope.enablePan) handleTouchMovePan(event);
}
function handleTouchMoveDollyRotate(event) {
if (scope.enableZoom) handleTouchMoveDolly(event);
if (scope.enableRotate) handleTouchMoveRotate(event);
}
//
// event handlers - FSM: listen for events and reset state
//
function onPointerDown(event) {
if (scope.enabled === false) return;
if (pointers.length === 0) {
scope.domElement.setPointerCapture(event.pointerId);
scope.domElement.addEventListener('pointermove', onPointerMove);
scope.domElement.addEventListener('pointerup', onPointerUp);
}
//
addPointer(event);
if (event.pointerType === 'touch') {
onTouchStart(event);
} else {
onMouseDown(event);
}
}
function onPointerMove(event) {
if (scope.enabled === false) return;
if (event.pointerType === 'touch') {
onTouchMove(event);
} else {
onMouseMove(event);
}
}
function onPointerUp(event) {
if (scope.enabled === false) return;
if (event.pointerType === 'touch') {
onTouchEnd();
} else {
onMouseUp();
}
removePointer(event);
//
if (pointers.length === 0) {
scope.domElement.releasePointerCapture(event.pointerId);
scope.domElement.removeEventListener('pointermove', onPointerMove);
scope.domElement.removeEventListener('pointerup', onPointerUp);
}
}
function onPointerCancel(event) {
removePointer(event);
}
function onMouseDown(event) {
let mouseAction;
switch (event.button) {
case 0:
mouseAction = scope.mouseButtons.LEFT;
break;
case 1:
mouseAction = scope.mouseButtons.MIDDLE;
break;
case 2:
mouseAction = scope.mouseButtons.RIGHT;
break;
default:
mouseAction = -1;
}
switch (mouseAction) {
case three.MOUSE.DOLLY:
if (scope.enableZoom === false) return;
handleMouseDownDolly(event);
state = STATE.DOLLY;
break;
case three.MOUSE.ROTATE:
if (event.ctrlKey || event.metaKey || event.shiftKey) {
if (scope.enablePan === false) return;
handleMouseDownPan(event);
state = STATE.PAN;
} else {
if (scope.enableRotate === false) return;
handleMouseDownRotate(event);
state = STATE.ROTATE;
}
break;
case three.MOUSE.PAN:
if (event.ctrlKey || event.metaKey || event.shiftKey) {
if (scope.enableRotate === false) return;
handleMouseDownRotate(event);
state = STATE.ROTATE;
} else {
if (scope.enablePan === false) return;
handleMouseDownPan(event);
state = STATE.PAN;
}
break;
default:
state = STATE.NONE;
}
if (state !== STATE.NONE) {
scope.dispatchEvent(_startEvent);
}
}
function onMouseMove(event) {
if (scope.enabled === false) return;
switch (state) {
case STATE.ROTATE:
if (scope.enableRotate === false) return;
handleMouseMoveRotate(event);
break;
case STATE.DOLLY:
if (scope.enableZoom === false) return;
handleMouseMoveDolly(event);
break;
case STATE.PAN:
if (scope.enablePan === false) return;
handleMouseMovePan(event);
break;
}
}
function onMouseUp(event) {
scope.dispatchEvent(_endEvent);
state = STATE.NONE;
}
function onMouseWheel(event) {
if (scope.enabled === false || scope.enableZoom === false || (state !== STATE.NONE && state !==
STATE.ROTATE)) return;
event.preventDefault();
scope.dispatchEvent(_startEvent);
handleMouseWheel(event);
scope.dispatchEvent(_endEvent);
}
function onKeyDown(event) {
if (scope.enabled === false || scope.enablePan === false) return;
handleKeyDown(event);
}
function onTouchStart(event) {
trackPointer(event);
switch (pointers.length) {
case 1:
switch (scope.touches.ONE) {
case three.TOUCH.ROTATE:
if (scope.enableRotate === false) return;
handleTouchStartRotate();
state = STATE.TOUCH_ROTATE;
break;
case three.TOUCH.PAN:
if (scope.enablePan === false) return;
handleTouchStartPan();
state = STATE.TOUCH_PAN;
break;
default:
state = STATE.NONE;
}
break;
case 2:
switch (scope.touches.TWO) {
case three.TOUCH.DOLLY_PAN:
if (scope.enableZoom === false && scope.enablePan === false) return;
handleTouchStartDollyPan();
state = STATE.TOUCH_DOLLY_PAN;
break;
case three.TOUCH.DOLLY_ROTATE:
if (scope.enableZoom === false && scope.enableRotate === false) return;
handleTouchStartDollyRotate();
state = STATE.TOUCH_DOLLY_ROTATE;
break;
default:
state = STATE.NONE;
}
break;
default:
state = STATE.NONE;
}
if (state !== STATE.NONE) {
scope.dispatchEvent(_startEvent);
}
}
function onTouchMove(event) {
trackPointer(event);
switch (state) {
case STATE.TOUCH_ROTATE:
if (scope.enableRotate === false) return;
handleTouchMoveRotate(event);
scope.update();
break;
case STATE.TOUCH_PAN:
if (scope.enablePan === false) return;
handleTouchMovePan(event);
scope.update();
break;
case STATE.TOUCH_DOLLY_PAN:
if (scope.enableZoom === false && scope.enablePan === false) return;
handleTouchMoveDollyPan(event);
scope.update();
break;
case STATE.TOUCH_DOLLY_ROTATE:
if (scope.enableZoom === false && scope.enableRotate === false) return;
handleTouchMoveDollyRotate(event);
scope.update();
break;
default:
state = STATE.NONE;
}
}
function onTouchEnd(event) {
scope.dispatchEvent(_endEvent);
state = STATE.NONE;
}
function onContextMenu(event) {
if (scope.enabled === false) return;
event.preventDefault();
}
function addPointer(event) {
pointers.push(event);
}
function removePointer(event) {
delete pointerPositions[event.pointerId];
for (let i = 0; i < pointers.length; i++) {
if (pointers[i].pointerId == event.pointerId) {
pointers.splice(i, 1);
return;
}
}
}
function trackPointer(event) {
let position = pointerPositions[event.pointerId];
if (position === undefined) {
position = new three.Vector2();
pointerPositions[event.pointerId] = position;
}
position.set(event.pageX, event.pageY);
}
function getSecondPointerPosition(event) {
const pointer = (event.pointerId === pointers[0].pointerId) ? pointers[1] : pointers[0];
return pointerPositions[pointer.pointerId];
}
//
scope.domElement.addEventListener('contextmenu', onContextMenu);
scope.domElement.addEventListener('pointerdown', onPointerDown);
scope.domElement.addEventListener('pointercancel', onPointerCancel);
scope.domElement.addEventListener('wheel', onMouseWheel, {
passive: false
});
// force an update at start
this.update();
}
}
export default {
data() {
return {
disposing: false,
platform: null,
frameId: -1,
}
},
onLoad() {
},
onReady() {
wx.createSelectorQuery()
.select('#gl')
.node()
.exec(res => {
const canvas = res[0].node;
this.platform = new WechatPlatform(canvas);
console.log(this.platform);
three.PlatformManager.set(this.platform);
const renderer = new three.WebGL1Renderer({
canvas,
antialias: true,
alpha: true
});
const camera = new three.PerspectiveCamera(75, canvas.width / canvas.height, 0.1, 1000);
const scene = new three.Scene();
const gltfLoader = new GLTFLoader();
const controls = new OrbitControls(camera, canvas);
controls.enableDamping = true;
gltfLoader
.loadAsync(
'https://dtmall-tel.alicdn.com/edgeComputingConfig/upload_models/1591673169101/RobotExpressive.glb',
)
.then((gltf) => {
// @ts-ignore
gltf.parser = null;
gltf.scene.position.y = -2;
scene.add(gltf.scene);
});
camera.position.z = 10;
renderer.outputEncoding = three.sRGBEncoding;
scene.add(new three.AmbientLight(0xffffff, 1.0));
scene.add(new three.DirectionalLight(0xffffff, 1.0));
renderer.setSize(canvas.width, canvas.height);
renderer.setPixelRatio(three.PlatformManager.polyfill.window.devicePixelRatio);
const render = () => {
if (!this.disposing) this.frameId = three.PlatformManager.polyfill.requestAnimationFrame(
render);
controls.update();
renderer.render(scene, camera);
};
render();
});
},
onUnload() {
this.disposing = true;
three.PlatformManager.polyfill.cancelAnimationFrame(this.frameId);
three.PlatformManager.dispose();
},
methods: {
onTX(e) {
this.platform.dispatchTouchEvent(e);
},
}
}
</script>
<style>
.webgl {
display: block;
}
</style>
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。