hpg-r3f-model-scene
Version:
Pretty Footer react component used in ant.design
460 lines (459 loc) • 22.7 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getJsonByMaterial = getJsonByMaterial;
exports.getMaterialByJson = getMaterialByJson;
exports.textureCache = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _three = require("three");
var _lodash = require("lodash");
var _index = require("./index");
var _index2 = require("../utils/index");
var _enums = require("./enums");
var _interface = require("../interface");
function getColor(color) {
var isEmissive = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
if (Array.isArray(color) && isFinite(color[0])) {
var c = "rgb(".concat(color[0], " ,").concat(color[1], " ,").concat(color[2], ")");
return new _three.Color(c);
} else if (typeof color === 'string') {
if (color.indexOf('#') > -1) {
return new _three.Color(color);
}
return new _three.Color('#' + color);
} else {
return new _three.Color(isEmissive ? 0x000000 : 0xffffff);
}
}
function getMatcapColor(color) {
if (Array.isArray(color) && isFinite(color[0])) {
var c = "rgb(".concat(color[0], " ,").concat(color[1], " ,").concat(color[2], ")");
return new _three.Color(c);
} else if (typeof color === 'string') {
if (color.indexOf('#') > -1) {
return new _three.Color(color);
}
return new _three.Color('#' + color);
} else {
return new _three.Color(0xffffff);
}
}
var textureCache = new Map();
exports.textureCache = textureCache;
var textureCacheInternal = new Map();
var urlCacheInternal = new Map();
var getTextureLength = 0;
var getTextureLoadedLength = 0;
var matCache = new Map();
function getTextureKey(t) {
return JSON.stringify(t);
}
function textureLoaderComplete() {
getTextureLoadedLength++;
if (getTextureLength === getTextureLoadedLength) {
// setMaterial();
}
}
function getTextureAsync(_x) {
return _getTextureAsync.apply(this, arguments);
}
function _getTextureAsync() {
_getTextureAsync = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(t) {
return _regenerator.default.wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
return _context.abrupt("return", new Promise(function (resolve) {
var _t$userData;
getTextureLength++;
if (!t || !(t === null || t === void 0 ? void 0 : t.url)) {
resolve(null);
}
//如果最后一个是?则去掉
if (t.url.slice(t.url.length - 1) === '?') {
t.url = t.url.slice(0, t.url.length - 1);
}
// if(t.url == 'https://sxt-dev-bucket.oss-cn-hangzhou.aliyuncs.com/img/1681110023278.jpg'){
// // t.url = 'https://sxt-dev-bucket.oss-cn-hangzhou.aliyuncs.com/texturize/x1.basis'
// // t.url = 'https://static.uzhan.3dwangzhan.com/texturize/f33a34e9-448b-49e7-9449-4ef3ac6b2d9c.basis'
// // t.url = 'https://threejs.org/examples/textures/compressed/sample_uastc_zstd.ktx2'
// // t.url = 'https://sxt-dev-bucket.oss-cn-hangzhou.aliyuncs.com/texturize/sample_uastc_zstd.ktx2'
// t.url = 'https://sxt-dev-bucket.oss-cn-hangzhou.aliyuncs.com/texturize/c1e8780c-a4e6-4732-9a15-665727af2e66.basis'
// console.log(t.url, 't.url')
// }
var textureKey = getTextureKey(t);
if (textureCacheInternal.has(textureKey)) {
resolve(textureCacheInternal.get(textureKey));
}
var isBasis = /.basis/i.test(t.url) || /.ktx2/i.test(t.url);
//编辑情况下
if (t.userData && ((_t$userData = t.userData) === null || _t$userData === void 0 ? void 0 : _t$userData.url)) {
if (isBasis) {
var _t$userData2;
t.url = (_t$userData2 = t.userData) === null || _t$userData2 === void 0 ? void 0 : _t$userData2.url;
}
}
// if (isBasis) {
// ktx2Loader.setCrossOrigin('anonymous');
// ktx2Loader.setTranscoderPath(
// '//hgp-public.oss-cn-hangzhou.aliyuncs.com/web3d/basis/v3/',
// );
// ktx2Loader.detectSupport(new WebGLRenderer());
// ktx2Loader.load(
// t.url,
// (_texture1: Texture) => {
// let _texture: Texture;
// if (urlCacheInternal.has(t.url)) {
// _texture = _texture1.clone();
// } else {
// _texture = _texture1;
// urlCacheInternal.set(t.url, t.url);
// }
// _texture.encoding = sRGBEncoding;
// // texture.anisotropy = Engine.maxAnisotropy;
// // if( t.format ) texture.format = t.format || RGBAFormat;
// // _texture.flipY = true;
// if (t.mapping)
// _texture.mapping = (t.mapping as AnyMapping) || UVMapping;
// if (t.minFilter)
// _texture.minFilter =
// (t.minFilter as MinificationTextureFilter) || NearestFilter;
// if (t.magFilter)
// _texture.magFilter =
// (t.magFilter as MagnificationTextureFilter) || NearestFilter;
// if (t.wrapS)
// _texture.wrapS = (t.wrapS as Wrapping) || ClampToEdgeWrapping;
// if (t.wrapT)
// _texture.wrapT = (t.wrapT as Wrapping) || ClampToEdgeWrapping;
// if (t.rotation) _texture.rotation = t.rotation;
// if (Array.isArray(t.offset)) {
// _texture.offset = new Vector2(t.offset[0], t.offset[1]);
// }
// if (Array.isArray(t.repeat)) {
// _texture.repeat = new Vector2(t.repeat[0], t.repeat[1]);
// }
// if (t.userData) {
// _texture.userData = t.userData;
// }
// textureCacheInternal.set(textureKey, _texture);
// textureLoaderComplete();
// resolve(_texture);
// },
// undefined,
// (e: any) => {
// console.log('_texture1', e);
// textureLoaderComplete();
// resolve(null);
// },
// );
// } else {
_index.textureLoader.load(t.url, function (_texture1) {
var _texture;
if (urlCacheInternal.has(t.url)) {
_texture = _texture1.clone();
} else {
_texture = _texture1;
urlCacheInternal.set(t.url, t.url);
}
// _texture.encoding = sRGBEncoding;
_texture.colorSpace = _three.SRGBColorSpace;
// _texture.wrapS = _texture.wrapT = RepeatWrapping;
// texture.anisotropy = Engine.maxAnisotropy;
// if( t.format ) texture.format = t.format || RGBAFormat;
if (t.mapping) _texture.mapping = t.mapping || _three.UVMapping;
if (t.minFilter) _texture.minFilter = t.minFilter || _three.NearestFilter;
if (t.magFilter) _texture.magFilter = t.magFilter || _three.NearestFilter;
if (t.wrapS) _texture.wrapS = t.wrapS || _three.RepeatWrapping;
if (t.wrapT) _texture.wrapT = t.wrapT || _three.RepeatWrapping;
if (t.rotation) _texture.rotation = t.rotation;
if (Array.isArray(t.offset)) {
_texture.offset = new _three.Vector2(t.offset[0], t.offset[1]);
}
if (Array.isArray(t.repeat)) {
_texture.repeat = new _three.Vector2(t.repeat[0], t.repeat[1]);
}
if (t.userData && t.userData.url) {
_texture.userData = t.userData;
} else {
_texture.userData.url = t.url;
}
textureCacheInternal.set(textureKey, _texture);
textureLoaderComplete();
resolve(_texture);
}, undefined, function () {
textureLoaderComplete();
resolve(null);
});
// }
}));
case 1:
case "end":
return _context.stop();
}
}, _callee);
}));
return _getTextureAsync.apply(this, arguments);
}
function getMaterialByJson(_x2) {
return _getMaterialByJson.apply(this, arguments);
}
function _getMaterialByJson() {
_getMaterialByJson = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(item) {
var mat, polygonOffsetUnits, polygonOffsetFactor, map, roughnessMap, metalnessMap, aoMap, alphaMap, bumpMap, emissiveMap, transmissionMap, normalMap, clearcoatMap, clearcoatNormalMap, clearcoatRoughnessMap, specularColorMap, sheenRoughnessMap, sheenColorMap;
return _regenerator.default.wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
mat = null;
polygonOffsetUnits = 0;
polygonOffsetFactor = 80;
_context2.next = 5;
return getTextureAsync(item.map);
case 5:
map = _context2.sent;
_context2.next = 8;
return getTextureAsync(item.roughnessMap);
case 8:
roughnessMap = _context2.sent;
_context2.next = 11;
return getTextureAsync(item.metalnessMap);
case 11:
metalnessMap = _context2.sent;
_context2.next = 14;
return getTextureAsync(item.aoMap);
case 14:
aoMap = _context2.sent;
_context2.next = 17;
return getTextureAsync(item.alphaMap);
case 17:
alphaMap = _context2.sent;
_context2.next = 20;
return getTextureAsync(item.bumpMap);
case 20:
bumpMap = _context2.sent;
_context2.next = 23;
return getTextureAsync(item.emissiveMap);
case 23:
emissiveMap = _context2.sent;
_context2.next = 26;
return getTextureAsync(item.transmissionMap);
case 26:
transmissionMap = _context2.sent;
_context2.next = 29;
return getTextureAsync(item.normalMap);
case 29:
normalMap = _context2.sent;
_context2.next = 32;
return getTextureAsync(item.clearcoatMap);
case 32:
clearcoatMap = _context2.sent;
_context2.next = 35;
return getTextureAsync(item.clearcoatNormalMap);
case 35:
clearcoatNormalMap = _context2.sent;
_context2.next = 38;
return getTextureAsync(item.clearcoatRoughnessMap);
case 38:
clearcoatRoughnessMap = _context2.sent;
_context2.next = 41;
return getTextureAsync(item.specularColorMap);
case 41:
specularColorMap = _context2.sent;
_context2.next = 44;
return getTextureAsync(item.sheenRoughnessMap);
case 44:
sheenRoughnessMap = _context2.sent;
_context2.next = 47;
return getTextureAsync(item.sheenColorMap);
case 47:
sheenColorMap = _context2.sent;
mat = new _three.MeshPhysicalMaterial({
color: getColor(item.color),
map: map,
roughness: isFinite(item.roughness) ? item.roughness : 0.5,
roughnessMap: roughnessMap,
metalness: isFinite(item.metalness) ? item.metalness : 0.5,
metalnessMap: metalnessMap,
aoMap: aoMap,
bumpMap: bumpMap,
emissiveMap: emissiveMap,
alphaMap: alphaMap,
transmissionMap: transmissionMap,
normalMap: normalMap,
clearcoatMap: clearcoatMap,
clearcoatNormalMap: clearcoatNormalMap,
clearcoatRoughnessMap: clearcoatRoughnessMap,
opacity: isFinite(item.opacity) ? item.opacity : 1,
transparent: (item === null || item === void 0 ? void 0 : item.transparent) ? item.transparent : item.opacity < 1,
alphaTest: item.alphaTest || 0,
aoMapIntensity: isFinite(item.aoMapIntensity) ? item.aoMapIntensity : 1,
// bumpScale: isFinite(item.bumpScale) ? (item.bumpScale > 0.5 ? item.bumpScale : 1) : 1,
bumpScale: isFinite(item.bumpScale) ? item.bumpScale : 1,
normalMapType: item.normalMapType || _three.TangentSpaceNormalMap,
displacementBias: item.displacementBias || 0.0,
displacementScale: isFinite(item.displacementScale) ? item.displacementScale : 1.0,
emissive: getColor(item.emissive, true),
normalScale: (0, _lodash.isArray)(item.normalScale) ? new _three.Vector2().fromArray(item.normalScale) : new _three.Vector2(1, 1),
emissiveIntensity: isFinite(item.emissiveIntensity) ? item.emissiveIntensity : 1.0,
envMapIntensity: isFinite(item.envMapIntensity) ? item.envMapIntensity : 1.0,
clearcoat: item.clearcoat || 0.0,
clearcoatRoughness: item.clearcoatRoughness || 0.0,
clearcoatNormalScale: (0, _lodash.isArray)(item.clearcoatNormalScale) ? new _three.Vector2().fromArray(item.clearcoatNormalScale) : new _three.Vector2(1, 1),
ior: isFinite(item.ior) ? item.ior : 1.5,
reflectivity: isFinite(item.reflectivity) ? item.reflectivity : 0.5,
sheen: item.sheen || 0.0,
transmission: item.transmission || 0.0,
side: item.side || _three.FrontSide,
polygonOffset: polygonOffsetUnits !== 0 || polygonOffsetFactor !== 80,
polygonOffsetUnits: polygonOffsetUnits,
polygonOffsetFactor: polygonOffsetFactor,
specularIntensity: isFinite(item.specularIntensity) ? item.specularIntensity : 1,
specularColor: getColor(item.specularColor),
specularColorMap: specularColorMap,
sheenRoughness: item.sheenRoughness || 1.0,
sheenColor: getColor(item.sheenColor)
});
if (item.sheenRoughnessMap) {
mat.sheenRoughnessMap = sheenRoughnessMap;
}
if (item.sheenColorMap) {
mat.sheenColorMap = sheenColorMap;
}
if (!mat) {
_context2.next = 54;
break;
}
mat.needsUpdate = true;
return _context2.abrupt("return", mat);
case 54:
return _context2.abrupt("return", new _three.MeshPhysicalMaterial());
case 55:
case "end":
return _context2.stop();
}
}, _callee2);
}));
return _getMaterialByJson.apply(this, arguments);
}
function getJsonByMaterial(_x3) {
return _getJsonByMaterial.apply(this, arguments);
}
function _getJsonByMaterial() {
_getJsonByMaterial = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(material) {
var opacity, alphaTest, side, depthWrite, transparent, color, map, roughness, roughnessMap, metalness, metalnessMap, alphaMap, aoMap, aoMapIntensity, bumpMap, bumpScale, normalMap, ns, normalScale, normalMapType, displacementMap, displacementBias, displacementScale, emissive, emissiveMap, emissiveIntensity, envMapIntensity, clearcoat, clearcoatMap, clearcoatNormalMap, cns, clearcoatNormalScale, clearcoatRoughness, clearcoatRoughnessMap, ior, reflectivity, sheen, sheenColor, sheenColorMap, sheenRoughness, sheenRoughnessMap, transmission, transmissionMap, specularIntensity, specularColor, specularColorMap, alphaMode;
return _regenerator.default.wrap(function _callee3$(_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
console.log(material, 'getJsonByMaterial');
//base
opacity = (0, _lodash.get)(material, 'opacity', 1.0);
alphaTest = (0, _lodash.get)(material, 'alphaTest', 0.0);
side = (0, _lodash.get)(material, 'side', _three.FrontSide);
depthWrite = (0, _lodash.get)(material, 'depthWrite', true);
transparent = (0, _lodash.get)(material, 'transparent', true); //standard
color = (0, _index2.getRGBColor)((0, _lodash.get)(material, 'color', new _three.Color(0xffffff)));
map = (0, _index2.exportTexture)(material, _enums.MAP.MAP);
roughness = (0, _lodash.get)(material, 'roughness', 0.5);
roughnessMap = (0, _index2.exportTexture)(material, _enums.MAP.ROUGHNESS);
metalness = (0, _lodash.get)(material, 'metalness', 0.5);
metalnessMap = (0, _index2.exportTexture)(material, _enums.MAP.METALNESS);
alphaMap = (0, _index2.exportTexture)(material, _enums.MAP.ALPHA);
aoMap = (0, _index2.exportTexture)(material, _enums.MAP.AO);
aoMapIntensity = (0, _lodash.get)(material, 'aoMapIntensity', 1.0);
bumpMap = (0, _index2.exportTexture)(material, _enums.MAP.BUMP);
bumpScale = (0, _lodash.get)(material, 'bumpScale', 1.0);
normalMap = (0, _index2.exportTexture)(material, _enums.MAP.NORMAL);
ns = (0, _lodash.get)(material, 'normalScale');
normalScale = (ns instanceof _three.Vector2 ? ns : new _three.Vector2(1, 1)).toArray();
normalMapType = (0, _lodash.get)(material, 'normalMapType', _three.TangentSpaceNormalMap);
displacementMap = (0, _index2.exportTexture)(material, _enums.MAP.DISPLACEMENT);
displacementBias = (0, _lodash.get)(material, 'displacementBias', 0.0);
displacementScale = (0, _lodash.get)(material, 'displacementScale', 1.0);
emissive = (0, _index2.getRGBColor)((0, _lodash.get)(material, 'emissive', new _three.Color(0x000000)));
emissiveMap = (0, _index2.exportTexture)(material, _enums.MAP.EMISSIVE);
emissiveIntensity = (0, _lodash.get)(material, 'emissiveIntensity', 1.0);
envMapIntensity = (0, _lodash.get)(material, 'envMapIntensity', 1.0); //physical
clearcoat = (0, _lodash.get)(material, 'clearcoat', 0.0);
clearcoatMap = (0, _index2.exportTexture)(material, _enums.MAP.CLEARCOAT);
clearcoatNormalMap = (0, _index2.exportTexture)(material, _enums.MAP.CLEARCOATNORMAL);
cns = (0, _lodash.get)(material, 'clearcoatNormalScale');
clearcoatNormalScale = (cns instanceof _three.Vector2 ? cns : new _three.Vector2(1, 1)).toArray();
clearcoatRoughness = (0, _lodash.get)(material, 'clearcoatRoughness', 0.0);
clearcoatRoughnessMap = (0, _index2.exportTexture)(material, _enums.MAP.CLEARCOATROUGHNESS);
ior = (0, _lodash.get)(material, 'ior', 1.5);
reflectivity = (0, _lodash.get)(material, 'reflectivity', 0.5);
sheen = (0, _lodash.get)(material, 'sheen', 0.0);
sheenColor = (0, _index2.getRGBColor)((0, _lodash.get)(material, 'sheenColor', new _three.Color(0xffffff)));
sheenColorMap = (0, _index2.exportTexture)(material, _enums.MAP.SHEENCOLORMAP);
sheenRoughness = (0, _lodash.get)(material, 'sheenRoughness', 1.0);
sheenRoughnessMap = (0, _index2.exportTexture)(material, _enums.MAP.SHEENROUGHNESSMAP);
transmission = (0, _lodash.get)(material, 'transmission', 0.0);
transmissionMap = (0, _index2.exportTexture)(material, _enums.MAP.TRANSMISSION);
specularIntensity = (0, _lodash.get)(material, 'specularIntensity', 1.0);
specularColor = (0, _index2.getRGBColor)((0, _lodash.get)(material, 'specularColor', new _three.Color(0xffffff)));
specularColorMap = (0, _index2.exportTexture)(material, _enums.MAP.SPECULARCOLORMAP);
alphaMode = _interface.AlphaMode.OPAQUE;
if ((material === null || material === void 0 ? void 0 : material.transparent) && material.depthWrite) {
alphaMode = _interface.AlphaMode.OPAQUE;
} else if ((material === null || material === void 0 ? void 0 : material.transparent) && !material.depthWrite) {
alphaMode = _interface.AlphaMode.BLEND;
} else if ((material === null || material === void 0 ? void 0 : material.alphaTest) > 0) {
alphaMode = _interface.AlphaMode.MASK;
}
return _context3.abrupt("return", {
type: 'PBR',
side: side,
depthWrite: depthWrite,
transparent: transparent,
color: color,
map: map,
roughness: roughness,
roughnessMap: roughnessMap,
metalness: metalness,
metalnessMap: metalnessMap,
opacity: opacity,
alphaTest: alphaTest,
alphaMap: alphaMap,
aoMap: aoMap,
aoMapIntensity: aoMapIntensity,
bumpMap: bumpMap,
bumpScale: bumpScale,
normalMap: normalMap,
normalMapType: normalMapType,
normalScale: normalScale,
displacementMap: displacementMap,
displacementBias: displacementBias,
displacementScale: displacementScale,
emissive: emissive,
emissiveMap: emissiveMap,
emissiveIntensity: emissiveIntensity,
envMapIntensity: envMapIntensity,
clearcoat: clearcoat,
clearcoatMap: clearcoatMap,
clearcoatNormalMap: clearcoatNormalMap,
clearcoatNormalScale: clearcoatNormalScale,
clearcoatRoughness: clearcoatRoughness,
clearcoatRoughnessMap: clearcoatRoughnessMap,
ior: ior,
reflectivity: reflectivity,
sheen: sheen,
transmission: transmission,
transmissionMap: transmissionMap,
specularIntensity: specularIntensity,
specularColor: specularColor,
specularColorMap: specularColorMap,
sheenColor: sheenColor,
sheenColorMap: sheenColorMap,
sheenRoughness: sheenRoughness,
sheenRoughnessMap: sheenRoughnessMap,
alphaMode: alphaMode
});
case 50:
case "end":
return _context3.stop();
}
}, _callee3);
}));
return _getJsonByMaterial.apply(this, arguments);
}