use gltfImage.uri as textureExternalObject in gltf import

This commit is contained in:
ousttrue
2021-03-08 14:20:18 +09:00
parent 01fc3ce78d
commit 3f05cd78d2
2 changed files with 37 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.Experimental.AssetImporters;
using UnityEngine;
@@ -27,6 +28,33 @@ namespace UniGLTF
AddSubAssets(context, loaded);
}
static IEnumerable<(string, UnityEngine.Object)> EnumerateExists(GltfParser parser, UnityPath dir)
{
foreach (var texParam in parser.EnumerateTextures())
{
switch (texParam.TextureType)
{
case GetTextureParam.METALLIC_GLOSS_PROP:
case GetTextureParam.OCCLUSION_PROP:
break;
default:
{
var gltfTexture = parser.GLTF.textures.First(y => y.name == texParam.Name);
var gltfImage = parser.GLTF.images[gltfTexture.source];
if (!string.IsNullOrEmpty(gltfImage.uri))
{
var child = dir.Child(gltfImage.uri);
var asset = AssetDatabase.LoadAssetAtPath<Texture2D>(child.Value);
// Debug.Log($"exists: {child}: {asset}");
yield return (asset.name, asset);
}
}
break;
}
}
}
/// <summary>
/// Parse して、UnityObject 化する。
///
@@ -47,7 +75,8 @@ namespace UniGLTF
//
// Import(create unity objects)
//
var context = new ImporterContext(parser, null, externalObjectMap);
var context = new ImporterContext(parser, null, externalObjectMap.Concat(
EnumerateExists(parser, UnityPath.FromUnityPath(assetPath).Parent)));
context.InvertAxis = reverseAxis;
context.Load();
context.ShowMeshes();

View File

@@ -229,10 +229,16 @@ namespace UniGLTF
for (int i = 0; i < GLTF.textures.Count; ++i)
{
var gltfTexture = GLTF.textures[i];
var gltfImage = GLTF.images[gltfTexture.source];
if (!string.IsNullOrEmpty(gltfImage.uri))
{
gltfTexture.name = Path.GetFileNameWithoutExtension(gltfImage.uri);
}
if (string.IsNullOrEmpty(gltfTexture.name))
{
// use image name
gltfTexture.name = GLTF.images[gltfTexture.source].name;
gltfTexture.name = gltfImage.name;
}
if (string.IsNullOrEmpty(gltfTexture.name))
{