UNPKG

hpg-r3f-model-scene

Version:

Pretty Footer react component used in ant.design

450 lines 21.4 kB
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; import _regeneratorRuntime from "@babel/runtime/regenerator"; import { MeshPhysicalMaterial, Color, Vector2, TangentSpaceNormalMap, FrontSide, NearestFilter, UVMapping, RepeatWrapping, SRGBColorSpace } from 'three'; import { get, isArray } from 'lodash'; import { textureLoader } from './index'; import { getRGBColor, exportTexture } from '../utils/index'; import { MAP } from './enums'; import { AlphaMode } from '../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 Color(c); } else if (typeof color === 'string') { if (color.indexOf('#') > -1) { return new Color(color); } return new Color('#' + color); } else { return new 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 Color(c); } else if (typeof color === 'string') { if (color.indexOf('#') > -1) { return new Color(color); } return new Color('#' + color); } else { return new Color(0xffffff); } } export var textureCache = new Map(); 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 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(t) { return _regeneratorRuntime.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 { 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 = 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 || UVMapping; if (t.minFilter) _texture.minFilter = t.minFilter || NearestFilter; if (t.magFilter) _texture.magFilter = t.magFilter || NearestFilter; if (t.wrapS) _texture.wrapS = t.wrapS || RepeatWrapping; if (t.wrapT) _texture.wrapT = t.wrapT || RepeatWrapping; 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 && 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); } export function getMaterialByJson(_x2) { return _getMaterialByJson.apply(this, arguments); } function _getMaterialByJson() { _getMaterialByJson = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(item) { var mat, polygonOffsetUnits, polygonOffsetFactor, map, roughnessMap, metalnessMap, aoMap, alphaMap, bumpMap, emissiveMap, transmissionMap, normalMap, clearcoatMap, clearcoatNormalMap, clearcoatRoughnessMap, specularColorMap, sheenRoughnessMap, sheenColorMap; return _regeneratorRuntime.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 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 || TangentSpaceNormalMap, displacementBias: item.displacementBias || 0.0, displacementScale: isFinite(item.displacementScale) ? item.displacementScale : 1.0, emissive: getColor(item.emissive, true), normalScale: isArray(item.normalScale) ? new Vector2().fromArray(item.normalScale) : new 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: isArray(item.clearcoatNormalScale) ? new Vector2().fromArray(item.clearcoatNormalScale) : new 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 || 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 MeshPhysicalMaterial()); case 55: case "end": return _context2.stop(); } }, _callee2); })); return _getMaterialByJson.apply(this, arguments); } export function getJsonByMaterial(_x3) { return _getJsonByMaterial.apply(this, arguments); } function _getJsonByMaterial() { _getJsonByMaterial = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.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 _regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: console.log(material, 'getJsonByMaterial'); //base opacity = get(material, 'opacity', 1.0); alphaTest = get(material, 'alphaTest', 0.0); side = get(material, 'side', FrontSide); depthWrite = get(material, 'depthWrite', true); transparent = get(material, 'transparent', true); //standard color = getRGBColor(get(material, 'color', new Color(0xffffff))); map = exportTexture(material, MAP.MAP); roughness = get(material, 'roughness', 0.5); roughnessMap = exportTexture(material, MAP.ROUGHNESS); metalness = get(material, 'metalness', 0.5); metalnessMap = exportTexture(material, MAP.METALNESS); alphaMap = exportTexture(material, MAP.ALPHA); aoMap = exportTexture(material, MAP.AO); aoMapIntensity = get(material, 'aoMapIntensity', 1.0); bumpMap = exportTexture(material, MAP.BUMP); bumpScale = get(material, 'bumpScale', 1.0); normalMap = exportTexture(material, MAP.NORMAL); ns = get(material, 'normalScale'); normalScale = (ns instanceof Vector2 ? ns : new Vector2(1, 1)).toArray(); normalMapType = get(material, 'normalMapType', TangentSpaceNormalMap); displacementMap = exportTexture(material, MAP.DISPLACEMENT); displacementBias = get(material, 'displacementBias', 0.0); displacementScale = get(material, 'displacementScale', 1.0); emissive = getRGBColor(get(material, 'emissive', new Color(0x000000))); emissiveMap = exportTexture(material, MAP.EMISSIVE); emissiveIntensity = get(material, 'emissiveIntensity', 1.0); envMapIntensity = get(material, 'envMapIntensity', 1.0); //physical clearcoat = get(material, 'clearcoat', 0.0); clearcoatMap = exportTexture(material, MAP.CLEARCOAT); clearcoatNormalMap = exportTexture(material, MAP.CLEARCOATNORMAL); cns = get(material, 'clearcoatNormalScale'); clearcoatNormalScale = (cns instanceof Vector2 ? cns : new Vector2(1, 1)).toArray(); clearcoatRoughness = get(material, 'clearcoatRoughness', 0.0); clearcoatRoughnessMap = exportTexture(material, MAP.CLEARCOATROUGHNESS); ior = get(material, 'ior', 1.5); reflectivity = get(material, 'reflectivity', 0.5); sheen = get(material, 'sheen', 0.0); sheenColor = getRGBColor(get(material, 'sheenColor', new Color(0xffffff))); sheenColorMap = exportTexture(material, MAP.SHEENCOLORMAP); sheenRoughness = get(material, 'sheenRoughness', 1.0); sheenRoughnessMap = exportTexture(material, MAP.SHEENROUGHNESSMAP); transmission = get(material, 'transmission', 0.0); transmissionMap = exportTexture(material, MAP.TRANSMISSION); specularIntensity = get(material, 'specularIntensity', 1.0); specularColor = getRGBColor(get(material, 'specularColor', new Color(0xffffff))); specularColorMap = exportTexture(material, MAP.SPECULARCOLORMAP); alphaMode = AlphaMode.OPAQUE; if ((material === null || material === void 0 ? void 0 : material.transparent) && material.depthWrite) { alphaMode = AlphaMode.OPAQUE; } else if ((material === null || material === void 0 ? void 0 : material.transparent) && !material.depthWrite) { alphaMode = AlphaMode.BLEND; } else if ((material === null || material === void 0 ? void 0 : material.alphaTest) > 0) { alphaMode = 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); }