Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions package/com.unity.formats.usd/Tests/Common/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public abstract class BaseFixture
protected string ArtifactsDirectoryName => "Artifacts";
protected string ArtifactsDirectoryFullPath => Path.Combine(Application.dataPath, ArtifactsDirectoryName);
protected string ArtifactsDirectoryRelativePath => Path.Combine("Assets", ArtifactsDirectoryName);
protected string TestAssetDirectoryName => TestAssetData.Directory.FolderName;
protected string TestUsdAssetDirectoryRelativePath => Path.Combine("Packages", "com.unity.formats.usd", "Tests", "Common", "Data", TestAssetDirectoryName);

public string GetUnityScenePath(string sceneName = null)
{
Expand Down Expand Up @@ -60,7 +62,7 @@ public string GetUSDScenePath(string usdFileName = null)
return Path.Combine(ArtifactsDirectoryFullPath, usdFileName);
}

public string GetPrefabPath(string prefabName = null)
public string GetPrefabPath(string prefabName = null, bool resource = false)
{
if (string.IsNullOrEmpty(prefabName))
{
Expand All @@ -72,7 +74,16 @@ public string GetPrefabPath(string prefabName = null)
prefabName += ".prefab";
}

return Path.Combine(ArtifactsDirectoryRelativePath, prefabName);
return Path.Combine(ArtifactsDirectoryRelativePath, resource ? "Resources" : "", prefabName);
}

public string GetTestAssetPath(string fileName)
{
if (!fileName.EndsWith(TestAssetData.Extension.Usda))
{
fileName += TestAssetData.Extension.Usda;
}
return Path.GetFullPath(Path.Combine(TestUsdAssetDirectoryRelativePath, fileName));
}

public string CreateTmpUsdFile(string fileName = "tempUsd.usda")
Expand Down Expand Up @@ -117,14 +128,28 @@ public void CleanupTestArtifacts()
Directory.Delete(ArtifactsDirectoryFullPath, true);
}

if (File.Exists(ArtifactsDirectoryFullPath.TrimEnd('/') + ".meta"))
DeleteMetaFile(ArtifactsDirectoryFullPath);

#if UNITY_EDITOR
// TODO: If materialImportMode = MaterialImportMode.ImportPreviewSurface, it creates all the texture2d files on the root assets
// Figure out if the texture2ds can be set into a different location - such as our artifacts directory
foreach (var textureArtifactGUID in AssetDatabase.FindAssets("t:texture2D", new string[] { "Assets" }))
{
File.Delete(ArtifactsDirectoryFullPath.TrimEnd('/') + ".meta");
var textureFilePath = Path.GetFullPath(AssetDatabase.GUIDToAssetPath(textureArtifactGUID));
File.Delete(textureFilePath);
DeleteMetaFile(textureFilePath);
}

#if UNITY_EDITOR
AssetDatabase.Refresh();
#endif
}

private void DeleteMetaFile(string fullPath)
{
if (File.Exists(fullPath.TrimEnd('/') + ".meta"))
{
File.Delete(fullPath.TrimEnd('/') + ".meta");
}
}
}
}
8 changes: 8 additions & 0 deletions package/com.unity.formats.usd/Tests/Common/CustomAsserts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using UnityEngine;
using NUnit.Framework;

namespace Unity.Formats.USD.Tests
{
public static class ImportAssert
{
public static class Editor
{
enum ObjectTypeName
{
UsdPlayableAsset = 0,
GameObject = 1,
Material = 2,
UsdPrimSource = 3
}

public static void IsValidImport(Object[] usdAsObjects, int expectedGameObjectCount, int expectedPrimSourceCount, int expectedMaterialCount)
{
Assert.NotZero(usdAsObjects.Length);

bool playableAssetFound = false;
int gameObjectCount = 0;
int materialCount = 0;
int usdPrimSourceCount = 0;

foreach (Object childObject in usdAsObjects)
{
switch (childObject.GetType().Name)
{
case nameof(ObjectTypeName.UsdPlayableAsset):
playableAssetFound = true;
break;

case nameof(ObjectTypeName.GameObject):
gameObjectCount++;
break;

case nameof(ObjectTypeName.Material):
materialCount++;
break;

case nameof(ObjectTypeName.UsdPrimSource):
usdPrimSourceCount++;
break;

default:
break;
}
}

Assert.IsTrue(playableAssetFound, "No PlayableAssset was found in the prefab.");
Assert.AreEqual(expectedGameObjectCount, gameObjectCount, "Wrong GameObjects count in the prefab.");
Assert.AreEqual(expectedPrimSourceCount, usdPrimSourceCount, "Wrong USD Prim Source object in the prefab");
Assert.AreEqual(expectedMaterialCount, materialCount, "Wrong Materials count in the prefab");
}
}

public static void IsTextureDataSaved(GameObject usdObject, string fileName, bool isPrefab)
{
var materials = usdObject.transform.Find(TestAssetData.ImportGameObjectName.Material);
var rootPrim = usdObject.transform.Find(TestAssetData.ImportGameObjectName.RootPrim);

Assert.IsTrue(rootPrim.childCount == 1);
Assert.AreEqual(materials.childCount, rootPrim.childCount);

foreach (Transform child in materials)
{
// TODO: Not sure how to access "Prim Type"
Assert.IsNotNull(child.GetComponent<UsdPrimSource>());
}

var renderer = rootPrim.Find(fileName).GetComponent<MeshRenderer>();

Material[] allMaterials;
if (isPrefab)
{
allMaterials = renderer.sharedMaterials;
}
else
{
allMaterials = renderer.materials;
}

foreach (var material in allMaterials)
{
IsTextureFileMapped(fileName, material);
}
}

private static void IsTextureFileMapped(string fileName, Material material)
{
switch (fileName)
{
case TestAssetData.FileName.TexturedTransparent_Cutout:
{
Assert.AreEqual("Cutout", material.GetTag("RenderType", false));
Assert.AreEqual("textured_transparency", material.mainTexture.name);
Assert.AreEqual(1f, material.GetFloat("_Cutoff"));
break;
}
case TestAssetData.FileName.TexturedOpaque:
{
Assert.AreEqual("Opaque", material.GetTag("RenderType", false));
Assert.AreEqual("textured", material.mainTexture.name);
break;
}

default:
break;
}

Assert.AreEqual("textured_metallic.metalicRough", material.GetTexture("_MetallicGlossMap").name);
Assert.AreEqual("textured_normal", material.GetTexture("_BumpMap").name);
Assert.AreEqual("textured_emissive", material.GetTexture("_EmissionMap").name);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions package/com.unity.formats.usd/Tests/Common/Data.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#usda 1.0
(
"""
USDA 1.0 file created for Unity USD Import test
"""
defaultPrim = "RootPrim"
metersPerUnit = 1
upAxis = "Y"
)

def Xform "RootPrim"
{
def Mesh "TexturedOpaque"
{
int[] faceVertexCounts = [4, 4, 4, 4, 4]
int[] faceVertexIndices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
rel material:binding = </Material/TexturedOpaque>
normal3f[] normals = [(-1, 0, 0), (-1, 0, 0), (-1, 0, 0), (-1, 0, 0), (0, 0, -1), (0, 0, -1), (0, 0, -1), (0, 0, -1), (1, 0, 0), (1, 0, 0), (1, 0, 0), (1, 0, 0), (0, 1, 0), (0, 1, 0), (0, 1, 0), (0, 1, 0), (0, 0, 1), (0, 0, 1), (0, 0, 1), (0, 0, 1)]
point3f[] points = [(0, 1, -2), (0, 1, -1), (0, 2, -1), (0, 2, -2), (1, 1, -2), (0, 1, -2), (0, 2, -2), (1, 2, -2), (1, 1, -1), (1, 1, -2), (1, 2, -2), (1, 2, -1), (0, 2, -1), (1, 2, -1), (1, 2, -2), (0, 2, -2), (0, 1, -1), (1, 1, -1), (1, 2, -1), (0, 2, -1)]
texCoord2f[] primvars:st = [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0), (1, 0), (1, 1), (0, 1), (0, 0), (1, 0), (1, 1), (0, 1), (0, 0), (1, 0), (1, 1), (0, 1), (0, 0), (1, 0), (1, 1), (0, 1)] (
interpolation = "vertex"
)
}
}

def Scope "Material" (
kind = "model"
)
{
def Material "TexturedOpaque"
{
token outputs:displacement.connect = </Material/TexturedOpaque/PreviewSurface.outputs:displacement>
token outputs:surface.connect = </Material/TexturedOpaque/PreviewSurface.outputs:surface>

def Shader "PreviewSurface"
{
uniform token info:id = "UsdPreviewSurface"
color3f inputs:diffuseColor.connect = </Material/TexturedOpaque/main_texture.outputs:rgb>
float inputs:metallic.connect = </Material/TexturedOpaque/metallic_texture.outputs:r>
color3f inputs:emissiveColor.connect = </Material/TexturedOpaque/emissive_texture.outputs:rgb>
normal3f inputs:normal.connect = </Material/TexturedOpaque/normal_texture.outputs:rgb>
float inputs:opacity = 1
float inputs:roughness.connect = </Material/TexturedOpaque/roughness_texture.outputs:r>
int inputs:useSpecularWorkflow = 0
token outputs:out
token outputs:surface
}

def Shader "uv_reader"
{
uniform token info:id = "UsdPrimvarReader_float2"
float2 inputs:fallback = (0, 0)
token inputs:varname = "st"
float2 outputs:result
}

def Shader "roughness_texture"
{
uniform token info:id = "UsdUVTexture"
asset inputs:file = @./Textures/textured_rough.png@
token inputs:sourceColorSpace = "raw"
float2 inputs:st.connect = </Material/TexturedOpaque/uv_reader.outputs:result>
token inputs:wrapS = "repeat"
token inputs:wrapT = "repeat"
float outputs:r
}

def Shader "normal_texture"
{
uniform token info:id = "UsdUVTexture"
float4 inputs:bias = (-1, 1, -1, -1)
asset inputs:file = @./Textures/textured_normal.png@
float4 inputs:scale = (2, -2, 2, 2)
token inputs:sourceColorSpace = "raw"
float2 inputs:st.connect = </Material/TexturedOpaque/uv_reader.outputs:result>
token inputs:wrapS = "repeat"
token inputs:wrapT = "repeat"
float3 outputs:rgb
}

def Shader "main_texture"
{
uniform token info:id = "UsdUVTexture"
asset inputs:file = @./Textures/textured.png@
token inputs:sourceColorSpace = "sRGB"
float2 inputs:st.connect = </Material/TexturedOpaque/uv_reader.outputs:result>
token inputs:wrapS = "repeat"
token inputs:wrapT = "repeat"
color3f outputs:rgb
}

def Shader "metallic_texture"
{
uniform token info:id = "UsdUVTexture"
asset inputs:file = @./Textures/textured_metallic.png@
token inputs:sourceColorSpace = "raw"
float2 inputs:st.connect = </Material/TexturedOpaque/uv_reader.outputs:result>
token inputs:wrapS = "repeat"
token inputs:wrapT = "repeat"
float outputs:r
}

def Shader "emissive_texture"
{
uniform token info:id = "UsdUVTexture"
asset inputs:file = @./Textures/textured_emissive.png@
float4 inputs:scale = (1000, 1000, 1000, 1)
float2 inputs:st.connect = </Material/TexturedOpaque/uv_reader.outputs:result>
token inputs:wrapS = "repeat"
token inputs:wrapT = "repeat"
float3 outputs:rgb
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading