浏览代码

[Material graph]More testing + fix things that are broken with the tests.

/main
Tim Cooper 8 年前
当前提交
5055d67e
共有 7 个文件被更改,包括 645 次插入40 次删除
  1. 305
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PropertyNodeTests.cs
  2. 133
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/ShaderGeneratorTests.cs
  3. 38
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/PixelGraph.cs
  4. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/ColorNode.cs
  5. 41
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs
  6. 103
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/PropertyNodeTests.cs
  7. 59
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PropertyGeneratorTests.cs

305
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PropertyNodeTests.cs


var expected = m_Vector1Node.precision
+ " "
+ m_Vector1Node.propertyName
+";"
+ ";"
Assert.AreEqual(expected, generator.GetShaderString(0));
}
[Test]
public void TestVector2NodeTypeIsCorrect()
{
Assert.AreEqual(PropertyType.Vector2, m_Vector2Node.propertyType);
}
[Test]
public void TestVector2NodeReturnsCorrectValue()
{
var value = new Vector2(0.6f, 0.7f);
m_Vector2Node.value = value;
Assert.AreEqual(value, m_Vector2Node.value);
}
[Test]
public void TestVector2NodeReturnsPreviewProperty()
{
var value = new Vector2(0.6f, 0.7f);
var props = new List<PreviewProperty>();
m_Vector2Node.value = value;
m_Vector2Node.CollectPreviewMaterialProperties(props);
Assert.AreEqual(props.Count, 1);
Assert.AreEqual(m_Vector2Node.propertyName, props[0].m_Name);
Assert.AreEqual(m_Vector2Node.propertyType, props[0].m_PropType);
Assert.AreEqual(value, m_Vector2Node.value);
}
[Test]
public void TestVector2NodeGeneratesCorrectPropertyBlock()
{
var value = new Vector2(0.6f, 0.7f);
m_Vector2Node.value = value;
m_Vector2Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new PropertyGenerator();
m_Vector2Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector2Node.propertyName
+ "(\""
+ m_Vector2Node.description
+ "\", Vector) = ("
+ m_Vector2Node.value.x
+ ","
+ m_Vector2Node.value.y
+ ",0,0)"
+ Environment.NewLine;
m_Vector2Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector2Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(expected, generator.GetShaderString(0));
}
[Test]
public void TestVector2NodeGeneratesCorrectPropertyUsages()
{
var value = new Vector2(0.6f, 0.7f);
m_Vector2Node.value = value;
m_Vector2Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new ShaderGenerator();
m_Vector2Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector2Node.precision
+ "2 "
+ m_Vector2Node.propertyName
+ ";"
+ Environment.NewLine;
m_Vector2Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector2Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(expected, generator.GetShaderString(0));
}
[Test]
public void TestVector3NodeTypeIsCorrect()
{
Assert.AreEqual(PropertyType.Vector3, m_Vector3Node.propertyType);
}
[Test]
public void TestVector3NodeReturnsCorrectValue()
{
var value = new Vector3(0.6f, 0.7f, 0.4f);
m_Vector3Node.value = value;
Assert.AreEqual(value, m_Vector3Node.value);
}
[Test]
public void TestVector3NodeReturnsPreviewProperty()
{
var value = new Vector3(0.6f, 0.7f, 0.4f);
var props = new List<PreviewProperty>();
m_Vector3Node.value = value;
m_Vector3Node.CollectPreviewMaterialProperties(props);
Assert.AreEqual(props.Count, 1);
Assert.AreEqual(m_Vector3Node.propertyName, props[0].m_Name);
Assert.AreEqual(m_Vector3Node.propertyType, props[0].m_PropType);
Assert.AreEqual(value, m_Vector3Node.value);
}
[Test]
public void TestVector3NodeGeneratesCorrectPropertyBlock()
{
var value = new Vector3(0.6f, 0.7f, 0.4f);
m_Vector3Node.value = value;
m_Vector3Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new PropertyGenerator();
m_Vector3Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector3Node.propertyName
+ "(\""
+ m_Vector3Node.description
+ "\", Vector) = ("
+ m_Vector3Node.value.x
+ ","
+ m_Vector3Node.value.y
+ ","
+ m_Vector3Node.value.z
+ ",0)"
+ Environment.NewLine;
m_Vector3Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector3Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(expected, generator.GetShaderString(0));
}
[Test]
public void TestVector3NodeGeneratesCorrectPropertyUsages()
{
var value = new Vector3(0.6f, 0.7f, 0.4f);
m_Vector3Node.value = value;
m_Vector3Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new ShaderGenerator();
m_Vector3Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector3Node.precision
+ "3 "
+ m_Vector3Node.propertyName
+ ";"
+ Environment.NewLine;
m_Vector3Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector3Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(expected, generator.GetShaderString(0));
}
[Test]
public void TestVector4NodeTypeIsCorrect()
{
Assert.AreEqual(PropertyType.Vector4, m_Vector4Node.propertyType);
}
[Test]
public void TestVector4NodeReturnsCorrectValue()
{
var value = new Vector4(0.6f, 0.7f, 0.4f, 0.3f);
m_Vector4Node.value = value;
Assert.AreEqual(value, m_Vector4Node.value);
}
[Test]
public void TestVector4NodeReturnsPreviewProperty()
{
var value = new Vector4(0.6f, 0.7f, 0.4f, 0.3f);
var props = new List<PreviewProperty>();
m_Vector4Node.value = value;
m_Vector4Node.CollectPreviewMaterialProperties(props);
Assert.AreEqual(props.Count, 1);
Assert.AreEqual(m_Vector4Node.propertyName, props[0].m_Name);
Assert.AreEqual(m_Vector4Node.propertyType, props[0].m_PropType);
Assert.AreEqual(value, m_Vector4Node.value);
}
[Test]
public void TestVector4NodeGeneratesCorrectPropertyBlock()
{
var value = new Vector4(0.6f, 0.7f, 0.4f, 0.3f);
m_Vector4Node.value = value;
m_Vector4Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new PropertyGenerator();
m_Vector4Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector4Node.propertyName
+ "(\""
+ m_Vector4Node.description
+ "\", Vector) = ("
+ m_Vector4Node.value.x
+ ","
+ m_Vector4Node.value.y
+ ","
+ m_Vector4Node.value.z
+ ","
+ m_Vector4Node.value.w
+ ")"
+ Environment.NewLine;
m_Vector4Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector4Node.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(expected, generator.GetShaderString(0));
}
[Test]
public void TestVector4NodeGeneratesCorrectPropertyUsages()
{
var value = new Vector4(0.6f, 0.7f, 0.4f, 0.3f);
m_Vector4Node.value = value;
m_Vector4Node.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new ShaderGenerator();
m_Vector4Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_Vector4Node.precision
+ "4 "
+ m_Vector4Node.propertyName
+ ";"
+ Environment.NewLine;
m_Vector4Node.exposedState = PropertyNode.ExposedState.Exposed;
m_Vector4Node.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(expected, generator.GetShaderString(0));
}
[Test]
public void TestColorNodeTypeIsCorrect()
{
Assert.AreEqual(PropertyType.Color, m_ColorNode.propertyType);
}
[Test]
public void TestColorNodeReturnsCorrectValue()
{
var value = new Color(0.6f, 0.7f, 0.4f, 0.3f);
m_ColorNode.color = value;
Assert.AreEqual(value, m_ColorNode.color);
}
[Test]
public void TestColorNodeReturnsPreviewProperty()
{
var value = new Color(0.6f, 0.7f, 0.4f, 0.3f);
var props = new List<PreviewProperty>();
m_ColorNode.color = value;
m_ColorNode.CollectPreviewMaterialProperties(props);
Assert.AreEqual(props.Count, 1);
Assert.AreEqual(m_ColorNode.propertyName, props[0].m_Name);
Assert.AreEqual(m_ColorNode.propertyType, props[0].m_PropType);
Assert.AreEqual(value, m_ColorNode.color);
}
[Test]
public void TestColorNodeGeneratesCorrectPropertyBlock()
{
var value = new Color(0.6f, 0.7f, 0.4f, 0.3f);
m_ColorNode.color = value;
m_ColorNode.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new PropertyGenerator();
m_ColorNode.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_ColorNode.propertyName
+ "(\""
+ m_ColorNode.description
+ "\", Color) = ("
+ m_ColorNode.color.r
+ ","
+ m_ColorNode.color.g
+ ","
+ m_ColorNode.color.b
+ ","
+ m_ColorNode.color.a
+ ")"
+ Environment.NewLine;
m_ColorNode.exposedState = PropertyNode.ExposedState.Exposed;
m_ColorNode.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(expected, generator.GetShaderString(0));
}
[Test]
public void TestColorNodeGeneratesCorrectPropertyUsages()
{
var value = new Color(0.6f, 0.7f, 0.4f, 0.3f);
m_ColorNode.color = value;
m_ColorNode.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new ShaderGenerator();
m_ColorNode.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_ColorNode.precision
+ "4 "
+ m_ColorNode.propertyName
+ ";"
+ Environment.NewLine;
m_ColorNode.exposedState = PropertyNode.ExposedState.Exposed;
m_ColorNode.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(expected, generator.GetShaderString(0));
}
}

133
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/ShaderGeneratorTests.cs


Assert.AreEqual(string.Format("{0}", node.GetVariableNameForSlot(TestNode.V4Out)), result);
}
[Test]
public void AdaptNodeOutput1To1PreviewWorks()
{
var node = new TestNode();
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V1Out, ConcreteSlotValueType.Vector1);
Assert.AreEqual(string.Format("{0}", node.GetVariableNameForSlot(TestNode.V1Out)), result);
}
[Test]
public void AdaptNodeOutput1To2PreviewWorks()
{
var node = new TestNode();
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V1Out, ConcreteSlotValueType.Vector2);
Assert.AreEqual(string.Format("({0})", node.GetVariableNameForSlot(TestNode.V1Out)), result);
}
[Test]
public void AdaptNodeOutput1To3PreviewWorks()
{
var node = new TestNode();
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V1Out, ConcreteSlotValueType.Vector3);
Assert.AreEqual(string.Format("({0})", node.GetVariableNameForSlot(TestNode.V1Out)), result);
}
[Test]
public void AdaptNodeOutput1To4PreviewWorks()
{
var node = new TestNode();
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V1Out, ConcreteSlotValueType.Vector4);
Assert.AreEqual(string.Format("({0})", node.GetVariableNameForSlot(TestNode.V1Out)), result);
}
[Test]
public void AdaptNodeOutput2To1PreviewWorks()
{
var node = new TestNode();
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V2Out, ConcreteSlotValueType.Vector1);
Assert.AreEqual(string.Format("({0}).x", node.GetVariableNameForSlot(TestNode.V2Out)), result);
}
[Test]
public void AdaptNodeOutput2To2PreviewWorks()
{
var node = new TestNode();
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V2Out, ConcreteSlotValueType.Vector2);
Assert.AreEqual(string.Format("{0}", node.GetVariableNameForSlot(TestNode.V2Out)), result);
}
[Test]
public void AdaptNodeOutput2To3PreviewWorks()
{
var node = new TestNode();
var expected = string.Format("half3({0}.x, {0}.y, 0.0)", node.GetVariableNameForSlot(TestNode.V2Out));
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V2Out, ConcreteSlotValueType.Vector3);
Assert.AreEqual(expected, result);
}
[Test]
public void AdaptNodeOutput2To4PreviewWorks()
{
var node = new TestNode();
var expected = string.Format("half4({0}.x, {0}.y, 0.0, 0.0)", node.GetVariableNameForSlot(TestNode.V2Out));
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V2Out, ConcreteSlotValueType.Vector4);
Assert.AreEqual(expected, result);
}
[Test]
public void AdaptNodeOutput3To1PreviewWorks()
{
var node = new TestNode();
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V3Out, ConcreteSlotValueType.Vector1);
Assert.AreEqual(string.Format("({0}).x", node.GetVariableNameForSlot(TestNode.V3Out)), result);
}
[Test]
public void AdaptNodeOutput3To2PreviewWorks()
{
var node = new TestNode();
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V3Out, ConcreteSlotValueType.Vector2);
Assert.AreEqual(string.Format("({0}.xy)", node.GetVariableNameForSlot(TestNode.V3Out)), result);
}
[Test]
public void AdaptNodeOutput3To3PreviewWorks()
{
var node = new TestNode();
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V3Out, ConcreteSlotValueType.Vector3);
Assert.AreEqual(string.Format("{0}", node.GetVariableNameForSlot(TestNode.V3Out)), result);
}
[Test]
public void AdaptNodeOutput3To4PreviewWorks()
{
var node = new TestNode();
var expected = string.Format("half4({0}.x, {0}.y, {0}.z, 0.0)", node.GetVariableNameForSlot(TestNode.V3Out));
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V3Out, ConcreteSlotValueType.Vector4);
Assert.AreEqual(expected, result);
}
[Test]
public void AdaptNodeOutput4To1PreviewWorks()
{
var node = new TestNode();
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V4Out, ConcreteSlotValueType.Vector1);
Assert.AreEqual(string.Format("({0}).x", node.GetVariableNameForSlot(TestNode.V4Out)), result);
}
[Test]
public void AdaptNodeOutput4To2PreviewWorks()
{
var node = new TestNode();
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V4Out, ConcreteSlotValueType.Vector2);
Assert.AreEqual(string.Format("({0}.xy)", node.GetVariableNameForSlot(TestNode.V4Out)), result);
}
[Test]
public void AdaptNodeOutput4To3PreviewWorks()
{
var node = new TestNode();
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V4Out, ConcreteSlotValueType.Vector3);
Assert.AreEqual(string.Format("({0}.xyz)", node.GetVariableNameForSlot(TestNode.V4Out)), result);
}
[Test]
public void AdaptNodeOutput4To4PreviewWorks()
{
var node = new TestNode();
var result = ShaderGenerator.AdaptNodeOutputForPreview(node, TestNode.V4Out, ConcreteSlotValueType.Vector4);
Assert.AreEqual(string.Format("{0}", node.GetVariableNameForSlot(TestNode.V4Out)), result);
}
}

38
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Graphs/PixelGraph.cs


base.AddNode(node);
}
public static void GenerateSurfaceShader(
PixelShaderNode pixelNode,
ShaderGenerator shaderBody,
ShaderGenerator inputStruct,
ShaderGenerator lightFunction,
ShaderGenerator surfaceOutput,
ShaderGenerator nodeFunction,
PropertyGenerator shaderProperties,
ShaderGenerator propertyUsages,
ShaderGenerator vertexShader,
bool isPreview)
{
pixelNode.GenerateLightFunction(lightFunction);
pixelNode.GenerateSurfaceOutput(surfaceOutput);
var genMode = isPreview ? GenerationMode.Preview3D : GenerationMode.SurfaceShader;
var activeNodes = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodes, pixelNode);
var activeMaterialNodes = activeNodes.OfType<AbstractMaterialNode>();
foreach (var node in activeMaterialNodes)
{
if (node is IGeneratesFunction) (node as IGeneratesFunction).GenerateNodeFunction(nodeFunction, genMode);
if (node is IGeneratesVertexToFragmentBlock) (node as IGeneratesVertexToFragmentBlock).GenerateVertexToFragmentBlock(inputStruct, genMode);
if (node is IGeneratesVertexShaderBlock) (node as IGeneratesVertexShaderBlock).GenerateVertexShaderBlock(vertexShader, genMode);
if (node is IGenerateProperties)
{
(node as IGenerateProperties).GeneratePropertyBlock(shaderProperties, genMode);
(node as IGenerateProperties).GeneratePropertyUsages(propertyUsages, genMode);
}
}
pixelNode.GenerateNodeCode(shaderBody, genMode);
}
/*
public Material GetMaterial()
{

6
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/ColorNode.cs


set { m_Color = value; }
}
public override void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode)
{
if (exposedState == ExposedState.Exposed)
visitor.AddShaderProperty(new ColorPropertyChunk(propertyName, description, m_Color, PropertyChunk.HideState.Visible));
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
if (exposedState == ExposedState.Exposed || generationMode.IsPreview())

41
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Util/ShaderGenerator.cs


}
}
private static string AdaptNodeOutputForPreview(AbstractMaterialNode node, int outputSlotId, ConcreteSlotValueType convertToType)
public static string AdaptNodeOutputForPreview(AbstractMaterialNode node, int outputSlotId, ConcreteSlotValueType convertToType)
{
var outputSlot = node.FindOutputSlot<MaterialSlot>(outputSlotId);

return Regex.Replace(template, @"\r\n|\n\r|\n|\r", Environment.NewLine);
}
private static void GenerateSurfaceShaderInternal(
PixelShaderNode pixelNode,
ShaderGenerator shaderBody,
ShaderGenerator inputStruct,
ShaderGenerator lightFunction,
ShaderGenerator surfaceOutput,
ShaderGenerator nodeFunction,
PropertyGenerator shaderProperties,
ShaderGenerator propertyUsages,
ShaderGenerator vertexShader,
bool isPreview)
{
pixelNode.GenerateLightFunction(lightFunction);
pixelNode.GenerateSurfaceOutput(surfaceOutput);
var genMode = isPreview ? GenerationMode.Preview3D : GenerationMode.SurfaceShader;
var activeNodes = new List<INode>();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodes, pixelNode);
var activeMaterialNodes = activeNodes.OfType<AbstractMaterialNode>();
foreach (var node in activeMaterialNodes)
{
if (node is IGeneratesFunction) (node as IGeneratesFunction).GenerateNodeFunction(nodeFunction, genMode);
if (node is IGeneratesVertexToFragmentBlock) (node as IGeneratesVertexToFragmentBlock).GenerateVertexToFragmentBlock(inputStruct, genMode);
if (node is IGeneratesVertexShaderBlock) (node as IGeneratesVertexShaderBlock).GenerateVertexShaderBlock(vertexShader, genMode);
if (node is IGenerateProperties)
{
(node as IGenerateProperties).GeneratePropertyBlock(shaderProperties, genMode);
(node as IGenerateProperties).GeneratePropertyUsages(propertyUsages, genMode);
}
}
pixelNode.GenerateNodeCode(shaderBody, genMode);
}
public static string GenerateSurfaceShader(PixelShaderNode node, MaterialOptions options, string shaderName, bool isPreview, out List<PropertyGenerator.TextureInfo> configuredTextures)
{
var templateLocation = GetTemplatePath("shader.template");

var shaderPropertyUsagesVisitor = new ShaderGenerator();
var vertexShaderBlock = new ShaderGenerator();
PixelGraph.GenerateSurfaceShader(
GenerateSurfaceShaderInternal(
node,
shaderBodyVisitor,
shaderInputVisitor,

103
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/PropertyNodeTests.cs


using System;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Graphing;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.IntegrationTests
{
[TestFixture]
public class PropertyNodeTests
{
private PixelGraph m_Graph;
private TextureNode m_TextureNode;
[TestFixtureSetUp]
public void RunBeforeAnyTests()
{
Debug.logger.logHandler = new ConsoleLogHandler();
}
[SetUp]
public void TestSetUp()
{
m_Graph = new PixelGraph();
m_TextureNode = new TextureNode();
m_Graph.AddNode(m_TextureNode);
}
[Test]
public void TestTextureNodeTypeIsCorrect()
{
Assert.AreEqual(PropertyType.Texture2D, m_TextureNode.propertyType);
}
[Test]
public void TestTextureNodeReturnsCorrectValue()
{
m_TextureNode.defaultTexture = null;
Assert.AreEqual(null, m_TextureNode.defaultTexture);
m_TextureNode.textureType = TextureType.Bump;
Assert.AreEqual(TextureType.Bump, m_TextureNode.textureType);
}
[Test]
public void TestTextureNodeReturnsPreviewProperty()
{
var props = new List<PreviewProperty>();
m_TextureNode.defaultTexture = null;
m_TextureNode.CollectPreviewMaterialProperties(props);
Assert.AreEqual(props.Count, 1);
Assert.AreEqual(m_TextureNode.propertyName, props[0].m_Name);
Assert.AreEqual(m_TextureNode.propertyType, props[0].m_PropType);
Assert.AreEqual(m_TextureNode.defaultTexture, props[0].m_Texture);
Assert.AreEqual(null, m_TextureNode.defaultTexture);
}
[Test]
public void TestTextureNodeGeneratesCorrectPropertyBlock()
{
m_TextureNode.defaultTexture = null;
m_TextureNode.textureType = TextureType.Bump;
m_TextureNode.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new PropertyGenerator();
m_TextureNode.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_TextureNode.propertyName
+ "(\""
+ m_TextureNode.description
+ "\", Texture) = ("
+ ")"
+ Environment.NewLine;
m_TextureNode.exposedState = PropertyNode.ExposedState.Exposed;
m_TextureNode.GeneratePropertyBlock(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(expected, generator.GetShaderString(0));
}
[Test]
public void TestTextureNodeGeneratesCorrectPropertyUsages()
{
m_TextureNode.defaultTexture = null;
m_TextureNode.exposedState = PropertyNode.ExposedState.NotExposed;
var generator = new ShaderGenerator();
m_TextureNode.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(string.Empty, generator.GetShaderString(0));
var expected = m_TextureNode.precision
+ "4 "
+ m_TextureNode.propertyName
+ ";"
+ Environment.NewLine;
m_TextureNode.exposedState = PropertyNode.ExposedState.Exposed;
m_TextureNode.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(expected, generator.GetShaderString(0));
}
}
}

59
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/PropertyGeneratorTests.cs


using System.Linq;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Graphing;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.UnitTests
{
[TestFixture]
public class PropertyGeneratorTests
{
[TestFixtureSetUp]
public void RunBeforeAnyTests()
{
Debug.logger.logHandler = new ConsoleLogHandler();
}
private const string kPropertyName = "ThePropertyName";
private const string kPropertyDescription = "ThePropertyDescription";
[Test]
public void TestCanAddPropertyChunkToPropertyGenerator()
{
var chunk = new FloatPropertyChunk(kPropertyName, kPropertyDescription, 0.5f, PropertyChunk.HideState.Visible);
var generator = new PropertyGenerator();
generator.AddShaderProperty(chunk);
Assert.AreNotEqual(string.Empty, generator.GetShaderString(0));
}
[Test]
public void TestCanGetShaderStringWithIndentWorks()
{
var chunk = new FloatPropertyChunk(kPropertyName, kPropertyDescription, 0.5f, PropertyChunk.HideState.Visible);
var generator = new PropertyGenerator();
generator.AddShaderProperty(chunk);
Assert.AreEqual(0, generator.GetShaderString(0).Count(x => x == '\t'));
Assert.AreEqual(1, generator.GetShaderString(1).Count(x => x == '\t'));
Assert.AreEqual(2, generator.GetShaderString(2).Count(x => x == '\t'));
}
[Test]
public void TestCanGetConfiguredTextureInfos()
{
var chunk = new TexturePropertyChunk(kPropertyName, kPropertyDescription, null, TextureType.Bump, PropertyChunk.HideState.Visible, TexturePropertyChunk.ModifiableState.Modifiable);
var generator = new PropertyGenerator();
generator.AddShaderProperty(chunk);
var infos = generator.GetConfiguredTexutres();
Assert.AreEqual(1, infos.Count);
Assert.AreEqual(kPropertyName, infos[0].name);
Assert.AreEqual(0, infos[0].textureId);
Assert.AreEqual(TexturePropertyChunk.ModifiableState.Modifiable, infos[0].modifiable);
}
}
}
正在加载...
取消
保存