浏览代码

Merge branch 'hackweek2017' of https://github.com/stramit/MaterialGraph into hackweek2017

/main
RinaldoTjan 7 年前
当前提交
ea69972e
共有 10 个文件被更改,包括 405 次插入10 次删除
  1. 4
      MaterialGraphProject/Assets/Eduardo/FunctionNAddNode.cs
  2. 4
      MaterialGraphProject/Assets/Eduardo/FunctionNInNOut.cs
  3. 10
      MaterialGraphProject/Assets/Rinaldo/FractalNode.cs
  4. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/CheckerboardNode.cs
  5. 48
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/ExportTexture.template
  6. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/ExportTexture.template.meta
  7. 77
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/UV/SpherizeNode.cs
  8. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/UV/SpherizeNode.cs.meta
  9. 242
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SurfaceModel/ExportTextureMasterNode.cs
  10. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SurfaceModel/ExportTextureMasterNode.cs.meta

4
MaterialGraphProject/Assets/Eduardo/FunctionNAddNode.cs


public void AddInputSlot()
{
string inputName = "Input" + GetInputSlots<MaterialSlot>().Count().ToString();
AddSlot(inputName, inputName, Graphing.SlotType.Input, SlotValueType.Dynamic);
AddSlot(inputName, inputName, Graphing.SlotType.Input, SlotValueType.Dynamic, Vector4.zero);
}
public AddManyNode()

AddInputSlot();
}
AddSlot("Sum", "finalSum", Graphing.SlotType.Output, SlotValueType.Dynamic);
AddSlot("Sum", "finalSum", Graphing.SlotType.Output, SlotValueType.Dynamic, Vector4.zero);
UpdateNodeAfterDeserialization();
}

4
MaterialGraphProject/Assets/Eduardo/FunctionNInNOut.cs


}
}
public int AddSlot(string displayName, string nameInShader, SlotType slotType, SlotValueType valueType)
public int AddSlot(string displayName, string nameInShader, SlotType slotType, SlotValueType valueType, Vector4 defaultValue)
{
int nextSlotId;
if (slotType == SlotType.Output)

AddSlot(new MaterialSlot(nextSlotId, displayName, nameInShader, slotType, valueType, Vector4.zero, true));
AddSlot(new MaterialSlot(nextSlotId, displayName, nameInShader, slotType, valueType, defaultValue, true));
return nextSlotId;
}

10
MaterialGraphProject/Assets/Rinaldo/FractalNode.cs


public FractalNode()
{
name = "Fractal";
AddSlot("UV", "texCoord", Graphing.SlotType.Input, SlotValueType.Vector2);
AddSlot("Pan", "Pan", Graphing.SlotType.Input, SlotValueType.Vector2);
AddSlot("Zoom", "Zoom", Graphing.SlotType.Input, SlotValueType.Vector1);
AddSlot("Aspect", "Aspect", Graphing.SlotType.Input, SlotValueType.Vector1);
AddSlot("UV", "texCoord", Graphing.SlotType.Input, SlotValueType.Vector2, Vector4.zero);
AddSlot("Pan", "Pan", Graphing.SlotType.Input, SlotValueType.Vector2, new Vector4(0.5f,0,0,0));
AddSlot("Zoom", "Zoom", Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(3,0,0,0));
AddSlot("Aspect", "Aspect", Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(0.9f,0,0,0));
AddSlot("FracResult", "fractalRes", Graphing.SlotType.Output, SlotValueType.Dynamic);
AddSlot("FracResult", "fractalRes", Graphing.SlotType.Output, SlotValueType.Dynamic, Vector4.zero);
UpdateNodeAfterDeserialization();
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/CheckerboardNode.cs


outputString.AddShaderChunk(GetFunctionPrototype("uv", "horizontalTileScale", "verticalTileScale"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return floor(fmod(floor(uv.x * horizontalTileScale) + floor(uv.y * verticalTileScale), 2.0)) * float4(1.0, 1.0, 1.0, 1.0);", false);
outputString.AddShaderChunk("return abs(floor(fmod(floor(uv.x * horizontalTileScale) + floor(uv.y * verticalTileScale), 2.0)));", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);

48
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/ExportTexture.template


Shader "${ShaderName}"
{
Properties
{
${ShaderPropertiesHeader}
}
SubShader
{
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
sampler2D ${ShaderTextureName};
float4 ${ShaderTextureName}_ST;
v2f vert (appdata_full v)
{
v2f o = (v2f)0;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, ${ShaderTextureName});
return o;
}
half4 frag (v2f IN) : COLOR
{
half4 col = tex2D(${ShaderTextureName}, IN.uv);
return col;
}
ENDCG
}
}
Fallback Off
}

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/ExportTexture.template.meta


fileFormatVersion: 2
guid: db810b4437bc78b4f94f31af1d276775
DefaultImporter:
userData:

77
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/UV/SpherizeNode.cs


namespace UnityEngine.MaterialGraph
{
[Title("UV/Spherize")]
public class SpherizeNode : Function3Input, IGeneratesFunction
{
public SpherizeNode()
{
name = "Spherize";
}
protected override string GetFunctionName()
{
return "unity_spherize_" + precision;
}
protected override string GetInputSlot1Name()
{
return "UV";
}
protected override MaterialSlot GetInputSlot1()
{
return new MaterialSlot(InputSlot1Id, GetInputSlot1Name(), kInputSlot1ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.zero);
}
protected override string GetInputSlot2Name()
{
return "Position";
}
protected override MaterialSlot GetInputSlot2()
{
return new MaterialSlot(InputSlot2Id, GetInputSlot2Name(), kInputSlot2ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.zero);
}
protected override string GetInputSlot3Name()
{
return "RadiusAndStrength";
}
protected override MaterialSlot GetInputSlot3()
{
return new MaterialSlot(InputSlot3Id, GetInputSlot3Name(), kInputSlot3ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector3.zero);
}
protected override MaterialSlot GetOutputSlot()
{
return new MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, UnityEngine.Graphing.SlotType.Output, SlotValueType.Vector2, Vector2.zero);
}
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
outputString.AddShaderChunk(GetFunctionPrototype("uv", "position", "radiusAndStrength"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
//vec2 fromUVToPoint = pos - uv;
//dist = length(fromUVToPoint);
//float mag = (1.0 - (dist / radius)) * strength;
//mag *= step(dist, radius);
//return uv + (mag * fromUVToPoint);
outputString.AddShaderChunk("float2 fromUVToPoint = position - uv;", false);
outputString.AddShaderChunk("float dist = length(fromUVToPoint);", false);
outputString.AddShaderChunk("float mag = ((1.0 - (dist / radiusAndStrength.x)) * radiusAndStrength.y) * step(dist, radiusAndStrength.x);", false);
outputString.AddShaderChunk("return uv + (mag * fromUVToPoint);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/UV/SpherizeNode.cs.meta


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

242
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SurfaceModel/ExportTextureMasterNode.cs


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
[Serializable]
[Title("Master/Export Texture")]
public class ExportTextureMasterNode : AbstractMasterNode
{
public const string ColorSlotName = "Color";
public const int ColorSlotId = 0;
[SerializeField]
private SurfaceMaterialOptions m_MaterialOptions = new SurfaceMaterialOptions();
public SurfaceMaterialOptions options
{
get { return m_MaterialOptions; }
}
public ExportTextureMasterNode()
{
name = "ExportTextureMasterNode";
UpdateNodeAfterDeserialization();
}
public sealed override void UpdateNodeAfterDeserialization()
{
AddSlot(new MaterialSlot(ColorSlotId, ColorSlotName, ColorSlotName, SlotType.Input, SlotValueType.Vector4, Vector4.zero));
// clear out slot names that do not match the slots we support
RemoveSlotsNameNotMatching(new[] { ColorSlotId });
}
public override PreviewMode previewMode
{
get
{
return PreviewMode.Preview2D;
}
}
public override string GetSubShader(GenerationMode mode, PropertyGenerator shaderPropertiesVisitor)
{
return "";
}
public override bool has3DPreview()
{
return false;
}
public override string GetFullShader(GenerationMode mode, out List<PropertyGenerator.TextureInfo> configuredTextures)
{
/*
// figure out what kind of preview we want!
var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this);
string templateLocation = ShaderGenerator.GetTemplatePath("2DPreview.template");
if (!File.Exists(templateLocation))
{
configuredTextures = new List<PropertyGenerator.TextureInfo>();
return string.Empty;
}
string template = File.ReadAllText(templateLocation);
var shaderBodyVisitor = new ShaderGenerator();
var shaderFunctionVisitor = new ShaderGenerator();
var shaderPropertiesVisitor = new PropertyGenerator();
var shaderPropertyUsagesVisitor = new ShaderGenerator();
var shaderName = "Hidden/PreviewShader/" + GetType() + guid.ToString();
//var shaderName = "Hidden/PreviewShader/" + this.GetVariableNameForSlot(this.GetOutputSlots<MaterialSlot>().First().id);
var shaderInputVisitor = new ShaderGenerator();
var vertexShaderBlock = new ShaderGenerator();
// always add color because why not.
shaderInputVisitor.AddShaderChunk("float4 color : COLOR;", true);
vertexShaderBlock.AddShaderChunk("float3 worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;", true);
vertexShaderBlock.AddShaderChunk("float3 viewDir = UnityWorldSpaceViewDir(worldPos);", true);
vertexShaderBlock.AddShaderChunk("float4 screenPos = ComputeScreenPos(UnityObjectToClipPos(v.vertex));", true);
vertexShaderBlock.AddShaderChunk("float3 worldNormal = UnityObjectToWorldNormal(v.normal);", true);
bool needBitangent = activeNodeList.OfType<IMayRequireBitangent>().Any(x => x.RequiresBitangent());
bool needsWorldPos = activeNodeList.OfType<IMayRequireViewDirection>().Any(x => x.RequiresViewDirection());
if (needsWorldPos || activeNodeList.OfType<IMayRequireWorldPosition>().Any(x => x.RequiresWorldPosition()))
{
shaderInputVisitor.AddShaderChunk("float3 worldPos : TEXCOORD0;", true);
vertexShaderBlock.AddShaderChunk("o.worldPos = worldPos;", true);
shaderBodyVisitor.AddShaderChunk("float3 " + ShaderGeneratorNames.WorldSpacePosition + " = IN.worldPos;", true);
}
if (needBitangent || activeNodeList.OfType<IMayRequireNormal>().Any(x => x.RequiresNormal()))
{
shaderInputVisitor.AddShaderChunk("float3 worldNormal : TEXCOORD1;", true);
vertexShaderBlock.AddShaderChunk("o.worldNormal = worldNormal;", true);
shaderBodyVisitor.AddShaderChunk("float3 " + ShaderGeneratorNames.WorldSpaceNormal + " = normalize(IN.worldNormal);", true);
}
for (int uvIndex = 0; uvIndex < ShaderGeneratorNames.UVCount; ++uvIndex)
{
var channel = (UVChannel)uvIndex;
if (activeNodeList.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel)))
{
shaderInputVisitor.AddShaderChunk(string.Format("half4 meshUV{0} : TEXCOORD{1};", uvIndex, (uvIndex + 5)), true);
vertexShaderBlock.AddShaderChunk(string.Format("o.meshUV{0} = v.texcoord{1};", uvIndex, uvIndex == 0 ? "" : uvIndex.ToString()), true);
shaderBodyVisitor.AddShaderChunk(string.Format("half4 {0} = IN.meshUV{1};", channel.GetUVName(), uvIndex), true);
}
}
if (activeNodeList.OfType<IMayRequireViewDirection>().Any(x => x.RequiresViewDirection()))
{
shaderBodyVisitor.AddShaderChunk(
"float3 "
+ ShaderGeneratorNames.WorldSpaceViewDirection
+ " = normalize(UnityWorldSpaceViewDir("
+ ShaderGeneratorNames.WorldSpacePosition
+ "));", true);
}
if (activeNodeList.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition()))
{
shaderInputVisitor.AddShaderChunk("float4 screenPos : TEXCOORD3;", true);
vertexShaderBlock.AddShaderChunk("o.screenPos = screenPos;", true);
shaderBodyVisitor.AddShaderChunk("half4 " + ShaderGeneratorNames.ScreenPosition + " = IN.screenPos;", true);
}
if (needBitangent || activeNodeList.OfType<IMayRequireTangent>().Any(x => x.RequiresTangent()))
{
shaderInputVisitor.AddShaderChunk("float4 worldTangent : TEXCOORD4;", true);
vertexShaderBlock.AddShaderChunk("o.worldTangent = float4(UnityObjectToWorldDir(v.tangent.xyz), v.tangent.w);", true);
shaderBodyVisitor.AddShaderChunk("float3 " + ShaderGeneratorNames.WorldSpaceTangent + " = normalize(IN.worldTangent.xyz);", true);
}
if (needBitangent)
{
shaderBodyVisitor.AddShaderChunk(string.Format("float3 {0} = cross({1}, {2}) * IN.worldTangent.w;", ShaderGeneratorNames.WorldSpaceBitangent, ShaderGeneratorNames.WorldSpaceNormal, ShaderGeneratorNames.WorldSpaceTangent), true);
}
if (activeNodeList.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor()))
{
vertexShaderBlock.AddShaderChunk("o.color = v.color;", true);
shaderBodyVisitor.AddShaderChunk("float4 " + ShaderGeneratorNames.VertexColor + " = IN.color;", true);
}
var generationMode = GenerationMode.Preview;
foreach (var activeNode in activeNodeList.OfType<AbstractMaterialNode>())
{
if (activeNode is IGeneratesFunction)
(activeNode as IGeneratesFunction).GenerateNodeFunction(shaderFunctionVisitor, generationMode);
if (activeNode is IGeneratesBodyCode)
(activeNode as IGeneratesBodyCode).GenerateNodeCode(shaderBodyVisitor, generationMode);
activeNode.GeneratePropertyBlock(shaderPropertiesVisitor, generationMode);
activeNode.GeneratePropertyUsages(shaderPropertyUsagesVisitor, generationMode);
}
//shaderBodyVisitor.AddShaderChunk("return " + ShaderGenerator.AdaptNodeOutputForPreview(this, GetOutputSlots<MaterialSlot>().First().id) + ";", true);
//shaderBodyVisitor.AddShaderChunk("return " + AdaptNodeInputForPreview(this, ColorSlotId) + ";", true);
shaderBodyVisitor.AddShaderChunk("return " + GetVariableNameForSlot(GetSlotReference(ColorSlotId).slotId) + ";", true);
ListPool<INode>.Release(activeNodeList);
template = template.Replace("${ShaderName}", shaderName);
template = template.Replace("${ShaderPropertiesHeader}", shaderPropertiesVisitor.GetShaderString(2));
template = template.Replace("${ShaderPropertyUsages}", shaderPropertyUsagesVisitor.GetShaderString(3));
template = template.Replace("${ShaderInputs}", shaderInputVisitor.GetShaderString(4));
template = template.Replace("${ShaderFunctions}", shaderFunctionVisitor.GetShaderString(3));
template = template.Replace("${VertexShaderBody}", vertexShaderBlock.GetShaderString(4));
template = template.Replace("${PixelShaderBody}", shaderBodyVisitor.GetShaderString(4));
string vertexShaderBody = vertexShaderBlock.GetShaderString(4);
if (vertexShaderBody.Length > 0)
{
template = template.Replace("${VertexShaderDecl}", "vertex:vert");
template = template.Replace("${VertexShaderBody}", vertexShaderBody);
}
else
{
template = template.Replace("${VertexShaderDecl}", "");
template = template.Replace("${VertexShaderBody}", vertexShaderBody);
}
configuredTextures = shaderPropertiesVisitor.GetConfiguredTexutres();
return Regex.Replace(template, @"\r\n|\n\r|\n|\r", Environment.NewLine);
*/
//=============================================================================================
string templateLocation = ShaderGenerator.GetTemplatePath("ExportTexture.template");
if (!File.Exists(templateLocation))
{
configuredTextures = new List<PropertyGenerator.TextureInfo>();
return string.Empty;
}
var shaderPropertiesVisitor = new PropertyGenerator();
// figure out what kind of preview we want!
var activeNodeList = ListPool<INode>.Get();
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this);
foreach (var node in activeNodeList.OfType<AbstractMaterialNode>())
{
//if (node is IGeneratesFunction)
// (node as IGeneratesFunction).GenerateNodeFunction(nodeFunction, mode);
node.GeneratePropertyBlock(shaderPropertiesVisitor, mode);
}
string texName = "_ExportTexture";
if (shaderPropertiesVisitor.GetConfiguredTexutres().Count > 0)
texName = shaderPropertiesVisitor.GetConfiguredTexutres()[0].name;
string template = File.ReadAllText(templateLocation);
var shaderName = "Hidden/ExportTexture/" + this.GetVariableNameForSlot(this.GetInputSlots<MaterialSlot>().First().id);
template = template.Replace("${ShaderName}", shaderName);
template = template.Replace("${ShaderPropertiesHeader}", shaderPropertiesVisitor.GetShaderString(0));
template = template.Replace("${ShaderTextureName}", texName);
configuredTextures = shaderPropertiesVisitor.GetConfiguredTexutres();
ListPool<INode>.Release(activeNodeList);
return Regex.Replace(template, @"\r\n|\n\r|\n|\r", Environment.NewLine);
}
}
}

12
MaterialGraphProject/Assets/UnityShaderEditor/Runtime/SurfaceModel/ExportTextureMasterNode.cs.meta


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