Skip to content

Commit

Permalink
Merge pull request #93 from CyberAgentGameEntertainment/feature/add_t…
Browse files Browse the repository at this point in the history
…est_vertex_deformer

Add vertex deformation test
  • Loading branch information
CA-Tatami authored Dec 24, 2024
2 parents 73bc0ec + 8aae9da commit f740833
Show file tree
Hide file tree
Showing 17 changed files with 1,087,392 additions and 52 deletions.
88 changes: 38 additions & 50 deletions Assets/Tests/Runtime/AverageTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// --------------------------------------------------------------
// Copyright 2024 CyberAgent, Inc.
// --------------------------------------------------------------

using System;
using System.Collections;
using System.IO;
Expand All @@ -9,79 +13,72 @@
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools.Graphics;

using Object = UnityEngine.Object;

namespace Tests.Runtime
{
/// <summary>
/// アベレージテストを行うクラス
/// Jzazbz色空間でのピクセルの差分の平均値を使ってテストを行います。
/// アベレージテストを行うクラス
/// Jzazbz色空間でのピクセルの差分の平均値を使ってテストを行います。
/// </summary>
public class AverageTest
{
[TestCase("Test_Unlit", ExpectedResult = (IEnumerator)null)]
[TestCase("Test_Lit", ExpectedResult = (IEnumerator)null)]
[TestCase("Test_Distortion", ExpectedResult = (IEnumerator)null)]
[TestCase("Test_Unlit", ExpectedResult = null)]
[TestCase("Test_Lit", ExpectedResult = null)]
[TestCase("Test_Distortion", ExpectedResult = null)]
[TestCase("Test_Vertex_Deformation", ExpectedResult = null)]
[GameViewResolution(1920, 1080, "Full HD")]
public IEnumerator Test(string scenePath)
{
var asyncOp = EditorSceneManager.LoadSceneAsyncInPlayMode(
$"Assets/Tests/Scenes/{scenePath}.unity",
new LoadSceneParameters(LoadSceneMode.Single));
// シーンの読み込み待ち
while (!asyncOp.isDone)
{
yield return null;
}
while (!asyncOp.isDone) yield return null;
// タイムスケールを0に指定しても、バインドポーズになるときもあれば、
// 0フレームのアニメーションが再生されてしまうことがあり、テストが不安定だった。
// そこでシーンに含まれているアニメーターを無効にしてアニメーションが再生されないようにする。
var animators = GameObject.FindObjectsOfType<Animator>();
foreach (var animator in animators)
{
animator.enabled = false;
}

foreach (var animator in animators) animator.enabled = false;

// シーンのレンダリングが一回終わるまで待つ
yield return new WaitForEndOfFrame();

// 一回描画するとシェーダーの非同期コンパイルが走るので、コンパイルが終わるのを待つ
while (ShaderUtil.anythingCompiling)
{
yield return null;
}

while (ShaderUtil.anythingCompiling) yield return null;

// さらにシーンのレンダリングが一回終わるまで待ってスクリーンショットを撮る
yield return new WaitForEndOfFrame();
var screenshotSrc = ScreenCapture.CaptureScreenshotAsTexture();
var settings = new ImageComparisonSettings()

var settings = new ImageComparisonSettings
{
TargetWidth = Screen.width,
TargetHeight = Screen.height,
AverageCorrectnessThreshold = 0.005f,
PerPixelCorrectnessThreshold = 0.005f
};
var expected = ExpectedImage();

// 成功イメージのフォーマットに合わせて再作成する。
var screenshot = new Texture2D(expected.width, expected.height, expected.format, false);
screenshot.SetPixels(screenshotSrc.GetPixels());
screenshot.Apply();

ImageAssert.AreEqual(expected, screenshot, settings);
UnityEngine.Object.Destroy(screenshot);
UnityEngine.Object.Destroy(expected);
Object.Destroy(screenshot);
Object.Destroy(expected);
yield return null;
}

private Texture2D ExpectedImage()
{
Texture2D expected = null;
var expectedFile = TestContext.CurrentTestExecutionContext.CurrentTest.Name
.Replace('(', '_')
.Replace(')', '_')
.Replace("\"", "");

var dirName = Path.Combine("Assets/Tests/SuccessfulImages", TestUtils.GetCurrentTestResultsFolderPath());
var expectedPath = Path.GetFullPath(Path.Combine(
dirName,
Expand All @@ -96,52 +93,43 @@ private Texture2D ExpectedImage()
{
// ダミーのテクスチャを作成
expected = new Texture2D(Screen.width, Screen.height);
for (int x = 0; x < Screen.width; x++)
{
for(int y = 0; y < Screen.height; y++)
{
expected.SetPixel(x, y, Color.black);
}
}
for (var x = 0; x < Screen.width; x++)
for (var y = 0; y < Screen.height; y++)
expected.SetPixel(x, y, Color.black);
}

return expected;
}

[MenuItem("Window/Nava Shader/Test/Copy AverageTest Result")]
static void CopyResult()
private static void CopyResult()
{
string[] platforms = { @"WindowsEditor/Direct3D11", @"OSXEditor_AppleSilicon/Metal"};
foreach(var platform in platforms)
string[] platforms = { @"WindowsEditor/Direct3D11", @"OSXEditor_AppleSilicon/Metal" };
foreach (var platform in platforms)
{
// コピー元とコピー先のパスを定義
var sourceDirectory = $"Assets/ActualImages/Linear/{platform}/None";
var destinationDirectory = $"Assets/Tests/SuccessfulImages/Linear/{platform}/None";

if (!Directory.Exists(sourceDirectory))
{
continue;
}
if (!Directory.Exists(sourceDirectory)) continue;
// コピー先のディレクトリが存在しない場合は作成
if (!Directory.Exists(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
if (!Directory.Exists(destinationDirectory)) Directory.CreateDirectory(destinationDirectory);

// コピー元ディレクトリからファイルを取得し、条件に合うものをコピー
var files = Directory.EnumerateFiles(sourceDirectory, "*.png")
.Where(file => !file.EndsWith(".diff.png", StringComparison.OrdinalIgnoreCase)
.Where(file => !file.EndsWith(".diff.png", StringComparison.OrdinalIgnoreCase)
&& !file.EndsWith(".expected.png", StringComparison.OrdinalIgnoreCase));

foreach (var file in files)
{
// ファイル名を取得
string fileName = Path.GetFileName(file);
var fileName = Path.GetFileName(file);

// コピー先のパスを定義
string destFile = Path.Combine(destinationDirectory, fileName);
var destFile = Path.Combine(destinationDirectory, fileName);

// ファイルをコピー
File.Copy(file, destFile, overwrite: true);
File.Copy(file, destFile, true);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions Assets/Tests/Scenes/Materials/VertexDeformation.meta

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

Loading

0 comments on commit f740833

Please sign in to comment.