Singularity/Library/PackageCache/com.unity.render-pipelines..../Editor/2D/ShaderGraph/Targets/UniversalSpriteLitSubTarget.cs

338 lines
13 KiB
C#
Raw Normal View History

2024-05-06 14:45:45 -04:00
using System;
using System.Linq;
using System.Collections.Generic;
using UnityEditor.ShaderGraph;
using UnityEditor.ShaderGraph.Legacy;
using UnityEditor.UIElements;
namespace UnityEditor.Rendering.Universal.ShaderGraph
{
sealed class UniversalSpriteLitSubTarget : SubTarget<UniversalTarget>, ILegacyTarget
{
static readonly GUID kSourceCodeGuid = new GUID("ea1514729d7120344b27dcd67fbf34de"); // UniversalSpriteLitSubTarget.cs
public UniversalSpriteLitSubTarget()
{
displayName = "Sprite Lit";
}
public override bool IsActive() => true;
public override void Setup(ref TargetSetupContext context)
{
context.AddAssetDependency(kSourceCodeGuid, AssetCollection.Flags.SourceDependency);
context.AddSubShader(SubShaders.SpriteLit(target));
}
public override void GetFields(ref TargetFieldContext context)
{
// Only support SpriteColor legacy block if BaseColor/Alpha are not active
var descs = context.blocks.Select(x => x.descriptor);
bool useLegacyBlocks = !descs.Contains(BlockFields.SurfaceDescription.BaseColor) && !descs.Contains(BlockFields.SurfaceDescription.Alpha);
context.AddField(CoreFields.UseLegacySpriteBlocks, useLegacyBlocks);
// Surface Type
context.AddField(UniversalFields.SurfaceTransparent);
context.AddField(Fields.DoubleSided);
// Blend Mode
switch (target.alphaMode)
{
case AlphaMode.Premultiply:
context.AddField(UniversalFields.BlendPremultiply);
break;
case AlphaMode.Additive:
context.AddField(UniversalFields.BlendAdd);
break;
case AlphaMode.Multiply:
context.AddField(UniversalFields.BlendMultiply);
break;
default:
context.AddField(Fields.BlendAlpha);
break;
}
}
public override void GetActiveBlocks(ref TargetActiveBlockContext context)
{
// Only support SpriteColor legacy block if BaseColor/Alpha are not active
bool useLegacyBlocks = !context.currentBlocks.Contains(BlockFields.SurfaceDescription.BaseColor) && !context.currentBlocks.Contains(BlockFields.SurfaceDescription.Alpha);
context.AddBlock(BlockFields.SurfaceDescriptionLegacy.SpriteColor, useLegacyBlocks);
context.AddBlock(UniversalBlockFields.SurfaceDescription.SpriteMask);
context.AddBlock(BlockFields.SurfaceDescription.NormalTS);
context.AddBlock(BlockFields.SurfaceDescription.Alpha);
}
public override void GetPropertiesGUI(ref TargetPropertyGUIContext context, Action onChange, Action<String> registerUndo)
{
context.AddProperty("Blending Mode", new EnumField(AlphaMode.Alpha) { value = target.alphaMode }, (evt) =>
{
if (Equals(target.alphaMode, evt.newValue))
return;
registerUndo("Change Blend");
target.alphaMode = (AlphaMode)evt.newValue;
onChange();
});
}
public bool TryUpgradeFromMasterNode(IMasterNode1 masterNode, out Dictionary<BlockFieldDescriptor, int> blockMap)
{
blockMap = null;
if (!(masterNode is SpriteLitMasterNode1 spriteLitMasterNode))
return false;
// Set blockmap
blockMap = new Dictionary<BlockFieldDescriptor, int>()
{
{ BlockFields.VertexDescription.Position, 9 },
{ BlockFields.VertexDescription.Normal, 10 },
{ BlockFields.VertexDescription.Tangent, 11 },
{ BlockFields.SurfaceDescriptionLegacy.SpriteColor, 0 },
{ UniversalBlockFields.SurfaceDescription.SpriteMask, 1 },
{ BlockFields.SurfaceDescription.NormalTS, 2 },
};
return true;
}
#region SubShader
static class SubShaders
{
public static SubShaderDescriptor SpriteLit(UniversalTarget target)
{
SubShaderDescriptor result = new SubShaderDescriptor()
{
pipelineTag = UniversalTarget.kPipelineTag,
customTags = UniversalTarget.kLitMaterialTypeTag,
renderType = $"{RenderType.Transparent}",
renderQueue = $"{UnityEditor.ShaderGraph.RenderQueue.Transparent}",
generatesPreview = true,
passes = new PassCollection
{
{ SpriteLitPasses.Lit },
{ SpriteLitPasses.Normal },
// Currently neither of these passes (selection/picking) can be last for the game view for
// UI shaders to render correctly. Verify [1352225] before changing this order.
{ CorePasses._2DSceneSelection(target) },
{ CorePasses._2DScenePicking(target) },
{ SpriteLitPasses.Forward },
},
};
return result;
}
}
#endregion
#region Passes
static class SpriteLitPasses
{
public static PassDescriptor Lit = new PassDescriptor
{
// Definition
displayName = "Sprite Lit",
referenceName = "SHADERPASS_SPRITELIT",
lightMode = "Universal2D",
useInPreview = true,
// Template
passTemplatePath = GenerationUtils.GetDefaultTemplatePath("PassMesh.template"),
sharedTemplateDirectories = GenerationUtils.GetDefaultSharedTemplateDirectories(),
// Port Mask
validVertexBlocks = CoreBlockMasks.Vertex,
validPixelBlocks = SpriteLitBlockMasks.FragmentLit,
// Fields
structs = CoreStructCollections.Default,
requiredFields = SpriteLitRequiredFields.Lit,
fieldDependencies = CoreFieldDependencies.Default,
// Conditional State
renderStates = CoreRenderStates.Default,
pragmas = CorePragmas._2DDefault,
keywords = SpriteLitKeywords.Lit,
includes = SpriteLitIncludes.Lit,
// Custom Interpolator Support
customInterpolators = CoreCustomInterpDescriptors.Common
};
public static PassDescriptor Normal = new PassDescriptor
{
// Definition
displayName = "Sprite Normal",
referenceName = "SHADERPASS_SPRITENORMAL",
lightMode = "NormalsRendering",
useInPreview = true,
// Template
passTemplatePath = GenerationUtils.GetDefaultTemplatePath("PassMesh.template"),
sharedTemplateDirectories = GenerationUtils.GetDefaultSharedTemplateDirectories(),
// Port Mask
validVertexBlocks = CoreBlockMasks.Vertex,
validPixelBlocks = SpriteLitBlockMasks.FragmentForwardNormal,
// Fields
structs = CoreStructCollections.Default,
requiredFields = SpriteLitRequiredFields.Normal,
fieldDependencies = CoreFieldDependencies.Default,
// Conditional State
renderStates = CoreRenderStates.Default,
pragmas = CorePragmas._2DDefault,
includes = SpriteLitIncludes.Normal,
// Custom Interpolator Support
customInterpolators = CoreCustomInterpDescriptors.Common
};
public static PassDescriptor Forward = new PassDescriptor
{
// Definition
displayName = "Sprite Forward",
referenceName = "SHADERPASS_SPRITEFORWARD",
lightMode = "UniversalForward",
useInPreview = true,
// Template
passTemplatePath = GenerationUtils.GetDefaultTemplatePath("PassMesh.template"),
sharedTemplateDirectories = GenerationUtils.GetDefaultSharedTemplateDirectories(),
// Port Mask
validVertexBlocks = CoreBlockMasks.Vertex,
validPixelBlocks = SpriteLitBlockMasks.FragmentForwardNormal,
// Fields
structs = CoreStructCollections.Default,
requiredFields = SpriteLitRequiredFields.Forward,
keywords = SpriteLitKeywords.Forward,
fieldDependencies = CoreFieldDependencies.Default,
// Conditional State
renderStates = CoreRenderStates.Default,
pragmas = CorePragmas._2DDefault,
includes = SpriteLitIncludes.Forward,
// Custom Interpolator Support
customInterpolators = CoreCustomInterpDescriptors.Common
};
}
#endregion
#region PortMasks
static class SpriteLitBlockMasks
{
public static BlockFieldDescriptor[] FragmentLit = new BlockFieldDescriptor[]
{
BlockFields.SurfaceDescription.BaseColor,
BlockFields.SurfaceDescriptionLegacy.SpriteColor,
BlockFields.SurfaceDescription.Alpha,
UniversalBlockFields.SurfaceDescription.SpriteMask,
};
public static BlockFieldDescriptor[] FragmentForwardNormal = new BlockFieldDescriptor[]
{
BlockFields.SurfaceDescription.BaseColor,
BlockFields.SurfaceDescriptionLegacy.SpriteColor,
BlockFields.SurfaceDescription.Alpha,
BlockFields.SurfaceDescription.NormalTS,
};
}
#endregion
#region RequiredFields
static class SpriteLitRequiredFields
{
public static FieldCollection Lit = new FieldCollection()
{
StructFields.Varyings.color,
StructFields.Varyings.positionWS,
StructFields.Varyings.texCoord0,
StructFields.Varyings.screenPosition,
};
public static FieldCollection Normal = new FieldCollection()
{
StructFields.Varyings.normalWS,
StructFields.Varyings.tangentWS,
};
public static FieldCollection Forward = new FieldCollection()
{
StructFields.Varyings.color,
StructFields.Varyings.positionWS,
StructFields.Varyings.texCoord0,
};
}
#endregion
#region Keywords
static class SpriteLitKeywords
{
public static KeywordCollection Lit = new KeywordCollection
{
{ CoreKeywordDescriptors.ShapeLightType0 },
{ CoreKeywordDescriptors.ShapeLightType1 },
{ CoreKeywordDescriptors.ShapeLightType2 },
{ CoreKeywordDescriptors.ShapeLightType3 },
{ CoreKeywordDescriptors.DebugDisplay },
};
public static KeywordCollection Forward = new KeywordCollection
{
{ CoreKeywordDescriptors.DebugDisplay },
};
}
#endregion
#region Includes
static class SpriteLitIncludes
{
const string k2DLightingUtil = "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl";
const string k2DNormal = "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/NormalsRenderingShared.hlsl";
const string kSpriteLitPass = "Packages/com.unity.render-pipelines.universal/Editor/2D/ShaderGraph/Includes/SpriteLitPass.hlsl";
const string kSpriteNormalPass = "Packages/com.unity.render-pipelines.universal/Editor/2D/ShaderGraph/Includes/SpriteNormalPass.hlsl";
const string kSpriteForwardPass = "Packages/com.unity.render-pipelines.universal/Editor/2D/ShaderGraph/Includes/SpriteForwardPass.hlsl";
public static IncludeCollection Lit = new IncludeCollection
{
// Pre-graph
{ CoreIncludes.CorePregraph },
{ CoreIncludes.ShaderGraphPregraph },
{ k2DLightingUtil, IncludeLocation.Pregraph },
// Post-graph
{ CoreIncludes.CorePostgraph },
{ kSpriteLitPass, IncludeLocation.Postgraph },
};
public static IncludeCollection Normal = new IncludeCollection
{
// Pre-graph
{ CoreIncludes.CorePregraph },
{ CoreIncludes.ShaderGraphPregraph },
{ k2DNormal, IncludeLocation.Pregraph },
// Post-graph
{ CoreIncludes.CorePostgraph },
{ kSpriteNormalPass, IncludeLocation.Postgraph },
};
public static IncludeCollection Forward = new IncludeCollection
{
// Pre-graph
{ CoreIncludes.CorePregraph },
{ CoreIncludes.ShaderGraphPregraph },
// Post-graph
{ CoreIncludes.CorePostgraph },
{ kSpriteForwardPass, IncludeLocation.Postgraph },
};
}
#endregion
}
}