UNPKG

forge-svf-utils

Version:

Utilities for working with Autodesk Forge SVF file format.

116 lines (115 loc) 4.62 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const zlib = __importStar(require("zlib")); /** * Parses materials from a binary buffer, typically stored in a file called 'Materials.json.gz', * referenced in the SVF manifest as an asset of type 'ProteinMaterials'. * @generator * @param {Buffer} buffer Binary buffer to parse. * @returns {Iterable<IMaterial | null>} Instances of parsed materials, or null if there are none (or are not supported). */ function* parseMaterials(buffer) { if (buffer[0] === 31 && buffer[1] === 139) { buffer = zlib.gunzipSync(buffer); } if (buffer.byteLength > 0) { const json = JSON.parse(buffer.toString()); for (const key of Object.keys(json.materials)) { const group = json.materials[key]; const material = group.materials[group.userassets[0]]; switch (material.definition) { case 'SimplePhong': yield parseSimplePhongMaterial(group); break; default: console.warn('Unsupported material definition', material.definition); yield null; break; } } } } exports.parseMaterials = parseMaterials; function parseSimplePhongMaterial(group) { let result = {}; const material = group.materials[group.userassets[0]]; result.diffuse = parseColorProperty(material, 'generic_diffuse', [0, 0, 0, 1]); result.specular = parseColorProperty(material, 'generic_specular', [0, 0, 0, 1]); result.ambient = parseColorProperty(material, 'generic_ambient', [0, 0, 0, 1]); result.emissive = parseColorProperty(material, 'generic_emissive', [0, 0, 0, 1]); result.glossiness = parseScalarProperty(material, 'generic_glossiness', 30); result.reflectivity = parseScalarProperty(material, 'generic_reflectivity_at_0deg', 0); result.opacity = 1.0 - parseScalarProperty(material, 'generic_transparency', 0); result.metal = parseBooleanProperty(material, 'generic_is_metal', false); if (material.textures) { result.maps = {}; const diffuse = parseTextureProperty(material, group, 'generic_diffuse'); if (diffuse) { result.maps.diffuse = diffuse; } const specular = parseTextureProperty(material, group, 'generic_specular'); if (specular) { result.maps.specular = specular; } const alpha = parseTextureProperty(material, group, 'generic_alpha'); if (alpha) { result.maps.alpha = alpha; } const bump = parseTextureProperty(material, group, 'generic_bump'); if (bump) { if (parseBooleanProperty(material, 'generic_bump_is_normal', false)) { result.maps.normal = bump; } else { result.maps.bump = bump; } } } return result; } function parseBooleanProperty(material, prop, defaultValue) { if (material.properties.booleans && prop in material.properties.booleans) { return material.properties.booleans[prop]; } else { return defaultValue; } } function parseScalarProperty(material, prop, defaultValue) { if (material.properties.scalars && prop in material.properties.scalars) { return material.properties.scalars[prop].values[0]; } else { return defaultValue; } } function parseColorProperty(material, prop, defaultValue) { if (material.properties.colors && prop in material.properties.colors) { const color = material.properties.colors[prop].values[0]; return [color.r, color.g, color.b, color.a]; } else { return defaultValue; } } function parseTextureProperty(material, group, prop) { if (material.textures && prop in material.textures) { const connection = material.textures[prop].connections[0]; const texture = group.materials[connection]; if (texture && texture.properties.uris && 'unifiedbitmap_Bitmap' in texture.properties.uris) { const uri = texture.properties.uris['unifiedbitmap_Bitmap'].values[0]; if (uri) { // TODO: parse texture transforms return { uri }; } } } return null; }