浏览代码

Added BlendMode

Soft Light need to be fixed.
Still missing the rest blend mode. (Will add again tomorro).
/main
RinaldoTjan 8 年前
当前提交
a194067c
共有 9 个文件被更改,包括 315 次插入0 次删除
  1. 184
      MaterialGraphProject/Assets/Rinaldo/BlendModeNode.cs
  2. 12
      MaterialGraphProject/Assets/Rinaldo/BlendModeNode.cs.meta
  3. 42
      MaterialGraphProject/Assets/Rinaldo/BlendModesEnum.cs
  4. 12
      MaterialGraphProject/Assets/Rinaldo/BlendModesEnum.cs.meta
  5. 9
      MaterialGraphProject/Assets/Rinaldo/Editor.meta
  6. 44
      MaterialGraphProject/Assets/Rinaldo/Editor/BlendModeNodePresenter.cs
  7. 12
      MaterialGraphProject/Assets/Rinaldo/Editor/BlendModeNodePresenter.cs.meta

184
MaterialGraphProject/Assets/Rinaldo/BlendModeNode.cs


using UnityEngine.Graphing;
namespace UnityEngine.MaterialGraph
{
[Title("Art/BlendMode")]
public class BlendModeNode : Function2Input, IGeneratesFunction
{
public BlendModeNode()
{
name = "BlendMode";
}
protected override string GetFunctionName()
{
return "unity_blendmode_" + precision;
}
[SerializeField]
private BlendModesEnum m_BlendMode;
public BlendModesEnum blendMode
{
get { return m_BlendMode; }
set
{
if (m_BlendMode == value)
return;
m_BlendMode = value;
if (onModified != null)
{
onModified(this, ModificationScope.Graph);
}
}
}
protected override MaterialSlot GetInputSlot1()
{
return new MaterialSlot(InputSlot1Id, GetInputSlot1Name(), kInputSlot1ShaderName, SlotType.Input, SlotValueType.Vector3, Vector4.zero);
}
protected override MaterialSlot GetInputSlot2()
{
return new MaterialSlot(InputSlot2Id, GetInputSlot2Name(), kInputSlot2ShaderName, SlotType.Input, SlotValueType.Vector3, Vector4.zero);
}
protected override MaterialSlot GetOutputSlot()
{
return new MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, SlotType.Output, SlotValueType.Vector3, Vector4.zero);
}
public override bool hasPreview { get { return true; } }
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode)
{
var outputString = new ShaderGenerator();
// var vector3one = precision+ "3(1.0, 1.0, 1.0);
switch (m_BlendMode)
{
case BlendModesEnum.Burn:
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
// outputString.AddShaderChunk("return "+precision+ "3(1.0,1.0,1.0) - (" + precision + "3(1.0,1.0,1.0) - arg2)/arg1;", false);
outputString.AddShaderChunk("return 1.0 - (1.0 - arg2)/arg1;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
break;
case BlendModesEnum.Darken:
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return min(arg2,arg1);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
break;
case BlendModesEnum.Difference:
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return abs(arg2-arg1);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
break;
case BlendModesEnum.Dodge:
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return arg2 / (" + precision + "3(1.0,1.0,1.0) - arg1);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
break;
case BlendModesEnum.Exclusion:
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return arg2 + arg1 - (" + precision + "3(2.0,2.0,2.0)*arg2*arg1);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
break;
case BlendModesEnum.HardLight:
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk(precision + outputDimension + " result1 = 1.0 - 2.0 * (1.0 - arg1) * (1.0 - arg2);", false);
outputString.AddShaderChunk(precision + outputDimension + " result2 = 2.0 * arg1 * arg2;", false);
outputString.AddShaderChunk(precision + outputDimension + " zeroOrOne = step(arg1, 0.5);", false);
outputString.AddShaderChunk("return result2 * zeroOrOne + (1 - zeroOrOne) * result1;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
break;
case BlendModesEnum.Lighten:
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return max(arg2,arg1);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
break;
case BlendModesEnum.Multiply:
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return arg1 * arg2;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
break;
case BlendModesEnum.Negation:
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk("return 1.0 - abs(1.0 - arg2 - arg1);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
break;
case BlendModesEnum.Overlay:
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk(precision + outputDimension + " result1 = 1.0 - 2.0 * (1.0 - arg1) * (1.0 - arg2);", false);
outputString.AddShaderChunk(precision + outputDimension + " result2 = 2.0 * arg1 * arg2;", false);
outputString.AddShaderChunk(precision + outputDimension + " zeroOrOne = step(arg2, 0.5);", false);
outputString.AddShaderChunk("return result2 * zeroOrOne + (1 - zeroOrOne) * result1;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
break;
/* case BlendModesEnum.Overlay:
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk(precision + outputDimension + " result1 = 2.0 * arg1 * arg2;", false);
outputString.AddShaderChunk(precision + outputDimension + " result2 = 1.0 - 2.0 * (1.0 - arg1) * (1.0 - arg2);", false);
outputString.AddShaderChunk(precision + outputDimension + " zeroOrOne = step(arg2, 0.5);", false);
outputString.AddShaderChunk("return result2 * zeroOrOne + (1 - zeroOrOne) * result1;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
break;
*/
case BlendModesEnum.Screen:
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
// outputString.AddShaderChunk("return " + precision + "3(1.0, 1.0, 1.0) - ((" + precision + "3(1.0, 1.0, 1.0)-arg2) * (" + precision + "3(1.0, 1.0, 1.0) - arg1));", false);
outputString.AddShaderChunk("return 1.0 - (1.0-arg2) * (1.0 - arg1);", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
break;
case BlendModesEnum.SoftLight:
outputString.AddShaderChunk(GetFunctionPrototype("arg1", "arg2"), false);
outputString.AddShaderChunk("{", false);
outputString.Indent();
outputString.AddShaderChunk(precision + outputDimension + " result2= 2.0 * arg2 * arg1 + arg2*arg2 - 2.0 * arg2*arg2*arg1;", false);
outputString.AddShaderChunk(precision + outputDimension + " result1= 2.0* sqrt(arg2) * arg1 - sqrt(arg2) + 2.0 * arg2 - 2.0 * arg2*arg1;", false);
outputString.AddShaderChunk(precision + outputDimension + " zeroOrOne = step(arg2, 0.5);", false);
outputString.AddShaderChunk("return result2 * zeroOrOne + (1 - zeroOrOne) * result1;", false);
outputString.Deindent();
outputString.AddShaderChunk("}", false);
break;
}
visitor.AddShaderChunk(outputString.GetShaderString(0), true);
}
}
}

12
MaterialGraphProject/Assets/Rinaldo/BlendModeNode.cs.meta


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

42
MaterialGraphProject/Assets/Rinaldo/BlendModesEnum.cs


namespace UnityEngine.MaterialGraph
{
public enum BlendModesEnum
{
Burn,
Dodge,
Darken,
Difference,
// Divide,
Exclusion,
HardLight,
// HardMix,
Lighten,
// LinearDodge,
// LinearLight,
// LinearBurn,
Multiply,
Negation,
Overlay,
// PinLight,
Screen,
SoftLight,
// Substract,
// VividLight
/* Old Order
Multiply
Screen
Darken
Lighten
Difference
Negation
Exclusion
Overlay
HardLight
SoftLight
Dodge (ColorDodge)
Burn (ColorBurn)
*/
}
}

12
MaterialGraphProject/Assets/Rinaldo/BlendModesEnum.cs.meta


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

9
MaterialGraphProject/Assets/Rinaldo/Editor.meta


fileFormatVersion: 2
guid: 5c0e473d1ac839c408f697d31a4e1c14
folderAsset: yes
timeCreated: 1495456613
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

44
MaterialGraphProject/Assets/Rinaldo/Editor/BlendModeNodePresenter.cs


using System;
using System.Collections.Generic;
using RMGUI.GraphView;
using UnityEditor.Graphing.Drawing;
using UnityEngine.MaterialGraph;
namespace UnityEditor.MaterialGraph.Drawing
{
[Serializable]
class BlendModeNodeControlPresenter : GraphControlPresenter
{
public override void OnGUIHandler()
{
base.OnGUIHandler();
var cNode = node as BlendModeNode;
if (cNode == null)
return;
cNode.blendMode = (BlendModesEnum)EditorGUILayout.EnumPopup("Mode", cNode.blendMode);
}
/* public override float GetHeight()
{
return EditorGUIUtility.singleLineHeight + 2 * EditorGUIUtility.standardVerticalSpacing;
}
*/
public override float GetHeight()
{
return 3 * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing) + EditorGUIUtility.standardVerticalSpacing;
}
}
[Serializable]
public class BlendModeNodePresenter : MaterialNodePresenter
{
protected override IEnumerable<GraphElementPresenter> GetControlData()
{
var instance = CreateInstance<BlendModeNodeControlPresenter>();
instance.Initialize(node);
return new List<GraphElementPresenter> { instance };
}
}
}

12
MaterialGraphProject/Assets/Rinaldo/Editor/BlendModeNodePresenter.cs.meta


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