설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Texture2DShaderProperty.cs 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Text;
  3. using UnityEditor.Graphing;
  4. using UnityEngine;
  5. namespace UnityEditor.ShaderGraph.Internal
  6. {
  7. [Serializable]
  8. [FormerName("UnityEditor.ShaderGraph.TextureShaderProperty")]
  9. [BlackboardInputInfo(50)]
  10. public class Texture2DShaderProperty : AbstractShaderProperty<SerializableTexture>
  11. {
  12. public enum DefaultType { White, Black, Grey, NormalMap, LinearGrey, Red }
  13. static readonly string[] k_DefaultTypeNames = new string[]
  14. {
  15. "white",
  16. "black",
  17. "grey",
  18. "bump",
  19. "linearGrey",
  20. "red",
  21. };
  22. internal static string ToShaderLabString(DefaultType defaultType)
  23. {
  24. int index = (int)defaultType;
  25. if ((index >= 0) && (index < k_DefaultTypeNames.Length))
  26. return k_DefaultTypeNames[index];
  27. return string.Empty;
  28. }
  29. internal Texture2DShaderProperty()
  30. {
  31. displayName = "Texture2D";
  32. value = new SerializableTexture();
  33. }
  34. public override PropertyType propertyType => PropertyType.Texture2D;
  35. [SerializeField]
  36. internal bool isMainTexture = false;
  37. internal override bool isExposable => true;
  38. internal override bool isRenamable => true;
  39. internal string modifiableTagString => modifiable ? "" : "[NonModifiableTextureData]";
  40. [SerializeField]
  41. internal bool useTilingAndOffset = false;
  42. internal string useSTString => useTilingAndOffset ? "" : "[NoScaleOffset]";
  43. internal string mainTextureString => isMainTexture ? "[MainTexture]" : "";
  44. internal override string GetPropertyBlockString()
  45. {
  46. var normalTagString = (defaultType == DefaultType.NormalMap) ? "[Normal]" : "";
  47. return $"{hideTagString}{modifiableTagString}{normalTagString}{mainTextureString}{useSTString}{referenceName}(\"{displayName}\", 2D) = \"{ToShaderLabString(defaultType)}\" {{}}";
  48. }
  49. // Texture2D properties cannot be set via Hybrid path at the moment; disallow that choice
  50. internal override bool AllowHLSLDeclaration(HLSLDeclaration decl) => (decl != HLSLDeclaration.HybridPerInstance) && (decl != HLSLDeclaration.DoNotDeclare);
  51. internal override void ForeachHLSLProperty(Action<HLSLProperty> action)
  52. {
  53. HLSLDeclaration decl = (generatePropertyBlock ? HLSLDeclaration.UnityPerMaterial : HLSLDeclaration.Global);
  54. action(new HLSLProperty(HLSLType._Texture2D, referenceName, HLSLDeclaration.Global));
  55. action(new HLSLProperty(HLSLType._SamplerState, "sampler" + referenceName, HLSLDeclaration.Global));
  56. action(new HLSLProperty(HLSLType._float4, referenceName + "_TexelSize", decl));
  57. if (useTilingAndOffset)
  58. {
  59. action(new HLSLProperty(HLSLType._float4, referenceName + "_ST", decl));
  60. }
  61. }
  62. internal override string GetPropertyAsArgumentString(string precisionString)
  63. {
  64. return "UnityTexture2D " + referenceName;
  65. }
  66. internal override string GetPropertyAsArgumentStringForVFX(string precisionString)
  67. {
  68. return "TEXTURE2D(" + referenceName + ")";
  69. }
  70. internal override string GetHLSLVariableName(bool isSubgraphProperty, GenerationMode mode)
  71. {
  72. if (isSubgraphProperty)
  73. return referenceName;
  74. else
  75. {
  76. if (useTilingAndOffset)
  77. {
  78. return $"UnityBuildTexture2DStruct({referenceName})";
  79. }
  80. else
  81. {
  82. return $"UnityBuildTexture2DStructNoScale({referenceName})";
  83. }
  84. }
  85. }
  86. [SerializeField]
  87. bool m_Modifiable = true;
  88. internal bool modifiable
  89. {
  90. get => m_Modifiable;
  91. set => m_Modifiable = value;
  92. }
  93. [SerializeField]
  94. DefaultType m_DefaultType = DefaultType.White;
  95. public DefaultType defaultType
  96. {
  97. get { return m_DefaultType; }
  98. set { m_DefaultType = value; }
  99. }
  100. internal override AbstractMaterialNode ToConcreteNode()
  101. {
  102. return new Texture2DAssetNode { texture = value.texture };
  103. }
  104. internal override PreviewProperty GetPreviewMaterialProperty()
  105. {
  106. return new PreviewProperty(propertyType)
  107. {
  108. name = referenceName,
  109. textureValue = value.texture,
  110. texture2DDefaultType = defaultType
  111. };
  112. }
  113. internal override ShaderInput Copy()
  114. {
  115. return new Texture2DShaderProperty()
  116. {
  117. displayName = displayName,
  118. value = value,
  119. defaultType = defaultType,
  120. useTilingAndOffset = useTilingAndOffset,
  121. isMainTexture = isMainTexture
  122. };
  123. }
  124. internal override void OnBeforePasteIntoGraph(GraphData graph)
  125. {
  126. if (isMainTexture)
  127. {
  128. Texture2DShaderProperty existingMain = graph.GetMainTexture();
  129. if (existingMain != null && existingMain != this)
  130. {
  131. isMainTexture = false;
  132. }
  133. }
  134. base.OnBeforePasteIntoGraph(graph);
  135. }
  136. }
  137. }