123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- using System;
- using System.Collections.Generic;
- using UnityEditor.ShaderGraph;
- using UnityEngine.Rendering;
- using UnityEngine.Rendering.Universal;
- using UnityEditor.Rendering.Universal;
- using Data.Util;
- using UnityEditor.ShaderGraph.Internal;
- namespace UnityEditor.Experimental.Rendering.Universal
- {
- [Serializable]
- [FormerName("UnityEditor.Experimental.Rendering.LWRP.LightWeightSpriteUnlitSubShader")]
- class UniversalSpriteUnlitSubShader : ISpriteUnlitSubShader
- {
- #region Passes
- ShaderPass m_UnlitPass = new ShaderPass
- {
- // Definition
- referenceName = "SHADERPASS_SPRITEUNLIT",
- passInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/SpriteUnlitPass.hlsl",
- varyingsInclude = "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/Varyings.hlsl",
- useInPreview = true,
- // Port mask
- vertexPorts = new List<int>()
- {
- SpriteUnlitMasterNode.PositionSlotId,
- SpriteUnlitMasterNode.VertNormalSlotId,
- SpriteUnlitMasterNode.VertTangentSlotId
- },
- pixelPorts = new List<int>
- {
- SpriteUnlitMasterNode.ColorSlotId,
- },
- // Required fields
- requiredAttributes = new List<string>()
- {
- "Attributes.color",
- "Attributes.uv0",
- },
- requiredVaryings = new List<string>()
- {
- "Varyings.color",
- "Varyings.texCoord0",
- },
-
- // Pass setup
- includes = new List<string>()
- {
- "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl",
- "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl",
- "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl",
- "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl",
- },
- pragmas = new List<string>()
- {
- "prefer_hlslcc gles",
- "exclude_renderers d3d11_9x",
- "target 2.0",
- },
- keywords = new KeywordDescriptor[]
- {
- s_ETCExternalAlphaKeyword,
- },
- };
- #endregion
- #region Keywords
- static KeywordDescriptor s_ETCExternalAlphaKeyword = new KeywordDescriptor()
- {
- displayName = "ETC External Alpha",
- referenceName = "ETC1_EXTERNAL_ALPHA",
- type = KeywordType.Boolean,
- definition = KeywordDefinition.MultiCompile,
- scope = KeywordScope.Global,
- };
- #endregion
- public int GetPreviewPassIndex() { return 0; }
- private static ActiveFields GetActiveFieldsFromMasterNode(SpriteUnlitMasterNode masterNode, ShaderPass pass)
- {
- var activeFields = new ActiveFields();
- var baseActiveFields = activeFields.baseInstance;
- // Graph Vertex
- if(masterNode.IsSlotConnected(SpriteUnlitMasterNode.PositionSlotId) ||
- masterNode.IsSlotConnected(SpriteUnlitMasterNode.VertNormalSlotId) ||
- masterNode.IsSlotConnected(SpriteUnlitMasterNode.VertTangentSlotId))
- {
- baseActiveFields.Add("features.graphVertex");
- }
- // Graph Pixel (always enabled)
- baseActiveFields.Add("features.graphPixel");
- baseActiveFields.Add("SurfaceType.Transparent");
- baseActiveFields.Add("BlendMode.Alpha");
- return activeFields;
- }
- private static bool GenerateShaderPass(SpriteUnlitMasterNode masterNode, ShaderPass pass, GenerationMode mode, ShaderGenerator result, List<string> sourceAssetDependencyPaths)
- {
- UniversalShaderGraphUtilities.SetRenderState(SurfaceType.Transparent, AlphaMode.Alpha, true, ref pass);
- // apply master node options to active fields
- var activeFields = GetActiveFieldsFromMasterNode(masterNode, pass);
- // use standard shader pass generation
- return ShaderGraph.GenerationUtils.GenerateShaderPass(masterNode, pass, mode, activeFields, result, sourceAssetDependencyPaths,
- UniversalShaderGraphResources.s_Dependencies, UniversalShaderGraphResources.s_ResourceClassName, UniversalShaderGraphResources.s_AssemblyName);
- }
- public string GetSubshader(IMasterNode masterNode, GenerationMode mode, List<string> sourceAssetDependencyPaths = null)
- {
- if (sourceAssetDependencyPaths != null)
- {
- // LightWeightSpriteUnlitSubShader.cs
- sourceAssetDependencyPaths.Add(AssetDatabase.GUIDToAssetPath("3ef30c5c1d5fc412f88511ef5818b654"));
- }
- // Master Node data
- var unlitMasterNode = masterNode as SpriteUnlitMasterNode;
- var subShader = new ShaderGenerator();
- subShader.AddShaderChunk("SubShader", true);
- subShader.AddShaderChunk("{", true);
- subShader.Indent();
- {
- var surfaceTags = ShaderGenerator.BuildMaterialTags(SurfaceType.Transparent);
- var tagsBuilder = new ShaderStringBuilder(0);
- surfaceTags.GetTags(tagsBuilder, "UniversalPipeline");
- subShader.AddShaderChunk(tagsBuilder.ToString());
- GenerateShaderPass(unlitMasterNode, m_UnlitPass, mode, subShader, sourceAssetDependencyPaths);
- }
- subShader.Deindent();
- subShader.AddShaderChunk("}", true);
- return subShader.GetShaderString(0);
- }
- public bool IsPipelineCompatible(RenderPipelineAsset renderPipelineAsset)
- {
- return renderPipelineAsset is UniversalRenderPipelineAsset;
- }
- public UniversalSpriteUnlitSubShader() { }
- }
- }
|