浏览代码

More work on porting node library across to new shader library.

/main
Tim Cooper 7 年前
当前提交
7a9a255f
共有 17 个文件被更改,包括 126 次插入83 次删除
  1. 6
      MaterialGraphProject/Assets/ShaderVariablesFunctions.hlsl
  2. 1
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs
  3. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SamplerStateShaderProperty.cs
  4. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Normal/NormalUnpackNode.cs
  5. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Matrix/TransformationMatrixNode.cs
  6. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/CubemapNode.cs
  7. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/Delete/TextureLODNode.cs
  8. 32
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/Texture2DNode.cs
  9. 34
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/TransformNode.cs
  10. 34
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderGenerator.cs
  11. 6
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs
  12. 18
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/MatrixNames.cs
  13. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/MatrixNames.cs.meta
  14. 18
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderGeneratorNames.cs
  15. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderGeneratorNames.cs.meta
  16. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/UvChannel.cs
  17. 11
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/UvChannel.cs.meta

6
MaterialGraphProject/Assets/ShaderVariablesFunctions.hlsl


return unity_WorldTransformParams.w;
}
float3 WorldSpaceViewDir(float3 cameraPositionWS, float3 positionOS)
{
float3 worldPos = mul(GetObjectToWorldMatrix(), float4(positionOS, 1.0)).xyz;
return normalize(cameraPositionWS - worldPos);
}
float3 TransformWorldToView(float3 positionWS)
{
return mul(GetWorldToViewMatrix(), float4(positionWS, 1.0)).xyz;

1
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/AbstractMaterialGraph.cs


finalShader.AddShaderChunk("HLSLINCLUDE", false);
finalShader.AddShaderChunk("#define USE_LEGACY_UNITY_MATRIX_VARIABLES", false);
finalShader.AddShaderChunk("#include \"Common.hlsl\"", false);
finalShader.AddShaderChunk("#include \"Packing.hlsl\"", false);
finalShader.AddShaderChunk("#include \"ShaderVariables.hlsl\"", false);
finalShader.AddShaderChunk("#include \"ShaderVariablesFunctions.hlsl\"", false);

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/SamplerStateShaderProperty.cs


public override string GetPropertyDeclarationString()
{
return string.Format(@"
#ifdef UNITY_COMPILER_HLSL
SamplerState {0};
#endif", referenceName);
return string.Format(@"SAMPLER2D({0});", referenceName);
}
public override PreviewProperty GetPreviewMaterialProperty()

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Artistic/Normal/NormalUnpackNode.cs


return
@"
{
Out = UnpackNormal(In);
Out = UnpackNormalmapRGorAG(In);
}
";
}

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Matrix/TransformationMatrixNode.cs


using System.ComponentModel;
using UnityEditor.Graphing;
using System.Collections.Generic;
using UnityEditor.ShaderGraph.Drawing.Controls;

{TransformationMatrixType.ViewProjection, "UNITY_MATRIX_VP"},
{TransformationMatrixType.TransposeModelView, "UNITY_MATRIX_T_MV"},
{TransformationMatrixType.InverseTransposeModelView, "UNITY_MATRIX_IT_MV"},
{TransformationMatrixType.ObjectToWorld, "unity_ObjectToWorld"},
{TransformationMatrixType.WorldToObject, "unity_WorldToObject"},
{TransformationMatrixType.ObjectToWorld, "UNITY_MATRIX_M"},
{TransformationMatrixType.WorldToObject, "UNITY_MATRIX_I_M"},
};
[SerializeField]

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/CubemapNode.cs


using System.Linq;
using System.Reflection;
using UnityEditor.ShaderGraph.Drawing.Controls;
namespace UnityEditor.ShaderGraph
{

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/Delete/TextureLODNode.cs


string body = "tex2Dlod (" + propertyName + ", " + precision + "4(" + uvName + ".x," + uvName + ".y, 0," + lodName + "));";
if (m_TextureType == TextureType.Bump)
body = precision + "4(UnpackNormal(" + body + "), 0)";
body = precision + "4(UnpackNormalmapRGorAG(" + body + "), 0)";
visitor.AddShaderChunk(precision + "4 " + GetVariableNameForNode() + " = " + body + ";", true);
}

32
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Texture/Texture2DNode.cs


var samplerSlot = FindInputSlot<MaterialSlot>(SamplerInput);
var edgesSampler = owner.GetEdges(samplerSlot.slotReference);
string result;
if (edgesSampler.Any())
{
result = string.Format(@"
#ifdef UNITY_COMPILER_HLSL
{0}4 {1} = {2}.Sample({3}, {4});
#else
{0}4 {1} = UNITY_SAMPLE_TEX2D({2}, {4});
#endif"
, precision
, GetVariableNameForSlot(OutputSlotRGBAId)
, GetSlotValue(TextureInputId, generationMode)
, GetSlotValue(SamplerInput, generationMode)
, uvName);
}
else
{
result = string.Format("{0}4 {1} = SAMPLE_TEXTURE2D({2}, sampler{2}, {3});"
, precision
, GetVariableNameForSlot(OutputSlotRGBAId)
, GetSlotValue(TextureInputId, generationMode)
, uvName);
}
var id = GetSlotValue(TextureInputId, generationMode);
var result = string.Format("{0}4 {1} = SAMPLE_TEXTURE2D({2}, {3}, {4});"
, precision
, GetVariableNameForSlot(OutputSlotRGBAId)
, id
, edgesSampler.Any() ? GetSlotValue(SamplerInput, generationMode) : "sampler" + id
, uvName);
visitor.AddShaderChunk(string.Format("{0}.rgb = UnpackNormal({0});", GetVariableNameForSlot(OutputSlotRGBAId)), true);
visitor.AddShaderChunk(string.Format("{0}.rgb = UnpackNormalmapRGorAG({0});", GetVariableNameForSlot(OutputSlotRGBAId)), true);
visitor.AddShaderChunk(string.Format("{0} {1} = {2}.r;", precision, GetVariableNameForSlot(OutputSlotRId), GetVariableNameForSlot(OutputSlotRGBAId)), true);
visitor.AddShaderChunk(string.Format("{0} {1} = {2}.g;", precision, GetVariableNameForSlot(OutputSlotGId), GetVariableNameForSlot(OutputSlotRGBAId)), true);

34
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Math/Vector/TransformNode.cs


protected int[] validSlots
{
get { return new[] {InputSlotId, OutputSlotId}; }
get { return new[] { InputSlotId, OutputSlotId }; }
}
protected virtual MaterialSlot GetInputSlot()

}
else if (conversion.to == CoordinateSpace.Object)
{
transformString = "mul(unity_WorldToObject, float4(" + inputValue + ", 0)).xyz";
transformString = string.Format("mul({0}, float4({1}, 0)).xyz", MatrixNames.ModelInverse, inputValue);
transformString = "mul(tangentTransform, " + inputValue + ").xyz";
transformString = string.Format("mul(tangentTransform, {0}).xyz", inputValue);
transformString = "mul( UNITY_MATRIX_V, float4(" + inputValue + ", 0)).xyz";
transformString = string.Format("mul({0}, float4({1}, 0)).xyz", MatrixNames.View, inputValue);
}
}
else if (conversion.from == CoordinateSpace.Object)

transformString = "mul(unity_ObjectToWorld, float4(" + inputValue + ", 0)).xyz";
transformString = string.Format("mul({0}, float4({1}, 0)).xyz", MatrixNames.Model, inputValue);
}
else if (conversion.to == CoordinateSpace.Object)
{

{
requiresTangentTransform = true;
transformString = "mul( tangentTransform, mul( unity_ObjectToWorld, float4(" + inputValue + ", 0) ).xyz).xyz";
transformString = string.Format("mul(tangentTransform, mul({0}, float4({1}, 0)).xyz).xyz", MatrixNames.Model, inputValue);
transformString = "mul( UNITY_MATRIX_MV, float4(" + inputValue + ", 0)).xyz";
transformString = string.Format("mul({0}, float4({1}, 0)).xyz", MatrixNames.ModelView, inputValue);
}
}
else if (conversion.from == CoordinateSpace.Tangent)

{
transformString = "mul( " + inputValue + ", tangentTransform ).xyz";
transformString = string.Format("mul({0}, tangentTransform).xyz", inputValue);
transformString = "mul( unity_WorldToObject, float4(mul(" + inputValue + ", tangentTransform ),0) ).xyz";
transformString = string.Format("mul({0}, float4(mul({1}, tangentTransform ), 0)).xyz", MatrixNames.ModelInverse, inputValue);
}
else if (conversion.to == CoordinateSpace.Tangent)
{

{
transformString = "mul( UNITY_MATRIX_V, float4(mul(" + inputValue + ", tangentTransform ),0) ).xyz";
transformString = string.Format("mul({0}, float4(mul({1}, tangentTransform), 0)).xyz", MatrixNames.View, inputValue);
}
}
else if (conversion.from == CoordinateSpace.View)

transformString = "mul( float4(" + inputValue + ", 0), UNITY_MATRIX_V ).xyz";
transformString = string.Format("mul(float4({0}, 0), {1}).xyz", inputValue, MatrixNames.View);
transformString = "mul( float4(" + inputValue + ", 0), UNITY_MATRIX_MV ).xyz";
transformString = string.Format("mul(float4({0}, 0), {1}).xyz", inputValue, MatrixNames.ModelView);
transformString = "mul( tangentTransform, mul( float4(" + inputValue + ", 0), UNITY_MATRIX_V ).xyz ).xyz";
transformString = string.Format("mul(tangentTransform, mul(float4({0}, 0), {1}).xyz).xyz", inputValue, MatrixNames.View);
}
else if (conversion.to == CoordinateSpace.View)
{

if (requiresTangentTransform)
visitor.AddShaderChunk("float3x3 tangentTransform = float3x3(" + conversion.from.ToString() + "SpaceTangent, " + conversion.from.ToString() + "SpaceBiTangent, " + conversion.from.ToString() + "SpaceNormal);", false);
visitor.AddShaderChunk(string.Format("float3x3 tangentTransform = float3x3({0}SpaceTangent, {0}SpaceBiTangent, {0}SpaceNormal);", conversion.from), false);
ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType),
GetVariableNameForSlot(OutputSlotId),
transformString), true);
ConvertConcreteSlotValueTypeToString(precision, FindOutputSlot<MaterialSlot>(OutputSlotId).concreteValueType),
GetVariableNameForSlot(OutputSlotId),
transformString), true);
}
public NeededCoordinateSpace RequiresTangent()

34
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderGenerator.cs


using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
public static class ShaderGeneratorNames
{
private static string[] UV = {"uv0", "uv1", "uv2", "uv3"};
public static int UVCount = 4;
public const string ScreenPosition = "screenPosition";
public const string VertexColor = "vertexColor";
public static string GetUVName(this UVChannel channel)
{
return UV[(int)channel];
}
}
public enum UVChannel
{
uv0 = 0,
uv1 = 1,
uv2 = 2,
uv3 = 3,
}
public class ShaderGenerator
{
private struct ShaderChunk

m_transforms[(int)CoordinateSpace.World, (int)CoordinateSpace.World] = new TransformDesc[] {};
m_transforms[(int)CoordinateSpace.Tangent, (int)CoordinateSpace.Tangent] = new TransformDesc[] {};
m_transforms[(int)CoordinateSpace.Object, (int)CoordinateSpace.World]
= new TransformDesc[] {new TransformDesc("unity_ObjectToWorld")};
= new TransformDesc[] {new TransformDesc(MatrixNames.Model)};
= new TransformDesc[] {new TransformDesc("UNITY_MATRIX_I_V")};
= new TransformDesc[] {new TransformDesc(MatrixNames.ViewInverse) };
= new TransformDesc[] {new TransformDesc("unity_WorldToObject")};
= new TransformDesc[] {new TransformDesc(MatrixNames.ModelInverse)};
= new TransformDesc[] {new TransformDesc("UNITY_MATRIX_V")};
= new TransformDesc[] {new TransformDesc(MatrixNames.View)};
for (var from = CoordinateSpace.Object; from != CoordinateSpace.Tangent; from++)
{
for (var to = CoordinateSpace.Object; to != CoordinateSpace.Tangent; to++)

{
var name = preferedCoordinateSpace.ToVariableName(InterpolatorType.ViewDirection);
interpolators.AddShaderChunk(string.Format("float3 {0} : TEXCOORD{1};", name, interpolatorIndex), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = {1};", name, ConvertBetweenSpace("ObjSpaceViewDir(v.vertex)", CoordinateSpace.Object, preferedCoordinateSpace, InputType.Vector)), false);
vertexShader.AddShaderChunk(string.Format("o.{0} = {1};", name, ConvertBetweenSpace("WorldSpaceViewDir(_WorldSpaceCameraPos.xyz, v.vertex)", CoordinateSpace.World, preferedCoordinateSpace, InputType.Vector)), false);
pixelShader.AddShaderChunk(string.Format("float3 {0} = normalize(IN.{0});", name), false);
interpolatorIndex++;
}

6
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/PreviewManager.cs


m_RenderList3D.Add(renderData);
}
if (masterRenderData.shaderData != null && m_DirtyPreviews.Contains(masterRenderData.shaderData.node.guid))
m_RenderList3D.Add(masterRenderData);
if (masterRenderData.shaderData != null
&& masterRenderData.shaderData.shader != null
&& m_DirtyPreviews.Contains(masterRenderData.shaderData.node.guid))
m_RenderList3D.Add(masterRenderData);
m_RenderList3D.Sort((data1, data2) => data1.shaderData.shader.GetInstanceID().CompareTo(data2.shaderData.shader.GetInstanceID()));
m_RenderList2D.Sort((data1, data2) => data1.shaderData.shader.GetInstanceID().CompareTo(data2.shaderData.shader.GetInstanceID()));

18
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/MatrixNames.cs


using System;
namespace UnityEditor.ShaderGraph {
public struct MatrixNames
{
public const string Model = "UNITY_MATRIX_M";
public const string ModelInverse = "UNITY_MATRIX_I_M";
public const string View = "UNITY_MATRIX_V";
public const string ViewInverse = "UNITY_MATRIX__I_V";
public const string Projection = "UNITY_MATRIX_P";
public const string ViewProjection = "UNITY_MATRIX_VP";
public const string ModelView = "UNITY_MATRIX_MV";
public const string ModelViewTransposed = "UNITY_MATRIX_T_MV";
public const string ModelViewInverseTransposed = "UNITY_MATRIX_IT_MV";
public const string ModelViewProjection = "UNITY_MATRIX_MVP";
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/MatrixNames.cs.meta


fileFormatVersion: 2
guid: 1358e9b39cd7e8442bece5b15a2a7c63
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

18
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderGeneratorNames.cs


using System;
namespace UnityEditor.ShaderGraph {
public static class ShaderGeneratorNames
{
private static string[] UV = {"uv0", "uv1", "uv2", "uv3"};
public static int UVCount = 4;
public const string ScreenPosition = "screenPosition";
public const string VertexColor = "vertexColor";
public static string GetUVName(this UVChannel channel)
{
return UV[(int)channel];
}
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderGeneratorNames.cs.meta


fileFormatVersion: 2
guid: 33854ecfd53cd054a99d8cb17b260170
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/UvChannel.cs


using System;
namespace UnityEditor.ShaderGraph {
public enum UVChannel
{
uv0 = 0,
uv1 = 1,
uv2 = 2,
uv3 = 3,
}
}

11
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/UvChannel.cs.meta


fileFormatVersion: 2
guid: d77363d2f0df1f544b21d9e8a05f01a1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存