Geen omschrijving
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.

Texture2DInputMaterialSlot.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.Graphing;
  4. using UnityEditor.ShaderGraph.Drawing.Slots;
  5. using UnityEditor.ShaderGraph.Internal;
  6. using UnityEngine;
  7. using UnityEngine.UIElements;
  8. namespace UnityEditor.ShaderGraph
  9. {
  10. [Serializable]
  11. [HasDependencies(typeof(MinimalTexture2DInputMaterialSlot))]
  12. class Texture2DInputMaterialSlot : Texture2DMaterialSlot
  13. {
  14. [SerializeField]
  15. private SerializableTexture m_Texture = new SerializableTexture();
  16. [SerializeField]
  17. private Texture2DShaderProperty.DefaultType m_DefaultType = Texture2DShaderProperty.DefaultType.White;
  18. public Texture texture
  19. {
  20. get { return m_Texture.texture; }
  21. set { m_Texture.texture = value; }
  22. }
  23. public Texture2DShaderProperty.DefaultType defaultType
  24. {
  25. get { return m_DefaultType; }
  26. set { m_DefaultType = value; }
  27. }
  28. public override bool isDefaultValue => texture == null;
  29. public Texture2DInputMaterialSlot()
  30. { }
  31. public Texture2DInputMaterialSlot(
  32. int slotId,
  33. string displayName,
  34. string shaderOutputName,
  35. ShaderStageCapability stageCapability = ShaderStageCapability.All,
  36. bool hidden = false)
  37. : base(slotId, displayName, shaderOutputName, SlotType.Input, stageCapability, hidden)
  38. { }
  39. public override VisualElement InstantiateControl()
  40. {
  41. return new TextureSlotControlView(this);
  42. }
  43. public override string GetDefaultValue(GenerationMode generationMode)
  44. {
  45. var nodeOwner = owner as AbstractMaterialNode;
  46. if (nodeOwner == null)
  47. throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
  48. return $"UnityBuildTexture2DStructNoScale({nodeOwner.GetVariableNameForSlot(id)})";
  49. }
  50. public override void AddDefaultProperty(PropertyCollector properties, GenerationMode generationMode)
  51. {
  52. var nodeOwner = owner as AbstractMaterialNode;
  53. if (nodeOwner == null)
  54. throw new Exception(string.Format("Slot {0} either has no owner, or the owner is not a {1}", this, typeof(AbstractMaterialNode)));
  55. var prop = new Texture2DShaderProperty();
  56. prop.overrideReferenceName = nodeOwner.GetVariableNameForSlot(id);
  57. prop.modifiable = false;
  58. prop.generatePropertyBlock = true;
  59. prop.value.texture = texture;
  60. prop.defaultType = defaultType;
  61. properties.AddShaderProperty(prop);
  62. }
  63. public override void GetPreviewProperties(List<PreviewProperty> properties, string name)
  64. {
  65. var pp = new PreviewProperty(PropertyType.Texture2D)
  66. {
  67. name = name,
  68. textureValue = texture,
  69. texture2DDefaultType = defaultType
  70. };
  71. properties.Add(pp);
  72. }
  73. public override void CopyValuesFrom(MaterialSlot foundSlot)
  74. {
  75. var slot = foundSlot as Texture2DInputMaterialSlot;
  76. if (slot != null)
  77. {
  78. m_Texture = slot.m_Texture;
  79. bareResource = slot.bareResource;
  80. }
  81. }
  82. }
  83. class MinimalTexture2DInputMaterialSlot : IHasDependencies
  84. {
  85. [SerializeField]
  86. private SerializableTexture m_Texture = null;
  87. public void GetSourceAssetDependencies(AssetCollection assetCollection)
  88. {
  89. var guidString = m_Texture.guid;
  90. if (!string.IsNullOrEmpty(guidString) && GUID.TryParse(guidString, out var guid))
  91. {
  92. assetCollection.AddAssetDependency(guid, AssetCollection.Flags.IncludeInExportPackage);
  93. }
  94. }
  95. }
  96. }