hpg-r3f-model-scene
Version:
Pretty Footer react component used in ant.design
252 lines (251 loc) • 9.36 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.exportTexture = exportTexture;
exports.getNodeWith = void 0;
exports.getRGBColor = getRGBColor;
exports.ktx2Loader = void 0;
exports.loadKTX2Loader = loadKTX2Loader;
exports.loadTexture = loadTexture;
exports.textureLoader = exports.removeOssExt = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _lodash = require("lodash");
var _three = require("three");
var _threeStdlib = require("three-stdlib");
var removeOssExt = function removeOssExt(url) {
if (!url) {
return url;
}
if (/x-oss-process/i.test(url)) {
return url.slice(0, url.indexOf('?'));
// return url.slice(0, url.indexOf("x-oss-process"));
} else {
return url;
}
};
exports.removeOssExt = removeOssExt;
function getRGBColor(color) {
if (color instanceof _three.Color) {
if (color.equals(new _three.Color(0xffffff))) {
return 'ffffff';
}
return color.clone().getHexString();
}
return new _three.Color(0xffffff).getHexString();
}
function getMatcapColor(color) {
if (color instanceof _three.Color) {
return color.clone().getHexString();
}
return new _three.Color(0xffffff).getHexString();
}
var getNodeWith = function getNodeWith(mat) {
var inode = {
name: 'UNKNOW',
id: null,
sign: 0
};
var setMap = function setMap(type) {
var alias = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : type;
var texture = (0, _lodash.get)(mat, "".concat(type));
if (!texture) {
return;
}
var parameter = {
wrapS: texture.wrapS,
wrapT: texture.wrapT,
filtering: texture.minFilter,
rotation: texture.rotation,
offset: texture.offset,
repeat: texture.repeat,
mapping: texture.mapping,
userData: texture.userData
};
if (type === 'map' || type === 'matcap' || type === 'emissiveMap' || type === 'alphaMap' || type === 'specularColorMap' || type === 'sheenColorMap' || type === 'sheenRoughnessMap') {
Object.assign(parameter, {
format: texture.format
});
}
var _fullUrl;
var basis = (0, _lodash.get)(mat, "".concat(type, ".userData.basis"), null);
if (basis) {
_fullUrl = (0, _lodash.get)(mat, "".concat(type, ".userData.url"), null);
} else {
_fullUrl = (0, _lodash.get)(mat, "".concat(type, ".source.data.currentSrc"), null);
}
var fullUrl = removeOssExt(_fullUrl);
(0, _lodash.set)(inode, "material.".concat(alias), {
name: texture.name,
fullUrl: fullUrl,
basis: basis,
parameter: parameter
});
};
if (mat instanceof _three.MeshPhysicalMaterial) {
inode.name = mat.name; //使用材质名称作为Node名称
inode.material = {
type: 'PBR',
color: '#' + getRGBColor(mat.color),
// color: "#" + _color.convertLinearToSRGB().getHexString(),
emissive: '#' + getRGBColor(mat.emissive),
roughness: mat.roughness,
metalness: mat.metalness,
blending: mat.blending,
normalScale: mat.normalScale.x,
aoMapIntensity: mat.aoMapIntensity,
bumpScale: mat.bumpScale,
envMapIntensity: mat.envMapIntensity,
lightMapIntensity: mat.lightMapIntensity,
displacementScale: mat.displacementScale,
ior: mat.ior,
reflectivity: mat.reflectivity,
opacity: mat.opacity,
transparent: mat.transparent,
alphaTest: mat.alphaTest,
clearcoat: mat.clearcoat,
clearcoatRoughness: mat.clearcoatRoughness,
clearcoatNormal: mat.clearcoatNormalScale.x,
transmission: mat.transmission,
emissiveIntensity: mat.emissiveIntensity,
blendSrc: mat.blendSrc,
blendSrcAlpha: mat.blendSrcAlpha,
blendDst: mat.blendDst,
blendDstAlpha: mat.blendDstAlpha,
depthWrite: mat.depthWrite,
side: mat.side,
anisotropy: 1,
specularIntensity: (0, _lodash.get)(mat, "specularIntensity", 1.0),
specularColor: '#' + getRGBColor(mat.specularColor),
sheen: (0, _lodash.get)(mat, "sheen", 0),
sheenRoughness: (0, _lodash.get)(mat, "sheenRoughness", 1.0),
sheenColor: '#' + getRGBColor(mat.sheenColor)
};
setMap('map');
setMap('aoMap');
setMap('emissiveMap');
setMap('normalMap');
setMap('roughnessMap');
setMap('metalnessMap');
setMap('bumpMap');
setMap('displacementMap');
setMap('lightMap');
setMap('alphaMap');
setMap('clearcoatMap');
setMap('clearcoatRoughnessMap');
setMap('clearcoatNormalMap');
setMap('transmissionMap');
setMap('specularColorMap');
setMap('sheenColorMap');
setMap('sheenRoughnessMap');
} else if (mat instanceof _three.MeshMatcapMaterial) {
inode.name = mat.name; //使用材质名称作为Node名称
inode.material = {
type: 'MATCAP',
// color: "#" + _color.getHexString(),
blending: mat.blending,
normalScale: mat.normalScale.x,
bumpScale: mat.bumpScale,
displacementScale: mat.displacementScale,
opacity: mat.opacity,
transparent: mat.transparent,
alphaTest: mat.alphaTest,
blendSrc: mat.blendSrc,
blendSrcAlpha: mat.blendSrcAlpha,
blendDst: mat.blendDst,
blendDstAlpha: mat.blendDstAlpha,
depthWrite: mat.depthWrite,
side: mat.side,
anisotropy: 1
};
if (mat.color instanceof _three.Color) {
var _color = mat.color.clone();
inode.material.color = '#' + _color.getHexString();
}
setMap('map');
setMap('matcap');
setMap('normalMap');
setMap('bumpMap');
setMap('displacementMap');
setMap('alphaMap');
}
return inode;
};
exports.getNodeWith = getNodeWith;
var textureLoader = new _three.TextureLoader();
exports.textureLoader = textureLoader;
textureLoader.setCrossOrigin('anonymous');
function loadTexture(_x) {
return _loadTexture.apply(this, arguments);
}
function _loadTexture() {
_loadTexture = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(url) {
return _regenerator.default.wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
return _context.abrupt("return", new Promise(function (resolve, reject) {
textureLoader.load(url, function (texture) {
return resolve(texture.image);
}, function () {
return reject(null);
});
}));
case 1:
case "end":
return _context.stop();
}
}, _callee);
}));
return _loadTexture.apply(this, arguments);
}
var ktx2Loader = new _threeStdlib.KTX2Loader();
exports.ktx2Loader = ktx2Loader;
function loadKTX2Loader(_x2) {
return _loadKTX2Loader.apply(this, arguments);
}
function _loadKTX2Loader() {
_loadKTX2Loader = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(url) {
return _regenerator.default.wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
return _context2.abrupt("return", new Promise(function (resolve, reject) {
ktx2Loader.load(url, function (texture) {
return resolve(texture);
}, function () {}, function () {
return reject(null);
});
}));
case 1:
case "end":
return _context2.stop();
}
}, _callee2);
}));
return _loadKTX2Loader.apply(this, arguments);
}
function exportTexture(material, textureName) {
if (!(material instanceof _three.MeshPhysicalMaterial)) {
return;
}
var currentSrc = '';
var basis = (0, _lodash.get)(material, "".concat(textureName, ".userData.basis"), null);
if (basis) {
currentSrc = basis;
} else {
currentSrc = (0, _lodash.get)(material, "".concat(textureName, ".image.currentSrc")) || '';
}
return {
url: removeOssExt(currentSrc) || '',
format: (0, _lodash.get)(material, "".concat(textureName, ".format"), null) || _three.RGBAFormat,
minFilter: (0, _lodash.get)(material, "".concat(textureName, ".minFilter"), null) || _three.LinearMipmapLinearFilter,
magFilter: (0, _lodash.get)(material, "".concat(textureName, ".magFilter"), null) || _three.LinearFilter,
wrapS: (0, _lodash.get)(material, "".concat(textureName, ".wrapS"), null) || _three.RepeatWrapping,
wrapT: (0, _lodash.get)(material, "".concat(textureName, ".wrapT"), null) || _three.RepeatWrapping,
rotation: (0, _lodash.get)(material, "".concat(textureName, ".rotation"), null) || 0,
offset: (0, _lodash.get)(material, "".concat(textureName, ".offset"), null) ? [(0, _lodash.get)(material, "".concat(textureName, ".offset.x"), null) || 0, (0, _lodash.get)(material, "".concat(textureName, ".offset.y"), null) || 0] : null,
repeat: (0, _lodash.get)(material, "".concat(textureName, ".repeat"), null) ? [(0, _lodash.get)(material, "".concat(textureName, ".repeat.x"), null) || 1, (0, _lodash.get)(material, "".concat(textureName, ".repeat.y"), null) || 1] : null,
userData: (0, _lodash.get)(material, "".concat(textureName, ".userData"), null) || {}
};
}