浏览代码

Texture Asset node and sampler2D input/output type

/main
vlad 8 年前
当前提交
09332619
共有 11 个文件被更改,包括 362 次插入0 次删除
  1. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs
  2. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs
  3. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs
  4. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SlotValue.cs
  5. 52
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/TextureAssetPresenter.cs
  6. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/TextureAssetPresenter.cs.meta
  7. 152
      MaterialGraphProject/Assets/Vlad/TextureAssetNode.cs
  8. 12
      MaterialGraphProject/Assets/Vlad/TextureAssetNode.cs.meta
  9. 112
      MaterialGraphProject/Assets/Vlad/UVTriPlanar.cs
  10. 12
      MaterialGraphProject/Assets/Vlad/UVTriPlanar.cs.meta

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs


typeMapper[typeof(AbstractMaterialNode)] = typeof(MaterialNodePresenter);
typeMapper[typeof(ColorNode)] = typeof(ColorNodePresenter);
typeMapper[typeof(TextureNode)] = typeof(TextureNodePresenter);
typeMapper[typeof(TextureAssetNode)] = typeof(TextureAssetNodePresenter);
typeMapper[typeof(TextureLODNode)] = typeof(TextureLODNodePresenter);
typeMapper[typeof(CubemapNode)] = typeof(CubeNodePresenter);
typeMapper[typeof(ToggleNode)] = typeof(ToggleNodePresenter);

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs


Assert.AreEqual("2", AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(ConcreteSlotValueType.Vector2));
Assert.AreEqual("3", AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(ConcreteSlotValueType.Vector3));
Assert.AreEqual("4", AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(ConcreteSlotValueType.Vector4));
Assert.AreEqual("5", AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(ConcreteSlotValueType.Error));
Assert.AreEqual("Error", AbstractMaterialNode.ConvertConcreteSlotValueTypeToString(ConcreteSlotValueType.Error));
}

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


return ConcreteSlotValueType.Vector3;
case SlotValueType.Vector4:
return ConcreteSlotValueType.Vector4;
case SlotValueType.sampler2D:
return ConcreteSlotValueType.sampler2D;
}
return ConcreteSlotValueType.Error;
}

return "3";
case ConcreteSlotValueType.Vector4:
return "4";
case ConcreteSlotValueType.sampler2D:
return "5";
default:
return "Error";
}

{
switch (slotValue)
{
case ConcreteSlotValueType.sampler2D:
return PropertyType.Texture2D;
case ConcreteSlotValueType.Vector1:
return PropertyType.Float;
case ConcreteSlotValueType.Vector2:

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SlotValue.cs


[Serializable]
public enum SlotValueType
{
sampler2D,
Dynamic,
Vector4,
Vector3,

public enum ConcreteSlotValueType
{
sampler2D = 5,
Vector4 = 4,
Vector3 = 3,
Vector2 = 2,

52
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/TextureAssetPresenter.cs


using System;
using System.Collections.Generic;
using RMGUI.GraphView;
using UnityEditor.Graphing.Drawing;
using UnityEngine;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.Drawing
{
class TextureAssetContolPresenter : GraphControlPresenter
{
private string[] m_TextureTypeNames;
private string[] textureTypeNames
{
get
{
if (m_TextureTypeNames == null)
m_TextureTypeNames = Enum.GetNames(typeof(TextureType));
return m_TextureTypeNames;
}
}
public override void OnGUIHandler()
{
base.OnGUIHandler();
var tNode = node as UnityEngine.MaterialGraph.TextureAssetNode;
if (tNode == null)
return;
tNode.exposedState = (PropertyNode.ExposedState)EditorGUILayout.EnumPopup(new GUIContent("Exposed"), tNode.exposedState);
tNode.defaultTexture = EditorGUILayout.MiniThumbnailObjectField(new GUIContent("Texture"), tNode.defaultTexture, typeof(Texture2D), null) as Texture2D;
tNode.textureType = (TextureType)EditorGUILayout.Popup((int)tNode.textureType, textureTypeNames, EditorStyles.popup);
}
public override float GetHeight()
{
return 3 * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing) + EditorGUIUtility.standardVerticalSpacing;
}
}
[Serializable]
public class TextureAssetNodePresenter : MaterialNodePresenter
{
protected override IEnumerable<GraphElementPresenter> GetControlData()
{
var instance = CreateInstance<TextureAssetContolPresenter>();
instance.Initialize(node);
return new List<GraphElementPresenter> { instance };
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/TextureAssetPresenter.cs.meta


fileFormatVersion: 2
guid: fea6f3daed9e37042828a425483c9a5c
timeCreated: 1495541485
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

152
MaterialGraphProject/Assets/Vlad/TextureAssetNode.cs


using System;
using System.Collections.Generic;
using System.Linq;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
[Title("Input/Texture/Texture Asset")]
public class TextureAssetNode : PropertyNode
{
protected const string kOutputSlotRGBAName = "Texture";
public const int OutputSlotRgbaId = 0;
[SerializeField]
private string m_SerializedTexture;
[SerializeField]
private TextureType m_TextureType;
[Serializable]
private class TextureHelper
{
public Texture2D texture;
}
public override bool hasPreview { get { return false; } }
#if UNITY_EDITOR
public Texture2D defaultTexture
{
get
{
if (string.IsNullOrEmpty(m_SerializedTexture))
return null;
var tex = new TextureHelper();
EditorJsonUtility.FromJsonOverwrite(m_SerializedTexture, tex);
return tex.texture;
}
set
{
if (defaultTexture == value)
return;
var tex = new TextureHelper();
tex.texture = value;
m_SerializedTexture = EditorJsonUtility.ToJson(tex, true);
if (onModified != null)
{
onModified(this, ModificationScope.Node);
}
}
}
#else
public Texture2D defaultTexture {get; set; }
#endif
public TextureType textureType
{
get { return m_TextureType; }
set
{
if (m_TextureType == value)
return;
m_TextureType = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
public TextureAssetNode()
{
name = "TextureAsset";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(OutputSlotRgbaId, kOutputSlotRGBAName, kOutputSlotRGBAName, SlotType.Output, SlotValueType.sampler2D, Vector4.zero, false));
RemoveSlotsNameNotMatching(validSlots);
}
protected int[] validSlots
{
get { return new[] { OutputSlotRgbaId }; }
}
public override string GetVariableNameForSlot(int slotId)
{
string slotOutput;
switch (slotId)
{
default:
slotOutput = "";
break;
}
return GetVariableNameForNode() + slotOutput;
}
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties)
{
properties.Add(GetPreviewProperty());
}
// Properties
public override void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode)
{
visitor.AddShaderProperty(
new TexturePropertyChunk(
propertyName,
description,
defaultTexture, m_TextureType,
PropertyChunk.HideState.Visible,
exposedState == ExposedState.Exposed ?
TexturePropertyChunk.ModifiableState.Modifiable
: TexturePropertyChunk.ModifiableState.NonModifiable));
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
visitor.AddShaderChunk("sampler2D " + propertyName + ";", true);
}
public override PreviewProperty GetPreviewProperty()
{
return new PreviewProperty
{
m_Name = propertyName,
m_PropType = PropertyType.Texture2D,
m_Texture = defaultTexture
};
}
public override PropertyType propertyType { get { return PropertyType.Texture2D; } }
}
}

12
MaterialGraphProject/Assets/Vlad/TextureAssetNode.cs.meta


fileFormatVersion: 2
guid: 9def76d530f56804b9f696a9b36eccfa
timeCreated: 1495535587
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

112
MaterialGraphProject/Assets/Vlad/UVTriPlanar.cs


using UnityEngine.Graphing;
using System.Collections.Generic;
using System.Linq;
namespace UnityEngine.MaterialGraph
{
[Title("UV/Tri-Planar Mapping")]
public class UVTriPlanar : Function1Input, IGeneratesFunction, IMayRequireNormal, IMayRequireWorldPosition
{
private const string kTextureSlotName = "Texture";
protected override string GetFunctionName()
{
return "unity_triplanar_" + precision;
}
protected override MaterialSlot GetInputSlot()
{
return new MaterialSlot(InputSlotId, kTextureSlotName, kTextureSlotName, SlotType.Input, SlotValueType.sampler2D, Vector4.zero, false);
}
protected override MaterialSlot GetOutputSlot()
{
return new MaterialSlot(OutputSlotId, kTextureSlotName, kOutputSlotShaderName, SlotType.Output, SlotValueType.Vector4, Vector4.zero);
}
public UVTriPlanar()
{
name = "UVTriPlanar";
UpdateNodeAfterDeserialization();
}
protected override string GetFunctionPrototype(string argName)
{
return "inline " + precision + outputDimension + " " + GetFunctionName() + " ("
+ "sampler2D " + argName + ", float3 normal, float3 pos)";
}
protected override string GetFunctionCallBody(string inputValue)
{
return GetFunctionName() + " (" + inputValue + "_Uniform" + ", IN.worldNormal, IN.worldPos)";
}
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode)
{
base.GeneratePropertyUsages(visitor, generationMode);
}
//TODO:Externalize
//Reference code from:http://www.chilliant.com/rgb2hsv.html
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
var textureSlot = FindInputSlot<MaterialSlot>(InputSlotId);
if (textureSlot == null)
return;
var textureName = "";
var edges = owner.GetEdges(textureSlot.slotReference).ToList();
if (edges.Count > 0)
{
var edge = edges[0];
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(edge.outputSlot.nodeGuid);
textureName = ShaderGenerator.AdaptNodeOutput(fromNode, edge.outputSlot.slotId, ConcreteSlotValueType.sampler2D, true);
}
////////////////////////////////QQQ: Any better way of getting "_Uniform" at the end? //////////////////////////////////
// outputString.AddShaderChunk("sampler2D " + textureName + "_Uniform;", false);
// outputString.AddShaderChunk("", false);
outputString.AddShaderChunk(GetFunctionPrototype("arg"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
// use absolute value of normal as texture weights
outputString.AddShaderChunk("half3 blend = abs(normal);", false);
// make sure the weights sum up to 1 (divide by sum of x+y+z)
outputString.AddShaderChunk("blend /= dot(blend, 1.0);", false);
// read the three texture projections, for x,y,z axes
outputString.AddShaderChunk("fixed4 cx = tex2D(arg, pos.yz);", false);
outputString.AddShaderChunk("fixed4 cy = tex2D(arg, pos.xz);", false);
outputString.AddShaderChunk("fixed4 cz = tex2D(arg, pos.xy);", false);
// blend the textures based on weights
outputString.AddShaderChunk("fixed4 c = cx * blend.x + cy * blend.y + cz * blend.z;", false);
outputString.AddShaderChunk("return " + "c;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
public bool RequiresNormal()
{
return true;
}
public bool RequiresWorldPosition()
{
return true;
}
}
}

12
MaterialGraphProject/Assets/Vlad/UVTriPlanar.cs.meta


fileFormatVersion: 2
guid: c355f376dad60404fbfd488ec80b88a4
timeCreated: 1495485045
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存