Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CustomRenderTextureTarget.cs 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. using UnityEngine.UIElements;
  7. using UnityEditor;
  8. using UnityEditor.ShaderGraph;
  9. using UnityEditor.UIElements;
  10. using UnityEditor.ShaderGraph.Serialization;
  11. using SubTargetListPool = UnityEngine.Rendering.ListPool<UnityEditor.ShaderGraph.SubTarget>;
  12. using System.Reflection;
  13. namespace UnityEditor.Rendering.CustomRenderTexture.ShaderGraph
  14. {
  15. [GenerateBlocks]
  16. internal struct FullScreenBlocks
  17. {
  18. // TODO: use base color and alpha blocks
  19. public static BlockFieldDescriptor colorBlock = new BlockFieldDescriptor(String.Empty, "Color", "Color",
  20. new ColorRGBAControl(UnityEngine.Color.white), ShaderStage.Fragment);
  21. }
  22. sealed class CustomRenderTextureTarget : Target
  23. {
  24. // Constants
  25. const string kAssetGuid = "a0bae34258e39cd4899b63278c24c086"; // FullscreenPassTarget.cs
  26. // SubTarget
  27. List<SubTarget> m_SubTargets;
  28. List<string> m_SubTargetNames;
  29. int activeSubTargetIndex => m_SubTargets.IndexOf(m_ActiveSubTarget);
  30. // View
  31. PopupField<string> m_SubTargetField;
  32. TextField m_CustomGUIField;
  33. [SerializeField]
  34. JsonData<SubTarget> m_ActiveSubTarget;
  35. [SerializeField]
  36. string m_CustomEditorGUI;
  37. public CustomRenderTextureTarget()
  38. {
  39. displayName = "Custom Render Texture";
  40. isHidden = false;
  41. m_SubTargets = GetSubTargets(this);
  42. m_SubTargetNames = m_SubTargets.Select(x => x.displayName).ToList();
  43. ProcessSubTargetList(ref m_ActiveSubTarget, ref m_SubTargets);
  44. }
  45. public static void ProcessSubTargetList(ref JsonData<SubTarget> activeSubTarget, ref List<SubTarget> subTargets)
  46. {
  47. if (subTargets == null || subTargets.Count == 0)
  48. return;
  49. // assign the initial sub-target, if none is assigned yet
  50. if (activeSubTarget.value == null)
  51. activeSubTarget = subTargets[0];
  52. // Update SubTarget list with active SubTarget
  53. var activeSubTargetType = activeSubTarget.value.GetType();
  54. var activeSubTargetCurrent = subTargets.FirstOrDefault(x => x.GetType() == activeSubTargetType);
  55. var index = subTargets.IndexOf(activeSubTargetCurrent);
  56. subTargets[index] = activeSubTarget;
  57. }
  58. public static List<SubTarget> GetSubTargets<T>(T target) where T : Target
  59. {
  60. // Get Variants
  61. var subTargets = SubTargetListPool.Get();
  62. var typeCollection = TypeCache.GetTypesDerivedFrom<SubTarget>();
  63. foreach (var type in typeCollection)
  64. {
  65. if (type.IsAbstract || !type.IsClass)
  66. continue;
  67. var subTarget = (SubTarget)Activator.CreateInstance(type);
  68. if (!subTarget.isHidden && subTarget.targetType.Equals(typeof(T)))
  69. {
  70. subTarget.target = target;
  71. subTargets.Add(subTarget);
  72. }
  73. }
  74. return subTargets;
  75. }
  76. public SubTarget activeSubTarget
  77. {
  78. get => m_ActiveSubTarget;
  79. set => m_ActiveSubTarget = value;
  80. }
  81. public string customEditorGUI
  82. {
  83. get => m_CustomEditorGUI;
  84. set => m_CustomEditorGUI = value;
  85. }
  86. public override bool IsActive() => activeSubTarget.IsActive();
  87. public override void Setup(ref TargetSetupContext context)
  88. {
  89. // Setup the Target
  90. context.AddAssetDependency(new GUID(kAssetGuid), AssetCollection.Flags.SourceDependency);
  91. // Setup the active SubTarget
  92. ProcessSubTargetList(ref m_ActiveSubTarget, ref m_SubTargets);
  93. m_ActiveSubTarget.value.target = this;
  94. m_ActiveSubTarget.value.Setup(ref context);
  95. // Override EditorGUI
  96. if (!string.IsNullOrEmpty(m_CustomEditorGUI))
  97. {
  98. context.SetDefaultShaderGUI(m_CustomEditorGUI);
  99. }
  100. }
  101. public override void GetFields(ref TargetFieldContext context)
  102. {
  103. var descs = context.blocks.Select(x => x.descriptor);
  104. // Core fields
  105. context.AddField(Fields.GraphVertex, descs.Contains(BlockFields.VertexDescription.Position) ||
  106. descs.Contains(BlockFields.VertexDescription.Normal) ||
  107. descs.Contains(BlockFields.VertexDescription.Tangent));
  108. context.AddField(Fields.GraphPixel);
  109. // SubTarget fields
  110. m_ActiveSubTarget.value.GetFields(ref context);
  111. }
  112. public override void GetActiveBlocks(ref TargetActiveBlockContext context)
  113. {
  114. // SubTarget blocks
  115. m_ActiveSubTarget.value.GetActiveBlocks(ref context);
  116. }
  117. public override void GetPropertiesGUI(ref TargetPropertyGUIContext context, Action onChange, Action<String> registerUndo)
  118. {
  119. // Core properties
  120. m_SubTargetField = new PopupField<string>(m_SubTargetNames, activeSubTargetIndex);
  121. context.AddProperty("Material", m_SubTargetField, (evt) =>
  122. {
  123. if (Equals(activeSubTargetIndex, m_SubTargetField.index))
  124. return;
  125. registerUndo("Change Material");
  126. m_ActiveSubTarget = m_SubTargets[m_SubTargetField.index];
  127. onChange();
  128. });
  129. // SubTarget properties
  130. m_ActiveSubTarget.value.GetPropertiesGUI(ref context, onChange, registerUndo);
  131. // Custom Editor GUI
  132. // Requires FocusOutEvent
  133. m_CustomGUIField = new TextField("") { value = customEditorGUI };
  134. m_CustomGUIField.RegisterCallback<FocusOutEvent>(s =>
  135. {
  136. if (Equals(customEditorGUI, m_CustomGUIField.value))
  137. return;
  138. registerUndo("Change Custom Editor GUI");
  139. customEditorGUI = m_CustomGUIField.value;
  140. onChange();
  141. });
  142. context.AddProperty("Custom Editor GUI", m_CustomGUIField, (evt) => { });
  143. }
  144. public bool TrySetActiveSubTarget(Type subTargetType)
  145. {
  146. if (!subTargetType.IsSubclassOf(typeof(SubTarget)))
  147. return false;
  148. foreach (var subTarget in m_SubTargets)
  149. {
  150. if (subTarget.GetType().Equals(subTargetType))
  151. {
  152. m_ActiveSubTarget = subTarget;
  153. return true;
  154. }
  155. }
  156. return false;
  157. }
  158. public override bool WorksWithSRP(RenderPipelineAsset scriptableRenderPipeline) => true;
  159. public override bool IsNodeAllowedByTarget(System.Type nodeType)
  160. {
  161. SRPFilterAttribute srpFilter = NodeClassCache.GetAttributeOnNodeType<SRPFilterAttribute>(nodeType);
  162. bool allowed = true;
  163. if (srpFilter != null)
  164. allowed = false;
  165. return allowed && nodeType != typeof(MainLightDirectionNode);
  166. }
  167. }
  168. static class FullscreePasses
  169. {
  170. public static PassDescriptor CustomRenderTexture = new PassDescriptor
  171. {
  172. // Definition
  173. referenceName = "SHADERPASS_CUSTOM_RENDER_TEXTURE",
  174. useInPreview = true,
  175. // Template
  176. passTemplatePath = AssetDatabase.GUIDToAssetPath("afa536a0de48246de92194c9e987b0b8"), // CustomTextureSubShader.template
  177. // Port Mask
  178. validVertexBlocks = new BlockFieldDescriptor[]
  179. {
  180. BlockFields.VertexDescription.Position,
  181. BlockFields.VertexDescription.Normal,
  182. BlockFields.VertexDescription.Tangent,
  183. },
  184. validPixelBlocks = new BlockFieldDescriptor[]
  185. {
  186. BlockFields.SurfaceDescription.BaseColor,
  187. BlockFields.SurfaceDescription.Alpha,
  188. },
  189. // Fields
  190. structs = new StructCollection
  191. {
  192. { Structs.Attributes },
  193. { Structs.SurfaceDescriptionInputs },
  194. { Structs.VertexDescriptionInputs },
  195. },
  196. requiredFields = new FieldCollection()
  197. {
  198. StructFields.Attributes.color,
  199. StructFields.Attributes.uv0,
  200. StructFields.Varyings.color,
  201. StructFields.Varyings.texCoord0,
  202. },
  203. fieldDependencies = new DependencyCollection()
  204. {
  205. { FieldDependencies.Default },
  206. },
  207. };
  208. }
  209. }