threepipe
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

JSONMaterialLoader.ts 1.4KB

123456789101112131415161718192021222324252627282930313233
  1. import {SimpleJSONLoader} from './SimpleJSONLoader'
  2. import {ThreeViewer} from '../../viewer'
  3. import {getEmptyMeta, SerializationMetaType, ThreeSerialization} from '../../utils/serialization'
  4. export class JSONMaterialLoader extends SimpleJSONLoader {
  5. viewer?: ThreeViewer
  6. async loadAsync(url: string, onProgress?: (event: ProgressEvent) => void): Promise<any> {
  7. if (!this.viewer) throw 'Viewer not set in JSONMaterialLoader.'
  8. const json = await super.loadAsync(url, onProgress) as any
  9. const meta: SerializationMetaType = getEmptyMeta()
  10. const json2 = {...json}
  11. if (json.images) {
  12. if (Array.isArray(json.images)) meta.images = Object.fromEntries(json.images.map((i: any) => [i.uuid, i]))
  13. else meta.images = json.images
  14. delete json2.images
  15. }
  16. if (json.textures) {
  17. if (Array.isArray(json.textures)) meta.textures = Object.fromEntries(json.textures.map((t: any) => [t.uuid, t]))
  18. else meta.textures = json.textures
  19. delete json2.textures
  20. }
  21. if (json.materials) {
  22. if (Array.isArray(json.materials)) meta.materials = Object.fromEntries(json.materials.map((m: any) => [m.uuid, m]))
  23. else meta.materials = json.materials
  24. delete json2.materials
  25. }
  26. const resources = await this.viewer.loadConfigResources(meta)
  27. return ThreeSerialization.Deserialize(json2, undefined, resources)
  28. }
  29. }