hpg-r3f-model-scene
Version:
Pretty Footer react component used in ant.design
237 lines • 8.52 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { get, set } from 'lodash';
import { LinearFilter, LinearMipmapLinearFilter, RGBAFormat, RepeatWrapping } from 'three';
import { TextureLoader } from 'three';
import { MeshPhysicalMaterial, Color, MeshMatcapMaterial } from 'three';
import { KTX2Loader } from 'three-stdlib';
export 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;
}
};
export function getRGBColor(color) {
if (color instanceof Color) {
if (color.equals(new Color(0xffffff))) {
return 'ffffff';
}
return color.clone().getHexString();
}
return new Color(0xffffff).getHexString();
}
function getMatcapColor(color) {
if (color instanceof Color) {
return color.clone().getHexString();
}
return new Color(0xffffff).getHexString();
}
export 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 = 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 = get(mat, "".concat(type, ".userData.basis"), null);
if (basis) {
_fullUrl = get(mat, "".concat(type, ".userData.url"), null);
} else {
_fullUrl = get(mat, "".concat(type, ".source.data.currentSrc"), null);
}
var fullUrl = removeOssExt(_fullUrl);
set(inode, "material.".concat(alias), {
name: texture.name,
fullUrl: fullUrl,
basis: basis,
parameter: parameter
});
};
if (mat instanceof 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: get(mat, "specularIntensity", 1.0),
specularColor: '#' + getRGBColor(mat.specularColor),
sheen: get(mat, "sheen", 0),
sheenRoughness: 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 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 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;
};
export var textureLoader = new TextureLoader();
textureLoader.setCrossOrigin('anonymous');
export function loadTexture(_x) {
return _loadTexture.apply(this, arguments);
}
function _loadTexture() {
_loadTexture = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(url) {
return _regeneratorRuntime.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);
}
export var ktx2Loader = new KTX2Loader();
export function loadKTX2Loader(_x2) {
return _loadKTX2Loader.apply(this, arguments);
}
function _loadKTX2Loader() {
_loadKTX2Loader = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(url) {
return _regeneratorRuntime.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);
}
export function exportTexture(material, textureName) {
if (!(material instanceof MeshPhysicalMaterial)) {
return;
}
var currentSrc = '';
var basis = get(material, "".concat(textureName, ".userData.basis"), null);
if (basis) {
currentSrc = basis;
} else {
currentSrc = get(material, "".concat(textureName, ".image.currentSrc")) || '';
}
return {
url: removeOssExt(currentSrc) || '',
format: get(material, "".concat(textureName, ".format"), null) || RGBAFormat,
minFilter: get(material, "".concat(textureName, ".minFilter"), null) || LinearMipmapLinearFilter,
magFilter: get(material, "".concat(textureName, ".magFilter"), null) || LinearFilter,
wrapS: get(material, "".concat(textureName, ".wrapS"), null) || RepeatWrapping,
wrapT: get(material, "".concat(textureName, ".wrapT"), null) || RepeatWrapping,
rotation: get(material, "".concat(textureName, ".rotation"), null) || 0,
offset: get(material, "".concat(textureName, ".offset"), null) ? [get(material, "".concat(textureName, ".offset.x"), null) || 0, get(material, "".concat(textureName, ".offset.y"), null) || 0] : null,
repeat: get(material, "".concat(textureName, ".repeat"), null) ? [get(material, "".concat(textureName, ".repeat.x"), null) || 1, get(material, "".concat(textureName, ".repeat.y"), null) || 1] : null,
userData: get(material, "".concat(textureName, ".userData"), null) || {}
};
}