vlad
8 年前
当前提交
09332619
共有 11 个文件被更改,包括 362 次插入 和 0 次删除
-
1MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs
-
1MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs
-
6MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SlotValue.cs
-
52MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/TextureAssetPresenter.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/TextureAssetPresenter.cs.meta
-
152MaterialGraphProject/Assets/Vlad/TextureAssetNode.cs
-
12MaterialGraphProject/Assets/Vlad/TextureAssetNode.cs.meta
-
112MaterialGraphProject/Assets/Vlad/UVTriPlanar.cs
-
12MaterialGraphProject/Assets/Vlad/UVTriPlanar.cs.meta
|
|||
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 }; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: fea6f3daed9e37042828a425483c9a5c |
|||
timeCreated: 1495541485 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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; } } |
|||
|
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 9def76d530f56804b9f696a9b36eccfa |
|||
timeCreated: 1495535587 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: c355f376dad60404fbfd488ec80b88a4 |
|||
timeCreated: 1495485045 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue