ShaderGraphRequirements.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace UnityEditor.ShaderGraph.Internal
  6. {
  7. [Serializable]
  8. public struct ShaderGraphRequirements
  9. {
  10. [SerializeField] NeededCoordinateSpace m_RequiresNormal;
  11. [SerializeField] NeededCoordinateSpace m_RequiresBitangent;
  12. [SerializeField] NeededCoordinateSpace m_RequiresTangent;
  13. [SerializeField] NeededCoordinateSpace m_RequiresViewDir;
  14. [SerializeField] NeededCoordinateSpace m_RequiresPosition;
  15. [SerializeField] bool m_RequiresScreenPosition;
  16. [SerializeField] bool m_RequiresVertexColor;
  17. [SerializeField] bool m_RequiresFaceSign;
  18. [SerializeField] List<UVChannel> m_RequiresMeshUVs;
  19. [SerializeField] bool m_RequiresDepthTexture;
  20. [SerializeField] bool m_RequiresCameraOpaqueTexture;
  21. [SerializeField] bool m_RequiresTime;
  22. [SerializeField] bool m_RequiresVertexSkinning;
  23. internal static ShaderGraphRequirements none
  24. {
  25. get
  26. {
  27. return new ShaderGraphRequirements
  28. {
  29. m_RequiresMeshUVs = new List<UVChannel>()
  30. };
  31. }
  32. }
  33. public NeededCoordinateSpace requiresNormal
  34. {
  35. get { return m_RequiresNormal; }
  36. internal set { m_RequiresNormal = value; }
  37. }
  38. public NeededCoordinateSpace requiresBitangent
  39. {
  40. get { return m_RequiresBitangent; }
  41. internal set { m_RequiresBitangent = value; }
  42. }
  43. public NeededCoordinateSpace requiresTangent
  44. {
  45. get { return m_RequiresTangent; }
  46. internal set { m_RequiresTangent = value; }
  47. }
  48. public NeededCoordinateSpace requiresViewDir
  49. {
  50. get { return m_RequiresViewDir; }
  51. internal set { m_RequiresViewDir = value; }
  52. }
  53. public NeededCoordinateSpace requiresPosition
  54. {
  55. get { return m_RequiresPosition; }
  56. internal set { m_RequiresPosition = value; }
  57. }
  58. public bool requiresScreenPosition
  59. {
  60. get { return m_RequiresScreenPosition; }
  61. internal set { m_RequiresScreenPosition = value; }
  62. }
  63. public bool requiresVertexColor
  64. {
  65. get { return m_RequiresVertexColor; }
  66. internal set { m_RequiresVertexColor = value; }
  67. }
  68. public bool requiresFaceSign
  69. {
  70. get { return m_RequiresFaceSign; }
  71. internal set { m_RequiresFaceSign = value; }
  72. }
  73. public List<UVChannel> requiresMeshUVs
  74. {
  75. get { return m_RequiresMeshUVs; }
  76. internal set { m_RequiresMeshUVs = value; }
  77. }
  78. public bool requiresDepthTexture
  79. {
  80. get { return m_RequiresDepthTexture; }
  81. internal set { m_RequiresDepthTexture = value; }
  82. }
  83. public bool requiresCameraOpaqueTexture
  84. {
  85. get { return m_RequiresCameraOpaqueTexture; }
  86. internal set { m_RequiresCameraOpaqueTexture = value; }
  87. }
  88. public bool requiresTime
  89. {
  90. get { return m_RequiresTime; }
  91. internal set { m_RequiresTime = value; }
  92. }
  93. public bool requiresVertexSkinning
  94. {
  95. get { return m_RequiresVertexSkinning; }
  96. internal set { m_RequiresVertexSkinning = value; }
  97. }
  98. internal bool NeedsTangentSpace()
  99. {
  100. var compoundSpaces = m_RequiresBitangent | m_RequiresNormal | m_RequiresPosition
  101. | m_RequiresTangent | m_RequiresViewDir | m_RequiresPosition
  102. | m_RequiresNormal;
  103. return (compoundSpaces & NeededCoordinateSpace.Tangent) > 0;
  104. }
  105. internal ShaderGraphRequirements Union(ShaderGraphRequirements other)
  106. {
  107. var newReqs = new ShaderGraphRequirements();
  108. newReqs.m_RequiresNormal = other.m_RequiresNormal | m_RequiresNormal;
  109. newReqs.m_RequiresTangent = other.m_RequiresTangent | m_RequiresTangent;
  110. newReqs.m_RequiresBitangent = other.m_RequiresBitangent | m_RequiresBitangent;
  111. newReqs.m_RequiresViewDir = other.m_RequiresViewDir | m_RequiresViewDir;
  112. newReqs.m_RequiresPosition = other.m_RequiresPosition | m_RequiresPosition;
  113. newReqs.m_RequiresScreenPosition = other.m_RequiresScreenPosition | m_RequiresScreenPosition;
  114. newReqs.m_RequiresVertexColor = other.m_RequiresVertexColor | m_RequiresVertexColor;
  115. newReqs.m_RequiresFaceSign = other.m_RequiresFaceSign | m_RequiresFaceSign;
  116. newReqs.m_RequiresDepthTexture = other.m_RequiresDepthTexture | m_RequiresDepthTexture;
  117. newReqs.m_RequiresCameraOpaqueTexture = other.m_RequiresCameraOpaqueTexture | m_RequiresCameraOpaqueTexture;
  118. newReqs.m_RequiresTime = other.m_RequiresTime | m_RequiresTime;
  119. newReqs.m_RequiresVertexSkinning = other.m_RequiresVertexSkinning | m_RequiresVertexSkinning;
  120. newReqs.m_RequiresMeshUVs = new List<UVChannel>();
  121. if (m_RequiresMeshUVs != null)
  122. newReqs.m_RequiresMeshUVs.AddRange(m_RequiresMeshUVs);
  123. if (other.m_RequiresMeshUVs != null)
  124. newReqs.m_RequiresMeshUVs.AddRange(other.m_RequiresMeshUVs);
  125. return newReqs;
  126. }
  127. internal static ShaderGraphRequirements FromNodes<T>(List<T> nodes, ShaderStageCapability stageCapability = ShaderStageCapability.All, bool includeIntermediateSpaces = true)
  128. where T : AbstractMaterialNode
  129. {
  130. NeededCoordinateSpace requiresNormal = nodes.OfType<IMayRequireNormal>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresNormal(stageCapability));
  131. NeededCoordinateSpace requiresBitangent = nodes.OfType<IMayRequireBitangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresBitangent(stageCapability));
  132. NeededCoordinateSpace requiresTangent = nodes.OfType<IMayRequireTangent>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresTangent(stageCapability));
  133. NeededCoordinateSpace requiresViewDir = nodes.OfType<IMayRequireViewDirection>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresViewDirection(stageCapability));
  134. NeededCoordinateSpace requiresPosition = nodes.OfType<IMayRequirePosition>().Aggregate(NeededCoordinateSpace.None, (mask, node) => mask | node.RequiresPosition(stageCapability));
  135. bool requiresScreenPosition = nodes.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition());
  136. bool requiresVertexColor = nodes.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor());
  137. bool requiresFaceSign = nodes.OfType<IMayRequireFaceSign>().Any(x => x.RequiresFaceSign());
  138. bool requiresDepthTexture = nodes.OfType<IMayRequireDepthTexture>().Any(x => x.RequiresDepthTexture());
  139. bool requiresCameraOpaqueTexture = nodes.OfType<IMayRequireCameraOpaqueTexture>().Any(x => x.RequiresCameraOpaqueTexture());
  140. bool requiresTime = nodes.Any(x => x.RequiresTime());
  141. bool requiresVertexSkinning = nodes.OfType<IMayRequireVertexSkinning>().Any(x => x.RequiresVertexSkinning(stageCapability));
  142. var meshUV = new List<UVChannel>();
  143. for (int uvIndex = 0; uvIndex < ShaderGeneratorNames.UVCount; ++uvIndex)
  144. {
  145. var channel = (UVChannel)uvIndex;
  146. if (nodes.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel)))
  147. meshUV.Add(channel);
  148. }
  149. // if anything needs tangentspace we have make
  150. // sure to have our othonormal basis!
  151. if (includeIntermediateSpaces)
  152. {
  153. var compoundSpaces = requiresBitangent | requiresNormal | requiresPosition
  154. | requiresTangent | requiresViewDir | requiresPosition
  155. | requiresNormal;
  156. var needsTangentSpace = (compoundSpaces & NeededCoordinateSpace.Tangent) > 0;
  157. if (needsTangentSpace)
  158. {
  159. requiresBitangent |= NeededCoordinateSpace.World;
  160. requiresNormal |= NeededCoordinateSpace.World;
  161. requiresTangent |= NeededCoordinateSpace.World;
  162. }
  163. }
  164. var reqs = new ShaderGraphRequirements()
  165. {
  166. m_RequiresNormal = requiresNormal,
  167. m_RequiresBitangent = requiresBitangent,
  168. m_RequiresTangent = requiresTangent,
  169. m_RequiresViewDir = requiresViewDir,
  170. m_RequiresPosition = requiresPosition,
  171. m_RequiresScreenPosition = requiresScreenPosition,
  172. m_RequiresVertexColor = requiresVertexColor,
  173. m_RequiresFaceSign = requiresFaceSign,
  174. m_RequiresMeshUVs = meshUV,
  175. m_RequiresDepthTexture = requiresDepthTexture,
  176. m_RequiresCameraOpaqueTexture = requiresCameraOpaqueTexture,
  177. m_RequiresTime = requiresTime,
  178. m_RequiresVertexSkinning = requiresVertexSkinning
  179. };
  180. return reqs;
  181. }
  182. }
  183. }