浏览代码

[Material Graph]Fix up texture node serialization.

/main
Tim Cooper 8 年前
当前提交
b42e0339
共有 4 个文件被更改,包括 760 次插入352 次删除
  1. 999
      ImageTemplates/WithSubGraph.ShaderGraph.png
  2. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/Graphs/WithSubGraph.ShaderGraph
  3. 64
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/PropertyNodeTests.cs
  4. 43
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/TextureNode.cs

999
ImageTemplates/WithSubGraph.ShaderGraph.png
文件差异内容过多而无法显示
查看文件

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/IntegrationTests/Graphs/WithSubGraph.ShaderGraph


JSONnodeData: "{\n \"m_GuidSerialized\": \"f4d20b5e-052c-48dd-8232-e0a67d391307\",\n
\ \"m_Name\": \"Texture\",\n \"m_DrawData\": {\n \"m_Expanded\":
false,\n \"m_Position\": {\n \"serializedVersion\": \"2\",\n
\ \"x\": -30.83177947998047,\n \"y\": -514.542236328125,\n
\ \"width\": -1367.957763671875,\n \"height\": 568.8974609375\n
\ \"x\": -49.50739288330078,\n \"y\": -533.2178344726563,\n
\ \"width\": -1349.2821044921875,\n \"height\": 587.5730590820313\n
\ }\n },\n \"m_SerializableSlots\": [\n {\n \"typeInfo\":
{\n \"fullName\": \"UnityEngine.MaterialGraph.MaterialSlot\",\n
\ \"assemblyName\": \"Assembly-CSharp\"\n },\n

\ },\\n \\\"m_ConcreteValueType\\\": 2,\\n \\\"m_ShaderOutputName\\\":
\\\"UV\\\"\\n}\"\n }\n ],\n \"m_OutputPrecision\": 1,\n \"m_PropertyName\":
\"\",\n \"m_Description\": \"\",\n \"m_Exposed\": 1,\n \"m_TextureGuid\":
\"28bdf1ba8c076ef4e910b59cc1baf342\",\n \"m_TextureType\": 0\n}"
\"76c3d794167d8524481195736f067518\",\n \"m_TextureType\": 0\n}"
- typeInfo:
fullName: UnityEngine.MaterialGraph.Vector1Node
assemblyName: Assembly-CSharp

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


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.Graphing;

Debug.logger.logHandler = new ConsoleLogHandler();
}
private static readonly string[] s_TexturePath =
{
"Assets",
"UnityShaderEditor",
"Editor",
"Testing",
"IntegrationTests",
"Textures",
"MudDiffuse.tif"
};
private static Texture2D FindTestTexture()
{
var texturePath = s_TexturePath.Aggregate(Path.Combine);
return AssetDatabase.LoadAssetAtPath<Texture2D>(texturePath);
}
[SetUp]
public void TestSetUp()
{

[Test]
public void TestTextureNodeReturnsCorrectValue()
{
m_TextureNode.defaultTexture = null;
Assert.AreEqual(null, m_TextureNode.defaultTexture);
m_TextureNode.defaultTexture = FindTestTexture();
Assert.AreEqual(FindTestTexture(), m_TextureNode.defaultTexture);
m_TextureNode.textureType = TextureType.Bump;
Assert.AreEqual(TextureType.Bump, m_TextureNode.textureType);

public void TestTextureNodeReturnsPreviewProperty()
{
var props = new List<PreviewProperty>();
m_TextureNode.defaultTexture = null;
m_TextureNode.defaultTexture = FindTestTexture();
Assert.AreEqual(props.Count, 1);
Assert.AreEqual(1, props.Count);
Assert.AreEqual(null, m_TextureNode.defaultTexture);
Assert.AreEqual(FindTestTexture(), m_TextureNode.defaultTexture);
}
[Test]

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
var expected1 = "[NonModifiableTextureData] "
+ m_TextureNode.propertyName
+ "\", Texture) = ("
+ ")"
+ "\", 2D) = \"bump\" {}"
Assert.AreEqual(expected1, generator.GetShaderString(0));
var expected2 = m_TextureNode.propertyName
+ "(\""
+ m_TextureNode.description
+ "\", 2D) = \"bump\" {}"
+ Environment.NewLine;
generator = new PropertyGenerator();
Assert.AreEqual(expected, generator.GetShaderString(0));
Assert.AreEqual(expected2, generator.GetShaderString(0));
}
[Test]

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;
var expected = "sampler2D "
+ m_TextureNode.propertyName
+ ";"
+ Environment.NewLine;
Assert.AreEqual(expected, generator.GetShaderString(0));
generator = new ShaderGenerator();
m_TextureNode.GeneratePropertyUsages(generator, GenerationMode.SurfaceShader);
Assert.AreEqual(expected, generator.GetShaderString(0));
}

43
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/TextureNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor;

public const int OutputSlotAId = 5;
[SerializeField]
private string m_TextureGuid;
private string m_SerializedTexture;
[Serializable]
private class TextureHelper
{
public Texture2D texture;
}
public override bool hasPreview { get { return true; } }
#if UNITY_EDITOR

{
if (string.IsNullOrEmpty(m_TextureGuid))
if (string.IsNullOrEmpty(m_SerializedTexture))
var path = AssetDatabase.GUIDToAssetPath(m_TextureGuid);
if (string.IsNullOrEmpty(path))
return null;
return AssetDatabase.LoadAssetAtPath<Texture2D>(path);
var tex = new TextureHelper();
EditorJsonUtility.FromJsonOverwrite(m_SerializedTexture, tex);
return tex.texture;
var assetPath = AssetDatabase.GetAssetPath(value);
if (string.IsNullOrEmpty(assetPath))
return;
m_TextureGuid = AssetDatabase.AssetPathToGUID(assetPath);
var tex = new TextureHelper();
tex.texture = value;
m_SerializedTexture = EditorJsonUtility.ToJson(tex, true);
public Texture2D defaultTexture
{
get
{
return Texture2D.whiteTexture;
}
set
{}
}
public Texture2D defaultTexture {get;set;}
#endif
public TextureType textureType

break;
}
return GetVariableNameForNode() + slotOutput;
}
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(GetPreviewProperty());
}
public void GenerateVertexToFragmentBlock(ShaderGenerator visitor, GenerationMode generationMode)

正在加载...
取消
保存