浏览代码

Merge pull request #131 from Unity-Technologies/LinearColorFix

Linear color fix
/main
GitHub 7 年前
当前提交
d1d69177
共有 20 个文件被更改,包括 135 次插入57 次删除
  1. 42
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorMaterialSlot.cs
  2. 45
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorRGBMaterialSlot.cs
  3. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs
  4. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialGraphAsset.cs
  5. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1MaterialSlot.cs
  6. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2MaterialSlot.cs
  7. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3MaterialSlot.cs
  8. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4MaterialSlot.cs
  9. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IMaterialSlotHasValue.cs.meta
  10. 30
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightPBRSubShader.cs
  11. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/PBRMasterNode.cs
  12. 3
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/UnlitMasterNode.cs
  13. 9
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ColorNode.cs
  14. 4
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Util/ShaderGenerator.cs
  15. 12
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialNodeView.cs
  16. 5
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Implementation/SerializableNode.cs
  17. 2
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightPBRForwardPass.template
  18. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IMaterialSlotHasValue.cs
  19. 8
      MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IMaterialSlotHasVaule.cs
  20. 0
      /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IMaterialSlotHasValue.cs.meta

42
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Slots;
using UnityEngine;

public override VisualElement InstantiateControl()
{
return new ColorRGBASlotControlView(this);
}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return string.Format("IsGammaSpace() ? {0}4({1}, {2}, {3}, {4}) : {0}4 (GammaToLinearSpace({0}3({1}, {2}, {3})), {4})"
, precision
, value.x
, value.y
, value.z
, value.w);
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
var property = new ColorShaderProperty()
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = value
};
properties.AddShaderProperty(property);
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
name = name,
propType = PropertyType.Color,
vector4Value = new Vector4(value.x, value.y, value.z, value.w),
floatValue = value.x,
colorValue = new Vector4(value.x, value.x, value.z, value.w),
};
return pp;
}
}
}

45
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/ColorRGBMaterialSlot.cs


using System;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Slots;
using UnityEngine;

string displayName,
string shaderOutputName,
SlotType slotType,
Vector4 value,
Color value,
: base(slotId, displayName, shaderOutputName, slotType, value, shaderStage, hidden)
: base(slotId, displayName, shaderOutputName, slotType, (Vector4)value, shaderStage, hidden)
{
}

}
protected override string ConcreteSlotValueAsVariable(AbstractMaterialNode.OutputPrecision precision)
{
return string.Format("IsGammaSpace() ? {0}3({1}, {2}, {3}) : GammaToLinearSpace({0}3({1}, {2}, {3}))"
, precision
, value.x
, value.y
, value.z);
}
public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
{
if (!generationMode.IsPreview())
return;
var matOwner = owner as AbstractMaterialNode;
if (matOwner == null)
throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
var property = new ColorShaderProperty()
{
overrideReferenceName = matOwner.GetVariableNameForSlot(id),
generatePropertyBlock = false,
value = new Color(value.x, value.y, value.z)
};
properties.AddShaderProperty(property);
}
public override PreviewProperty GetPreviewProperty(string name)
{
var pp = new PreviewProperty
{
name = name,
propType = PropertyType.Color,
vector4Value = new Vector4(value.x, value.y, value.z, 1),
floatValue = value.x,
colorValue = new Vector4(value.x, value.y, value.z, 1),
};
return pp;
}
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/DynamicVectorMaterialSlot.cs


namespace UnityEditor.ShaderGraph
{
[Serializable]
public class DynamicVectorMaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector4>
public class DynamicVectorMaterialSlot : MaterialSlot, IMaterialSlotHasValue<Vector4>
{
[SerializeField]
private Vector4 m_Value;

4
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/MaterialGraphAsset.cs


{
public static bool ShaderHasError(Shader shader)
{
var hasErrorsCall = typeof(ShaderUtil).GetMethod("GetShaderErrorCount", BindingFlags.Static | BindingFlags.NonPublic);
var result = hasErrorsCall.Invoke(null, new object[] { shader });
return (int)result != 0;
return ShaderUtil.GetShaderErrorCount(shader) > 0;
}
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector1MaterialSlot.cs


namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Vector1MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<float>
public class Vector1MaterialSlot : MaterialSlot, IMaterialSlotHasValue<float>
{
[SerializeField]
private float m_Value;

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector2MaterialSlot.cs


namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Vector2MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector2>
public class Vector2MaterialSlot : MaterialSlot, IMaterialSlotHasValue<Vector2>
{
[SerializeField]
private Vector2 m_Value;

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector3MaterialSlot.cs


namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Vector3MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector3>
public class Vector3MaterialSlot : MaterialSlot, IMaterialSlotHasValue<Vector3>
{
[SerializeField]
private Vector3 m_Value;

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/Vector4MaterialSlot.cs


namespace UnityEditor.ShaderGraph
{
[Serializable]
public class Vector4MaterialSlot : MaterialSlot, IMaterialSlotHasVaule<Vector4>
public class Vector4MaterialSlot : MaterialSlot, IMaterialSlotHasValue<Vector4>
{
[SerializeField]
private Vector4 m_Value;

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IMaterialSlotHasValue.cs.meta


fileFormatVersion: 2
guid: 722107fbcd3d86249ac260090e5cc77e
guid: 94492244ef44a2e48b3e7790d7ac681d
MonoImporter:
externalObjects: {}
serializedVersion: 2

30
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/LightWeightPBRSubShader.cs


}
};
private static void GenerateApplicationVertexInputs(ShaderGraphRequirements graphRequiements, ShaderGenerator vertexInputs, int vertexInputStartIndex, int maxVertexInputs)
private static void GenerateApplicationVertexInputs(ShaderGraphRequirements graphRequiements, ShaderGenerator vertexInputs)
int vertexInputIndex = vertexInputStartIndex;
vertexInputs.AddShaderChunk("struct GraphVertexInput", false);
vertexInputs.AddShaderChunk("{", false);
vertexInputs.Indent();

vertexInputs.AddShaderChunk("float4 lightmapUV : TEXCOORD0;", false);
if (graphRequiements.requiresVertexColor)
{

foreach (var channel in graphRequiements.requiresMeshUVs.Distinct())
{
vertexInputs.AddShaderChunk(string.Format("float4 texcoord{0} : TEXCOORD{1};", ((int)channel).ToString(), vertexInputIndex.ToString()), false);
vertexInputIndex++;
}
vertexInputs.AddShaderChunk(string.Format("float4 texcoord{0} : TEXCOORD{0};", (int)channel), false);
vertexInputs.AddShaderChunk("UNITY_VERTEX_INPUT_INSTANCE_ID", false);
vertexInputs.Deindent();

NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, masterNode, NodeUtils.IncludeSelf.Include, pass.PixelShaderSlots);
var requirements = AbstractMaterialGraph.GetRequirements(activeNodeList);
GenerateApplicationVertexInputs(requirements, vertexInputs, 1, 8);
var modelRequiements = ShaderGraphRequirements.none;
modelRequiements.requiresNormal |= NeededCoordinateSpace.World;
modelRequiements.requiresTangent |= NeededCoordinateSpace.World;
modelRequiements.requiresBitangent |= NeededCoordinateSpace.World;
modelRequiements.requiresPosition |= NeededCoordinateSpace.World;
modelRequiements.requiresViewDir |= NeededCoordinateSpace.World;
modelRequiements.requiresMeshUVs.Add(UVChannel.uv1);
GenerateApplicationVertexInputs(requirements.Union(modelRequiements), vertexInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresNormal, InterpolatorType.Normal, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresTangent, InterpolatorType.Tangent, surfaceInputs);
ShaderGenerator.GenerateSpaceTranslationSurfaceInputs(requirements.requiresBitangent, InterpolatorType.BiTangent, surfaceInputs);

var localSurfaceInputs = new ShaderGenerator();
var surfaceOutputRemap = new ShaderGenerator();
var reqs = ShaderGraphRequirements.none;
reqs.requiresNormal |= NeededCoordinateSpace.World;
reqs.requiresTangent |= NeededCoordinateSpace.World;
reqs.requiresBitangent |= NeededCoordinateSpace.World;
reqs.requiresPosition |= NeededCoordinateSpace.World;
reqs.requiresViewDir |= NeededCoordinateSpace.World;
ShaderGenerator.GenerateStandardTransforms(
3,
10,

localSurfaceInputs,
requirements,
reqs,
modelRequiements,
CoordinateSpace.World);
ShaderGenerator defines = new ShaderGenerator();

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/PBRMasterNode.cs


public sealed override void UpdateNodeAfterDeserialization()
{
name = "PBR Master";
AddSlot(new Vector3MaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, new Vector4(0.5f, 0.5f, 0.5f), ShaderStage.Fragment));
AddSlot(new ColorRGBMaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, Color.grey, ShaderStage.Fragment));
AddSlot(new Vector3MaterialSlot(EmissionSlotId, EmissionSlotName, EmissionSlotName, SlotType.Input, Vector3.zero, ShaderStage.Fragment));
AddSlot(new ColorRGBMaterialSlot(EmissionSlotId, EmissionSlotName, EmissionSlotName, SlotType.Input, Color.black, ShaderStage.Fragment));
AddSlot(new Vector3MaterialSlot(SpecularSlotId, SpecularSlotName, SpecularSlotName, SlotType.Input, Vector3.zero, ShaderStage.Fragment));
AddSlot(new ColorRGBMaterialSlot(SpecularSlotId, SpecularSlotName, SpecularSlotName, SlotType.Input, Color.grey, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(SmoothnessSlotId, SmoothnessSlotName, SmoothnessSlotName, SlotType.Input, 0.5f, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(OcclusionSlotId, OcclusionSlotName, OcclusionSlotName, SlotType.Input, 1f, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, 1f, ShaderStage.Fragment));

SmoothnessSlotId,
OcclusionSlotId,
AlphaSlotId
});
}, true);
}
public override string GetShader(GenerationMode mode, string outputName, out List<PropertyCollector.TextureInfo> configuredTextures)

3
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/LightweightPipeline/UnlitMasterNode.cs


using System.Collections.Generic;
using System.Linq;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;
namespace UnityEditor.ShaderGraph

public sealed override void UpdateNodeAfterDeserialization()
{
name = "Unlit Master";
AddSlot(new Vector3MaterialSlot(ColorSlotId, ColorSlotName, ColorSlotName, SlotType.Input, new Vector3(0.5f, 0.5f, 0.5f), ShaderStage.Fragment));
AddSlot(new ColorRGBMaterialSlot(ColorSlotId, ColorSlotName, ColorSlotName, SlotType.Input, Color.grey, ShaderStage.Fragment));
AddSlot(new Vector1MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, 1, ShaderStage.Fragment));
// clear out slot names that do not match the slots

9
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Nodes/Input/Basic/ColorNode.cs


if (generationMode.IsPreview())
return;
visitor.AddShaderChunk(precision + "4 " + GetVariableNameForNode() + " = " + precision + "4 (" + color.r + ", " + color.g + ", " + color.b + ", " + color.a + ");", true);
visitor.AddShaderChunk(string.Format(
@"{0}4 {1} = IsGammaSpace() ? {0}4({2}, {3}, {4}, {5}) : {0}4(GammaToLinearSpace({0}3({2}, {3}, {4})), {5});"
, precision
, GetVariableNameForNode()
, color.r
, color.g
, color.b
, color.a), true);
}
public override string GetVariableNameForSlot(int slotId)

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


if (graphRequiements.requiresScreenPosition)
surfaceInputs.AddShaderChunk(string.Format("surfaceInput.{0} = {0};", ShaderGeneratorNames.ScreenPosition), false);
foreach (var channel in combinedRequierments.requiresMeshUVs.Distinct())
surfaceInputs.AddShaderChunk(string.Format("surfaceInput.{0} ={0};", channel.GetUVName()), false);
foreach (var channel in graphRequiements.requiresMeshUVs.Distinct())
surfaceInputs.AddShaderChunk(string.Format("surfaceInput.{0} = {0};", channel.GetUVName()), false);
}
public enum Dimension

12
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Views/MaterialNodeView.cs


{
var slots = node.GetSlots<MaterialSlot>().ToList();
var anchorsToRemove = new List<VisualElement>();
foreach (var anchor in inputContainer.Children())
if (!slots.Contains(anchor.userData as MaterialSlot))
anchorsToRemove.Add(anchor);
var anchorsToRemove = inputContainer.Children().ToList();
foreach (var anchorElement in anchorsToRemove)
{
inputContainer.Remove(anchorElement);

}
}
anchorsToRemove.Clear();
foreach (var anchor in outputContainer.Children())
if (!slots.Contains(anchor.userData as MaterialSlot))
anchorsToRemove.Add(anchor);
anchorsToRemove = outputContainer.Children().ToList();
foreach (var ve in anchorsToRemove)
outputContainer.Remove(ve);

port.portName = slot.displayName;
}
AddSlots(slots.Except(inputContainer.Children().Concat(outputContainer.Children()).Select(data => data.userData as MaterialSlot)));
AddSlots(slots);
if (inputContainer.childCount > 0)
inputContainer.Sort((x, y) => slots.IndexOf(x.userData as MaterialSlot) - slots.IndexOf(y.userData as MaterialSlot));

5
MaterialGraphProject/Assets/UnityShaderEditor/Editor/SerializableGraph/Implementation/SerializableNode.cs


}
}
public void RemoveSlotsNameNotMatching(IEnumerable<int> slotIds)
public void RemoveSlotsNameNotMatching(IEnumerable<int> slotIds, bool supressWarnings = false)
Debug.LogWarningFormat("Removing Invalid MaterialSlot: {0}", invalidSlot);
if (!supressWarnings)
Debug.LogWarningFormat("Removing Invalid MaterialSlot: {0}", invalidSlot);
RemoveSlot(invalidSlot);
}
}

2
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/lightweightPBRForwardPass.template


float4 clipPos = mul(UNITY_MATRIX_VP, lwWorldPos);
#ifdef LIGHTMAP_ON
o.lightmapUV.zw = v.lightmapUV * unity_LightmapST.xy + unity_LightmapST.zw;
o.lightmapUV.zw = v.texcoord1 * unity_LightmapST.xy + unity_LightmapST.zw;
#else
o.vertexSH = half4(EvaluateSHPerVertex(lwWNormal), 0.0);
#endif

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IMaterialSlotHasValue.cs


namespace UnityEditor.ShaderGraph
{
public interface IMaterialSlotHasValue<T>
{
T defaultValue { get; }
T value { get; }
}
}

8
MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IMaterialSlotHasVaule.cs


namespace UnityEditor.ShaderGraph
{
public interface IMaterialSlotHasVaule<T>
{
T defaultValue { get; }
T value { get; }
}
}

/MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IMaterialSlotHasVaule.cs.meta → /MaterialGraphProject/Assets/UnityShaderEditor/Editor/Data/Graphs/IMaterialSlotHasValue.cs.meta

正在加载...
取消
保存