All files / src/exporter/asset Unity.ts

96.36% Statements 53/55
85% Branches 17/20
94.12% Functions 16/17
96.08% Lines 49/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 1481x                     1x         1x 2x           3x             3x   3x 3x   3x 6x 6x 6x 3x 3x 3x 3x 3x 3x     6x             6x     6x       3x     6x               6x 2x     4x 4x                   3x               3x 1x 2x 3x 3x 3x           3x   1x 1x             1x 13x 13x 10x 10x 4x 4x                   1x         9x   8x 8x           1x  
import * as path from 'path';
import { SchemaJson, Node } from '@drecom/scene-graph-schema';
import AssetExporter from '../../interface/AssetExporter';
import AssetExporterPlugin from '../../interface/AssetExporterPlugin';
import AssetExportMapEntity from '../../interface/AssetExportMapEntity';
 
type ExportAssetIterator = (owner: any, key: string, path: string, parentAsset?: string) => void;
 
/**
 * Unity scene exporter
 */
export default class Unity implements AssetExporter {
 
  /**
   * Returns runtime identifier string.
   */
  public getIdentifier(): string {
    return 'unity';
  }
 
  /**
   * Create asset export map.
   */
  public createExportMap(
    sceneGraphMap: Map<string, SchemaJson>,
    assetRoot: string,
    destDir: string,
    urlNameSpace: string,
    plugins?: Map<string, AssetExporterPlugin>
  ): Map<string, AssetExportMapEntity> {
    const exportMap = new Map<string, AssetExportMapEntity>();
 
    sceneGraphMap.forEach((graph) => {
      const scene = graph.scene;
 
      scene.forEach((node) => {
        this.forEachExportingAsset(node, (_owner, _key, path, parentAsset) => {
          let movePath = '';
          if (parentAsset) {
            const assetPaths = path.split('/');
            Eif (assetPaths.length > 0) {
              const parentAssetPaths = parentAsset.split('/');
              parentAssetPaths.pop();
              parentAssetPaths.push(assetPaths.pop() as string);
              movePath = parentAssetPaths.join('/');
            }
          }
          const entity = this.createExportMapEntity(
            path,
            assetRoot,
            destDir,
            urlNameSpace,
            movePath
          );
          exportMap.set(path, entity);
        });
 
        this.pluginPostProcess(node, exportMap, assetRoot, destDir, urlNameSpace, plugins);
      });
    });
 
    return exportMap;
  }
 
  public pluginPostProcess(
    node: Node,
    exportMap: Map<string, AssetExportMapEntity>,
    assetRoot: string,
    destDir: string,
    urlNameSpace: string,
    plugins?: Map<string, AssetExporterPlugin>
  ): void {
    if (!plugins) {
      return;
    }
 
    plugins.forEach((plugin) => {
      plugin.getExportMapExtendPaths(node).forEach((path) => {
        const entity = this.createExportMapEntity(path, assetRoot, destDir, urlNameSpace);
        exportMap.set(path, entity);
      });
    });
  }
 
  /**
   * Replace paths in scene graph from absolute local path to relative path/url.
   */
  public replacePaths(
    sceneGraphMap: Map<string, SchemaJson>,
    exportMap: Map<string, AssetExportMapEntity>,
    plugins?: Map<string, AssetExporterPlugin>
  ): void {
    /**
     * replace local path with url
     */
    sceneGraphMap.forEach((sceneGraph) => {
      sceneGraph.scene.forEach((node) => {
        this.forEachExportingAsset(node, (owner, key, path) => {
          const entity = exportMap.get(path);
          Eif (entity) {
            owner[key] = entity.url;
          }
        });
      });
    });
 
    if (!plugins) return;
 
    plugins.forEach((plugin) => {
      plugin.replaceExtendedPaths(sceneGraphMap, exportMap);
    });
  }
 
  /**
   * iterate exporting assets
   */
  private forEachExportingAsset(node: Node, proc: ExportAssetIterator): void {
    Eif (node.meshRenderer) {
      if (node.meshRenderer.mesh) {
        proc(node.meshRenderer.mesh, 'url', node.meshRenderer.mesh.url);
        if (node.meshRenderer.materials) {
          node.meshRenderer.materials.forEach((material: any) => {
            proc(material, 'url', material.url, node.meshRenderer!.mesh!.url);
          });
        }
      }
    }
  }
 
  /**
   * Create asset export map entity.
   */
  private createExportMapEntity(
    basePath: string,
    assetRoot: string,
    destDir: string,
    urlNameSpace: string,
    movePath: string = ''
  ): AssetExportMapEntity {
    const destRelativePath = (movePath || basePath).replace(RegExp(`\^${assetRoot}`), '');
    return {
      localSrcPath: basePath,
      localDestPath: path.join(destDir, destRelativePath),
      url: path.join(urlNameSpace, destRelativePath)
    };
  }
}