1 line
17 KiB
Plaintext
1 line
17 KiB
Plaintext
|
|
{"version":3,"file":"MTLLoader.cjs","sources":["../../src/loaders/MTLLoader.js"],"sourcesContent":["import {\n Color,\n DefaultLoadingManager,\n FileLoader,\n FrontSide,\n Loader,\n LoaderUtils,\n MeshPhongMaterial,\n RepeatWrapping,\n TextureLoader,\n Vector2,\n} from 'three'\n\n/**\n * Loads a Wavefront .mtl file specifying materials\n */\n\nclass MTLLoader extends Loader {\n constructor(manager) {\n super(manager)\n }\n\n /**\n * Loads and parses a MTL asset from a URL.\n *\n * @param {String} url - URL to the MTL file.\n * @param {Function} [onLoad] - Callback invoked with the loaded object.\n * @param {Function} [onProgress] - Callback for download progress.\n * @param {Function} [onError] - Callback for download errors.\n *\n * @see setPath setResourcePath\n *\n * @note In order for relative texture references to resolve correctly\n * you must call setResourcePath() explicitly prior to load.\n */\n load(url, onLoad, onProgress, onError) {\n const scope = this\n\n const path = this.path === '' ? LoaderUtils.extractUrlBase(url) : this.path\n\n const loader = new FileLoader(this.manager)\n loader.setPath(this.path)\n loader.setRequestHeader(this.requestHeader)\n loader.setWithCredentials(this.withCredentials)\n loader.load(\n url,\n function (text) {\n try {\n onLoad(scope.parse(text, path))\n } catch (e) {\n if (onError) {\n onError(e)\n } else {\n console.error(e)\n }\n\n scope.manager.itemError(url)\n }\n },\n onProgress,\n onError,\n )\n }\n\n setMaterialOptions(value) {\n this.materialOptions = value\n return this\n }\n\n /**\n * Parses a MTL file.\n *\n * @param {String} text - Content of MTL file\n * @return {MaterialCreator}\n *\n * @see setPath setResourcePath\n *\n * @note In order for relative texture references to resolve correctly\n * you must call setResourcePath() explicitly prior to parse.\n */\n parse(text, path) {\n const lines = text.split('\\n')\n let info = {}\n const delimiter_pattern = /\\s+/\n const materialsInfo = {}\n\n for (let i = 0; i < lines.length; i++) {\n let line = lines[i]\n line = line.trim()\n\n if (line.length === 0 || line.charAt(0) === '#') {\n // Blank line or comment ignore\n continue\n }\n\n const pos = line.indexOf(' ')\n\n let key = pos >= 0 ? line.substring(0, pos) : line\n key = key.toLowerCase()\n\n let value = pos >= 0 ? line.substring(pos + 1) : ''\n value = value.trim()\n\n if (key === 'newmtl') {\n // New material\n\n info = { name: value }\n materialsInfo[value] = info\n } else {\n if (key === 'ka' || key === 'kd' || key === 'ks' || key === 'ke') {\n const ss = value.split(delimiter_pattern, 3)\n info[key] = [parseFloat(ss[0]), parseFloat(ss[1]), parseFloat(ss[2])]\n } else {\n info[key] = value\n }\n }\n }\n\n const materialCreator = new MaterialCreator(this.resourcePath || path, this.materialOptions)\n materialCreator.setCrossOrigin(this.crossOrigin)\n materialCreator.setManager(this.manager)\n materialCreator.setMaterials(materialsInfo)\n return materialCreator\n }\n}\n\n/**\n * Create a new MTLLoader.MaterialCreator\n * @param baseUrl - Url relative to which textures are loaded\n * @param options - Set of options on how to construct the materials\n * side: Which side to apply the material\n * FrontSide (default), THREE.BackSide, THREE.DoubleSide\n * wrap: What type of wrapping to apply for textures\n * RepeatWrapping (default), THREE.ClampToEdgeWrapping, THREE.MirroredRepeatWrapping\n * normalizeRGB: RGBs need to be normalized to 0-1 from 0-255\n * Default: false, assumed to be already normalized\n * ignoreZeroRGBs: Ignore values of RGBs (K
|