UNPKG

arcade-physics

Version:
59 lines 2.41 kB
"use strict"; /** * @author Richard Davey <rich@photonstorm.com> * @copyright 2020 Photon Storm Ltd. * @license {@link https://opensource.org/licenses/MIT|MIT License} */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const GetColor_1 = __importDefault(require("../../display/color/GetColor")); /** * Takes a Wavefront Material file and extracts the diffuse reflectivity of the named * materials, converts them to integer color values and returns them. * * This is used internally by the `addOBJ` and `addModel` methods, but is exposed for * public consumption as well. * * Note this only works with diffuse values, specified in the `Kd r g b` format, where * `g` and `b` are optional, but `r` is required. It does not support spectral rfl files, * or any other material statement (such as `Ka` or `Ks`) * * @method Phaser.Geom.Mesh.ParseObjMaterial * @since 3.50.0 * * @param {string} mtl - The OBJ MTL file as a raw string, i.e. loaded via `this.load.text`. * * @return {object} The parsed material colors, where each property of the object matches the material name. */ const ParseObjMaterial = mtl => { const output = {}; const lines = mtl.split('\n'); let currentMaterial = ''; for (let i = 0; i < lines.length; i++) { const line = lines[i].trim(); if (line.indexOf('#') === 0 || line === '') { continue; } const lineItems = line.replace(/\s\s+/g, ' ').trim().split(' '); switch (lineItems[0].toLowerCase()) { case 'newmtl': { currentMaterial = lineItems[1]; break; } // The diffuse reflectivity of the current material // Support r, [g], [b] format, where g and b are optional case 'kd': { const r = Math.floor(lineItems[1] * 255); const g = lineItems.length >= 2 ? Math.floor(lineItems[2] * 255) : r; const b = lineItems.length >= 3 ? Math.floor(lineItems[3] * 255) : r; output[currentMaterial] = (0, GetColor_1.default)(r, g, b); break; } } } return output; }; exports.default = ParseObjMaterial; //# sourceMappingURL=ParseObjMaterial.js.map