浏览代码

Make MaterialGraphPreviewGenerator disposable

/main
Peter Bay Bastian 7 年前
当前提交
bdd1ae34
共有 2 个文件被更改,包括 66 次插入48 次删除
  1. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphPreviewGenerator.cs
  2. 106
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/ShaderGenerationTest.cs

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/MaterialGraphPreviewGenerator.cs


using System;
internal class MaterialGraphPreviewGenerator
internal class MaterialGraphPreviewGenerator : IDisposable
{
private PreviewRenderUtility m_PreviewUtility;
private static readonly Mesh[] s_Meshes = {null, null, null, null, null};

utility.Render(true, false);
return utility.EndPreview();
}
public void Dispose()
{
Reset();
}
}
}

106
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/ShaderGenerationTest.cs


get
{
var absoluteGraphsPath = s_Path.Aggregate(Application.dataPath, Path.Combine);
var filePaths = Directory.GetFiles(absoluteGraphsPath).Select(x => new FileInfo(x)).Where(x => x.Extension == ".ShaderGraph");
var filePaths = Directory.GetFiles(absoluteGraphsPath).Select(x => new FileInfo(x))
.Where(x => x.Extension == ".ShaderGraph");
{
name = p.Name,
info = p,
threshold = 0.02f
};
{
name = p.Name,
info = p,
threshold = 0.02f
};
}
}
}

// Generate the shader
List<PropertyGenerator.TextureInfo> configuredTextures;
var shaderString = graph.masterNode.GetFullShader(GenerationMode.ForReals, graph.name, out configuredTextures);
var shaderString =
graph.masterNode.GetFullShader(GenerationMode.ForReals, graph.name, out configuredTextures);
if (!File.Exists(textTemplateFilePath)) {
if (!File.Exists(textTemplateFilePath))
{
else {
else
{
var misMatchLocationResult = Path.Combine(failedPath, string.Format("{0}.{1}", file.Name, "shader"));
var misMatchLocationTemplate = Path.Combine(failedPath, string.Format("{0}.template.{1}", file.Name, "shader"));
var misMatchLocationResult =
Path.Combine(failedPath, string.Format("{0}.{1}", file.Name, "shader"));
var misMatchLocationTemplate =
Path.Combine(failedPath, string.Format("{0}.template.{1}", file.Name, "shader"));
File.WriteAllText(misMatchLocationResult, shaderString);
File.WriteAllText(misMatchLocationTemplate, textTemplate);
}

m_Shader = ShaderUtil.CreateShaderAsset(shaderString);
m_Shader.hideFlags = HideFlags.HideAndDontSave;
Assert.IsNotNull(m_Shader, "Shader Generation Failed");
//Assert.IsFalse(AbstractMaterialNodeUI.ShaderHasError(m_Shader), "Shader has error");
m_PreviewMaterial = new Material(m_Shader)

Assert.IsNotNull(m_PreviewMaterial, "preview material could not be created");
const int res = 256;
var generator = new MaterialGraphPreviewGenerator();
var rendered = generator.DoRenderPreview(m_PreviewMaterial, PreviewMode.Preview3D, new Rect(0, 0, res, res), 10) as RenderTexture;
using (var generator = new MaterialGraphPreviewGenerator())
{
var rendered =
generator.DoRenderPreview(m_PreviewMaterial, PreviewMode.Preview3D, new Rect(0, 0, res, res), 10) as
RenderTexture;
Assert.IsNotNull(rendered, "Render failed");
Assert.IsNotNull(rendered, "Render failed");
RenderTexture.active = rendered;
m_Captured = new Texture2D(rendered.width, rendered.height, TextureFormat.ARGB32, false);
m_Captured.ReadPixels(new Rect(0, 0, rendered.width, rendered.height), 0, 0);
RenderTexture.active = null; //can help avoid errors
RenderTexture.active = rendered;
m_Captured = new Texture2D(rendered.width, rendered.height, TextureFormat.ARGB32, false);
m_Captured.ReadPixels(new Rect(0, 0, rendered.width, rendered.height), 0, 0);
RenderTexture.active = null; //can help avoid errors
// find the reference image
var dumpFileLocation = Path.Combine(shaderTemplatePath, string.Format("{0}.{1}", file.Name, "png"));
if (!File.Exists(dumpFileLocation))
{
// no reference exists, create it
var generated = m_Captured.EncodeToPNG();
File.WriteAllBytes(dumpFileLocation, generated);
Assert.Fail("Image template file not found for {0}, creating it.", file);
}
// find the reference image
var dumpFileLocation = Path.Combine(shaderTemplatePath, string.Format("{0}.{1}", file.Name, "png"));
if (!File.Exists(dumpFileLocation))
{
// no reference exists, create it
var generated = m_Captured.EncodeToPNG();
File.WriteAllBytes(dumpFileLocation, generated);
Assert.Fail("Image template file not found for {0}, creating it.", file);
}
var template = File.ReadAllBytes(dumpFileLocation);
m_FromDisk = new Texture2D(2, 2);
m_FromDisk.LoadImage(template, false);
var template = File.ReadAllBytes(dumpFileLocation);
m_FromDisk = new Texture2D(2, 2);
m_FromDisk.LoadImage(template, false);
var areEqual = CompareTextures(m_FromDisk, m_Captured, testInfo.threshold);
var areEqual = CompareTextures(m_FromDisk, m_Captured, testInfo.threshold);
if (!areEqual)
{
var failedPath = Path.Combine(rootPath.ToString(), "Failed");
Directory.CreateDirectory(failedPath);
var misMatchLocationResult = Path.Combine(failedPath, string.Format("{0}.{1}", file.Name, "png"));
var misMatchLocationTemplate =
Path.Combine(failedPath, string.Format("{0}.template.{1}", file.Name, "png"));
var generated = m_Captured.EncodeToPNG();
File.WriteAllBytes(misMatchLocationResult, generated);
File.WriteAllBytes(misMatchLocationTemplate, template);
}
if (!areEqual)
{
var failedPath = Path.Combine(rootPath.ToString(), "Failed");
Directory.CreateDirectory(failedPath);
var misMatchLocationResult = Path.Combine(failedPath, string.Format("{0}.{1}", file.Name, "png"));
var misMatchLocationTemplate = Path.Combine(failedPath, string.Format("{0}.template.{1}", file.Name, "png"));
var generated = m_Captured.EncodeToPNG();
File.WriteAllBytes(misMatchLocationResult, generated);
File.WriteAllBytes(misMatchLocationTemplate, template);
Assert.IsTrue(areEqual, "Shader from graph {0}, did not match .template file.", file);
Assert.IsTrue(areEqual, "Shader from graph {0}, did not match .template file.", file);
private bool CompareTextures(Texture2D fromDisk, Texture2D captured, float threshold)
private bool CompareTextures(Texture2D fromDisk, Texture2D captured, float threshold)
if (fromDisk.width != captured.width
if (fromDisk.width != captured.width
|| fromDisk.height != captured.height)
return false;

int numberOfPixels = pixels1.Length;
float sumOfSquaredColorDistances = 0;
for (int i = 0; i < numberOfPixels; i++)
for (int i = 0; i < numberOfPixels; i++)
{
Color p1 = pixels1[i];
Color p2 = pixels2[i];

正在加载...
取消
保存