hpg-r3f-model-scene
Version:
Pretty Footer react component used in ant.design
192 lines (187 loc) • 7.38 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _regeneratorRuntime from "@babel/runtime/regenerator";
/**
* Loader for KTX 2.0 GPU Texture containers.
*
* KTX 2.0 is a container format for various GPU texture formats. The loader
* supports Basis Universal GPU textures, which can be quickly transcoded to
* a wide variety of GPU texture compression formats. While KTX 2.0 also allows
* other hardware-specific formats, this loader does not yet parse them.
*
* This loader parses the KTX 2.0 container and then relies on
* THREE.BasisTextureLoader to complete the transcoding process.
*
* References:
* - KTX: http://github.khronos.org/KTX-Specification/
* - DFD: https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.html#basicdescriptor
*/
import { CompressedTexture, CompressedTextureLoader, FileLoader, LinearEncoding, sRGBEncoding } from 'three';
import { read as readKTX, KTX2ChannelETC1S, KTX2ChannelUASTC, KTX2Flags, KTX2Model, KTX2SupercompressionScheme, KTX2Transfer } from 'three/examples/jsm/libs/ktx-parse.module';
import { BasisTextureLoader } from './BasisTextureLoader';
import { ZSTDDecoder } from 'three/examples/jsm/libs/zstddec.module';
var KTX2Loader = /*#__PURE__*/function (_CompressedTextureLoa) {
_inherits(KTX2Loader, _CompressedTextureLoa);
var _super = _createSuper(KTX2Loader);
function KTX2Loader(manager) {
var _this;
_classCallCheck(this, KTX2Loader);
_this = _super.call(this, manager);
_this.basisLoader = new BasisTextureLoader(manager);
_this.zstd = new ZSTDDecoder();
_this.zstd.init();
if (typeof MSC_TRANSCODER !== 'undefined') {
console.warn('THREE.KTX2Loader: Please update to latest "basis_transcoder".' + ' "msc_basis_transcoder" is no longer supported in three.js r125+.');
}
return _this;
}
_createClass(KTX2Loader, [{
key: "setTranscoderPath",
value: function setTranscoderPath(path) {
this.basisLoader.setTranscoderPath(path);
return this;
}
}, {
key: "setWorkerLimit",
value: function setWorkerLimit(path) {
this.basisLoader.setWorkerLimit(path);
return this;
}
}, {
key: "detectSupport",
value: function detectSupport(renderer) {
this.basisLoader.detectSupport(renderer);
return this;
}
}, {
key: "dispose",
value: function dispose() {
this.basisLoader.dispose();
return this;
}
}, {
key: "load",
value: function load(url, onLoad, onProgress, onError) {
var scope = this;
var texture = new CompressedTexture();
var bufferPending = new Promise(function (resolve, reject) {
new FileLoader(scope.manager).setPath(scope.path).setResponseType('arraybuffer').load(url, resolve, onProgress, reject);
});
bufferPending.then(function (buffer) {
scope.parse(buffer, function (_texture) {
texture.copy(_texture);
texture.needsUpdate = true;
if (onLoad) onLoad(texture);
}, onError);
}).catch(onError);
return texture;
}
}, {
key: "parse",
value: function parse(buffer, onLoad, onError) {
var scope = this;
var ktx = readKTX(new Uint8Array(buffer));
if (ktx.pixelDepth > 0) {
throw new Error('THREE.KTX2Loader: Only 2D textures are currently supported.');
}
if (ktx.layerCount > 1) {
throw new Error('THREE.KTX2Loader: Array textures are not currently supported.');
}
if (ktx.faceCount > 1) {
throw new Error('THREE.KTX2Loader: Cube textures are not currently supported.');
}
var dfd = KTX2Utils.getBasicDFD(ktx);
KTX2Utils.createLevels(ktx, this.zstd).then(function (levels) {
var basisFormat = dfd.colorModel === KTX2Model.UASTC ? BasisTextureLoader.BasisFormat.UASTC_4x4 : BasisTextureLoader.BasisFormat.ETC1S;
var parseConfig = {
levels: levels,
width: ktx.pixelWidth,
height: ktx.pixelHeight,
basisFormat: basisFormat,
hasAlpha: KTX2Utils.getAlpha(ktx)
};
if (basisFormat === BasisTextureLoader.BasisFormat.ETC1S) {
parseConfig.globalData = ktx.globalData;
}
return scope.basisLoader.parseInternalAsync(parseConfig);
}).then(function (texture) {
texture.encoding = dfd.transferFunction === KTX2Transfer.SRGB ? sRGBEncoding : LinearEncoding;
texture.premultiplyAlpha = KTX2Utils.getPremultiplyAlpha(ktx);
onLoad(texture);
}).catch(onError);
return this;
}
}]);
return KTX2Loader;
}(CompressedTextureLoader);
var KTX2Utils = {
createLevels: function () {
var _createLevels = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(ktx, zstd) {
var levels, width, height, levelIndex, levelWidth, levelHeight, levelData;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
if (!(ktx.supercompressionScheme === KTX2SupercompressionScheme.ZSTD)) {
_context.next = 3;
break;
}
_context.next = 3;
return zstd.init();
case 3:
levels = [];
width = ktx.pixelWidth;
height = ktx.pixelHeight;
for (levelIndex = 0; levelIndex < ktx.levels.length; levelIndex++) {
levelWidth = Math.max(1, Math.floor(width / Math.pow(2, levelIndex)));
levelHeight = Math.max(1, Math.floor(height / Math.pow(2, levelIndex)));
levelData = ktx.levels[levelIndex].levelData;
if (ktx.supercompressionScheme === KTX2SupercompressionScheme.ZSTD) {
levelData = zstd.decode(levelData, ktx.levels[levelIndex].uncompressedByteLength);
}
levels.push({
index: levelIndex,
width: levelWidth,
height: levelHeight,
data: levelData
});
}
return _context.abrupt("return", levels);
case 8:
case "end":
return _context.stop();
}
}, _callee);
}));
function createLevels(_x, _x2) {
return _createLevels.apply(this, arguments);
}
return createLevels;
}(),
getBasicDFD: function getBasicDFD(ktx) {
// Basic Data Format Descriptor Block is always the first DFD.
return ktx.dataFormatDescriptor[0];
},
getAlpha: function getAlpha(ktx) {
var dfd = this.getBasicDFD(ktx);
// UASTC
if (dfd.colorModel === KTX2Model.UASTC) {
if ((dfd.samples[0].channelID & 0xf) === KTX2ChannelUASTC.RGBA) {
return true;
}
return false;
}
// ETC1S
if (dfd.samples.length === 2 && (dfd.samples[1].channelID & 0xf) === KTX2ChannelETC1S.AAA) {
return true;
}
return false;
},
getPremultiplyAlpha: function getPremultiplyAlpha(ktx) {
var dfd = this.getBasicDFD(ktx);
return !!(dfd.flags & KTX2Flags.ALPHA_PREMULTIPLIED);
}
};
export { KTX2Loader };