(jsonDoc: JSONDocument, _options: ReaderOptions = DEFAULT_OPTIONS)
| 40 | /** @internal */ |
| 41 | export class GLTFReader { |
| 42 | public static read(jsonDoc: JSONDocument, _options: ReaderOptions = DEFAULT_OPTIONS): Document { |
| 43 | const options = { ...DEFAULT_OPTIONS, ..._options } as Required<ReaderOptions>; |
| 44 | const { json } = jsonDoc; |
| 45 | const document = new Document().setLogger(options.logger); |
| 46 | |
| 47 | this.validate(jsonDoc, options); |
| 48 | |
| 49 | /* Reader context. */ |
| 50 | |
| 51 | const context = new ReaderContext(jsonDoc); |
| 52 | |
| 53 | /** Asset. */ |
| 54 | |
| 55 | const assetDef = json.asset; |
| 56 | const asset = document.getRoot().getAsset(); |
| 57 | |
| 58 | if (assetDef.copyright) asset.copyright = assetDef.copyright; |
| 59 | if (assetDef.extras) asset.extras = assetDef.extras; |
| 60 | |
| 61 | if (json.extras !== undefined) { |
| 62 | document.getRoot().setExtras({ ...json.extras }); |
| 63 | } |
| 64 | |
| 65 | /** Extensions (1/2). */ |
| 66 | |
| 67 | const extensionsUsed = json.extensionsUsed || []; |
| 68 | const extensionsRequired = json.extensionsRequired || []; |
| 69 | |
| 70 | options.extensions.sort((a, b) => (a.EXTENSION_NAME > b.EXTENSION_NAME ? 1 : -1)); |
| 71 | |
| 72 | for (const Extension of options.extensions) { |
| 73 | if (extensionsUsed.includes(Extension.EXTENSION_NAME)) { |
| 74 | // Create extension. |
| 75 | const extension = document |
| 76 | .createExtension(Extension as unknown as new (doc: Document) => Extension) |
| 77 | .setRequired(extensionsRequired.includes(Extension.EXTENSION_NAME)); |
| 78 | |
| 79 | // Warn on unsupported preread hooks. |
| 80 | const unsupportedHooks = extension.prereadTypes.filter((type) => !SUPPORTED_PREREAD_TYPES.has(type)); |
| 81 | if (unsupportedHooks.length) { |
| 82 | options.logger.warn( |
| 83 | `Preread hooks for some types (${unsupportedHooks.join()}), requested by extension ` + |
| 84 | `${extension.extensionName}, are unsupported. Please file an issue or a PR.`, |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | // Install dependencies. |
| 89 | for (const key of extension.readDependencies) { |
| 90 | extension.install(key, options.dependencies[key]); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** Buffers. */ |
| 96 | |
| 97 | const bufferDefs = json.buffers || []; |
| 98 | document |
| 99 | .getRoot() |
no test coverage detected