Skip to content

Commit 6bd5bd1

Browse files
authored
[USDU-230][USDU-232][USDU-275] Add testcases regarding texture mapping (#328)
* [USDU-230][USDU-232][USDU-275] Adds test cases regarding importing textured usd scenes * Change test case name to be more fitting * Adds missing .meta file * Changes usage of UnityEngine.Windows.Directory to System.IO.Directory * Removes usage of editor specific api calls and adds a define if when necessary * Adds ignore tag to HDRP required test case * Moves test files to a folder that better matches existing data structure * Implementing review suggestions: reverted changes to ImportHelper.cs script and reducing the sizes of test textures * Applying review suggestions * reverting usdz structure test as this PR is getting way too big
1 parent 19ea387 commit 6bd5bd1

27 files changed

+1082
-67
lines changed

package/com.unity.formats.usd/Tests/Common/BaseFixture.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public abstract class BaseFixture
2929
protected string ArtifactsDirectoryName => "Artifacts";
3030
protected string ArtifactsDirectoryFullPath => Path.Combine(Application.dataPath, ArtifactsDirectoryName);
3131
protected string ArtifactsDirectoryRelativePath => Path.Combine("Assets", ArtifactsDirectoryName);
32+
protected string TestAssetDirectoryName => TestAssetData.Directory.FolderName;
33+
protected string TestUsdAssetDirectoryRelativePath => Path.Combine("Packages", "com.unity.formats.usd", "Tests", "Common", "Data", TestAssetDirectoryName);
3234

3335
public string GetUnityScenePath(string sceneName = null)
3436
{
@@ -60,7 +62,7 @@ public string GetUSDScenePath(string usdFileName = null)
6062
return Path.Combine(ArtifactsDirectoryFullPath, usdFileName);
6163
}
6264

63-
public string GetPrefabPath(string prefabName = null)
65+
public string GetPrefabPath(string prefabName = null, bool resource = false)
6466
{
6567
if (string.IsNullOrEmpty(prefabName))
6668
{
@@ -72,7 +74,16 @@ public string GetPrefabPath(string prefabName = null)
7274
prefabName += ".prefab";
7375
}
7476

75-
return Path.Combine(ArtifactsDirectoryRelativePath, prefabName);
77+
return Path.Combine(ArtifactsDirectoryRelativePath, resource ? "Resources" : "", prefabName);
78+
}
79+
80+
public string GetTestAssetPath(string fileName)
81+
{
82+
if (!fileName.EndsWith(TestAssetData.Extension.Usda))
83+
{
84+
fileName += TestAssetData.Extension.Usda;
85+
}
86+
return Path.GetFullPath(Path.Combine(TestUsdAssetDirectoryRelativePath, fileName));
7687
}
7788

7889
public string CreateTmpUsdFile(string fileName = "tempUsd.usda")
@@ -117,14 +128,28 @@ public void CleanupTestArtifacts()
117128
Directory.Delete(ArtifactsDirectoryFullPath, true);
118129
}
119130

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

125-
#if UNITY_EDITOR
126143
AssetDatabase.Refresh();
127144
#endif
128145
}
146+
147+
private void DeleteMetaFile(string fullPath)
148+
{
149+
if (File.Exists(fullPath.TrimEnd('/') + ".meta"))
150+
{
151+
File.Delete(fullPath.TrimEnd('/') + ".meta");
152+
}
153+
}
129154
}
130155
}

package/com.unity.formats.usd/Tests/Common/CustomAsserts.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using UnityEngine;
2+
using NUnit.Framework;
3+
4+
namespace Unity.Formats.USD.Tests
5+
{
6+
public static class ImportAssert
7+
{
8+
public static class Editor
9+
{
10+
enum ObjectTypeName
11+
{
12+
UsdPlayableAsset = 0,
13+
GameObject = 1,
14+
Material = 2,
15+
UsdPrimSource = 3
16+
}
17+
18+
public static void IsValidImport(Object[] usdAsObjects, int expectedGameObjectCount, int expectedPrimSourceCount, int expectedMaterialCount)
19+
{
20+
Assert.NotZero(usdAsObjects.Length);
21+
22+
bool playableAssetFound = false;
23+
int gameObjectCount = 0;
24+
int materialCount = 0;
25+
int usdPrimSourceCount = 0;
26+
27+
foreach (Object childObject in usdAsObjects)
28+
{
29+
switch (childObject.GetType().Name)
30+
{
31+
case nameof(ObjectTypeName.UsdPlayableAsset):
32+
playableAssetFound = true;
33+
break;
34+
35+
case nameof(ObjectTypeName.GameObject):
36+
gameObjectCount++;
37+
break;
38+
39+
case nameof(ObjectTypeName.Material):
40+
materialCount++;
41+
break;
42+
43+
case nameof(ObjectTypeName.UsdPrimSource):
44+
usdPrimSourceCount++;
45+
break;
46+
47+
default:
48+
break;
49+
}
50+
}
51+
52+
Assert.IsTrue(playableAssetFound, "No PlayableAssset was found in the prefab.");
53+
Assert.AreEqual(expectedGameObjectCount, gameObjectCount, "Wrong GameObjects count in the prefab.");
54+
Assert.AreEqual(expectedPrimSourceCount, usdPrimSourceCount, "Wrong USD Prim Source object in the prefab");
55+
Assert.AreEqual(expectedMaterialCount, materialCount, "Wrong Materials count in the prefab");
56+
}
57+
}
58+
59+
public static void IsTextureDataSaved(GameObject usdObject, string fileName, bool isPrefab)
60+
{
61+
var materials = usdObject.transform.Find(TestAssetData.ImportGameObjectName.Material);
62+
var rootPrim = usdObject.transform.Find(TestAssetData.ImportGameObjectName.RootPrim);
63+
64+
Assert.IsTrue(rootPrim.childCount == 1);
65+
Assert.AreEqual(materials.childCount, rootPrim.childCount);
66+
67+
foreach (Transform child in materials)
68+
{
69+
// TODO: Not sure how to access "Prim Type"
70+
Assert.IsNotNull(child.GetComponent<UsdPrimSource>());
71+
}
72+
73+
var renderer = rootPrim.Find(fileName).GetComponent<MeshRenderer>();
74+
75+
Material[] allMaterials;
76+
if (isPrefab)
77+
{
78+
allMaterials = renderer.sharedMaterials;
79+
}
80+
else
81+
{
82+
allMaterials = renderer.materials;
83+
}
84+
85+
foreach (var material in allMaterials)
86+
{
87+
IsTextureFileMapped(fileName, material);
88+
}
89+
}
90+
91+
private static void IsTextureFileMapped(string fileName, Material material)
92+
{
93+
switch (fileName)
94+
{
95+
case TestAssetData.FileName.TexturedTransparent_Cutout:
96+
{
97+
Assert.AreEqual("Cutout", material.GetTag("RenderType", false));
98+
Assert.AreEqual("textured_transparency", material.mainTexture.name);
99+
Assert.AreEqual(1f, material.GetFloat("_Cutoff"));
100+
break;
101+
}
102+
case TestAssetData.FileName.TexturedOpaque:
103+
{
104+
Assert.AreEqual("Opaque", material.GetTag("RenderType", false));
105+
Assert.AreEqual("textured", material.mainTexture.name);
106+
break;
107+
}
108+
109+
default:
110+
break;
111+
}
112+
113+
Assert.AreEqual("textured_metallic.metalicRough", material.GetTexture("_MetallicGlossMap").name);
114+
Assert.AreEqual("textured_normal", material.GetTexture("_BumpMap").name);
115+
Assert.AreEqual("textured_emissive", material.GetTexture("_EmissionMap").name);
116+
}
117+
}
118+
}

package/com.unity.formats.usd/Tests/Common/CustomAsserts/ImportAssert.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/com.unity.formats.usd/Tests/Common/Data.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/com.unity.formats.usd/Tests/Common/Data/UsdImportTests.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#usda 1.0
2+
(
3+
"""
4+
USDA 1.0 file created for Unity USD Import test
5+
"""
6+
defaultPrim = "RootPrim"
7+
metersPerUnit = 1
8+
upAxis = "Y"
9+
)
10+
11+
def Xform "RootPrim"
12+
{
13+
def Mesh "TexturedOpaque"
14+
{
15+
int[] faceVertexCounts = [4, 4, 4, 4, 4]
16+
int[] faceVertexIndices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
17+
rel material:binding = </Material/TexturedOpaque>
18+
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)]
19+
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)]
20+
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)] (
21+
interpolation = "vertex"
22+
)
23+
}
24+
}
25+
26+
def Scope "Material" (
27+
kind = "model"
28+
)
29+
{
30+
def Material "TexturedOpaque"
31+
{
32+
token outputs:displacement.connect = </Material/TexturedOpaque/PreviewSurface.outputs:displacement>
33+
token outputs:surface.connect = </Material/TexturedOpaque/PreviewSurface.outputs:surface>
34+
35+
def Shader "PreviewSurface"
36+
{
37+
uniform token info:id = "UsdPreviewSurface"
38+
color3f inputs:diffuseColor.connect = </Material/TexturedOpaque/main_texture.outputs:rgb>
39+
float inputs:metallic.connect = </Material/TexturedOpaque/metallic_texture.outputs:r>
40+
color3f inputs:emissiveColor.connect = </Material/TexturedOpaque/emissive_texture.outputs:rgb>
41+
normal3f inputs:normal.connect = </Material/TexturedOpaque/normal_texture.outputs:rgb>
42+
float inputs:opacity = 1
43+
float inputs:roughness.connect = </Material/TexturedOpaque/roughness_texture.outputs:r>
44+
int inputs:useSpecularWorkflow = 0
45+
token outputs:out
46+
token outputs:surface
47+
}
48+
49+
def Shader "uv_reader"
50+
{
51+
uniform token info:id = "UsdPrimvarReader_float2"
52+
float2 inputs:fallback = (0, 0)
53+
token inputs:varname = "st"
54+
float2 outputs:result
55+
}
56+
57+
def Shader "roughness_texture"
58+
{
59+
uniform token info:id = "UsdUVTexture"
60+
asset inputs:file = @./Textures/textured_rough.png@
61+
token inputs:sourceColorSpace = "raw"
62+
float2 inputs:st.connect = </Material/TexturedOpaque/uv_reader.outputs:result>
63+
token inputs:wrapS = "repeat"
64+
token inputs:wrapT = "repeat"
65+
float outputs:r
66+
}
67+
68+
def Shader "normal_texture"
69+
{
70+
uniform token info:id = "UsdUVTexture"
71+
float4 inputs:bias = (-1, 1, -1, -1)
72+
asset inputs:file = @./Textures/textured_normal.png@
73+
float4 inputs:scale = (2, -2, 2, 2)
74+
token inputs:sourceColorSpace = "raw"
75+
float2 inputs:st.connect = </Material/TexturedOpaque/uv_reader.outputs:result>
76+
token inputs:wrapS = "repeat"
77+
token inputs:wrapT = "repeat"
78+
float3 outputs:rgb
79+
}
80+
81+
def Shader "main_texture"
82+
{
83+
uniform token info:id = "UsdUVTexture"
84+
asset inputs:file = @./Textures/textured.png@
85+
token inputs:sourceColorSpace = "sRGB"
86+
float2 inputs:st.connect = </Material/TexturedOpaque/uv_reader.outputs:result>
87+
token inputs:wrapS = "repeat"
88+
token inputs:wrapT = "repeat"
89+
color3f outputs:rgb
90+
}
91+
92+
def Shader "metallic_texture"
93+
{
94+
uniform token info:id = "UsdUVTexture"
95+
asset inputs:file = @./Textures/textured_metallic.png@
96+
token inputs:sourceColorSpace = "raw"
97+
float2 inputs:st.connect = </Material/TexturedOpaque/uv_reader.outputs:result>
98+
token inputs:wrapS = "repeat"
99+
token inputs:wrapT = "repeat"
100+
float outputs:r
101+
}
102+
103+
def Shader "emissive_texture"
104+
{
105+
uniform token info:id = "UsdUVTexture"
106+
asset inputs:file = @./Textures/textured_emissive.png@
107+
float4 inputs:scale = (1000, 1000, 1000, 1)
108+
float2 inputs:st.connect = </Material/TexturedOpaque/uv_reader.outputs:result>
109+
token inputs:wrapS = "repeat"
110+
token inputs:wrapT = "repeat"
111+
float3 outputs:rgb
112+
}
113+
}
114+
}

package/com.unity.formats.usd/Tests/Common/Data/UsdImportTests/TexturedOpaque.usda.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)