VFXDataMesh.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Serialization;
  6. using UnityEngine.VFX;
  7. using System.Text;
  8. namespace UnityEditor.VFX
  9. {
  10. class VFXDataMesh : VFXData
  11. {
  12. [SerializeField, FormerlySerializedAs("shader")]
  13. private Shader m_Shader;
  14. [SerializeField]
  15. private string m_ShaderName;
  16. public Shader shader
  17. {
  18. get
  19. {
  20. //This is needed for standard shaders ( for instance Unlit/Color ) that are not deserialized correctly during first import.
  21. if (m_Shader == null && !object.ReferenceEquals(m_Shader, null) && !string.IsNullOrEmpty(m_ShaderName))
  22. {
  23. Shader newShader = Shader.Find(m_ShaderName);
  24. if (newShader != null)
  25. m_Shader = newShader;
  26. }
  27. return m_Shader;
  28. }
  29. set
  30. {
  31. m_Shader = value;
  32. DestroyCachedMaterial();
  33. if (m_Shader != null)
  34. m_ShaderName = m_Shader.name;
  35. }
  36. }
  37. private Material m_CachedMaterial = null; // Transient material used to retrieve key words and properties
  38. public override VFXDataType type { get { return VFXDataType.Mesh; } }
  39. public override void OnEnable()
  40. {
  41. base.OnEnable();
  42. VFXLibrary.OnSRPChanged += OnSRPChanged;
  43. if (object.ReferenceEquals(shader, null)) shader = VFXResources.defaultResources.shader;
  44. if (m_Shader != null)
  45. {
  46. if (m_ShaderName != m_Shader.name)
  47. {
  48. m_ShaderName = m_Shader.name;
  49. EditorUtility.SetDirty(this);
  50. }
  51. }
  52. }
  53. public virtual void OnDisable()
  54. {
  55. VFXLibrary.OnSRPChanged -= OnSRPChanged;
  56. DestroyCachedMaterial();
  57. }
  58. private void OnSRPChanged()
  59. {
  60. DestroyCachedMaterial();
  61. }
  62. public void RefreshShader()
  63. {
  64. DestroyCachedMaterial();
  65. Invalidate(InvalidationCause.kSettingChanged);
  66. }
  67. private void DestroyCachedMaterial()
  68. {
  69. Material.DestroyImmediate(m_CachedMaterial);
  70. m_CachedMaterial = null;
  71. }
  72. public override void CopySettings<T>(T dst)
  73. {
  74. VFXDataMesh other = dst as VFXDataMesh;
  75. if (other != null)
  76. other.shader = shader;
  77. }
  78. public override VFXDeviceTarget GetCompilationTarget(VFXContext context)
  79. {
  80. return VFXDeviceTarget.GPU;
  81. }
  82. public override bool CanBeCompiled()
  83. {
  84. return shader != null && m_Owners.Count == 1;
  85. }
  86. public Material GetOrCreateMaterial()
  87. {
  88. if (m_CachedMaterial == null && shader != null)
  89. {
  90. m_CachedMaterial = new Material(shader);
  91. m_CachedMaterial.hideFlags = HideFlags.HideAndDontSave;
  92. VFXLibrary.currentSRPBinder?.SetupMaterial(m_CachedMaterial);
  93. }
  94. return m_CachedMaterial;
  95. }
  96. public override void FillDescs(
  97. VFXCompileErrorReporter reporter,
  98. List<VFXGPUBufferDesc> outBufferDescs,
  99. List<VFXTemporaryGPUBufferDesc> outTemporaryBufferDescs,
  100. List<VFXEditorSystemDesc> outSystemDescs,
  101. VFXExpressionGraph expressionGraph,
  102. Dictionary<VFXContext, VFXContextCompiledData> contextToCompiledData,
  103. Dictionary<VFXContext, int> contextSpawnToBufferIndex,
  104. VFXDependentBuffersData dependentBuffers,
  105. Dictionary<VFXContext, List<VFXContextLink>[]> effectiveFlowInputLinks,
  106. VFXSystemNames systemNames = null)
  107. {
  108. var context = m_Owners[0];
  109. var contextData = contextToCompiledData[context];
  110. var mappings = new List<VFXMapping>();
  111. var uniforms = contextData.uniformMapper.uniforms;
  112. uniforms = uniforms.Concat(contextData.uniformMapper.textures);
  113. uniforms = uniforms.Concat(contextData.uniformMapper.buffers);
  114. foreach (var uniform in uniforms)
  115. {
  116. int exprIndex = expressionGraph.GetFlattenedIndex(uniform);
  117. foreach (var name in contextData.uniformMapper.GetNames(uniform))
  118. mappings.Add(new VFXMapping(name, exprIndex));
  119. }
  120. var task = new VFXEditorTaskDesc()
  121. {
  122. externalProcessor = shader,
  123. values = mappings.ToArray(),
  124. type = (UnityEngine.VFX.VFXTaskType)VFXTaskType.Output,
  125. model = context
  126. };
  127. mappings.Clear();
  128. var mapper = contextData.cpuMapper;
  129. // TODO Factorize that
  130. var meshExp = mapper.FromNameAndId("mesh", -1);
  131. var transformExp = mapper.FromNameAndId("transform", -1);
  132. var subMaskExp = mapper.FromNameAndId("subMeshMask", -1);
  133. int meshIndex = meshExp != null ? expressionGraph.GetFlattenedIndex(meshExp) : -1;
  134. int transformIndex = transformExp != null ? expressionGraph.GetFlattenedIndex(transformExp) : -1;
  135. int subMaskIndex = subMaskExp != null ? expressionGraph.GetFlattenedIndex(subMaskExp) : -1;
  136. if (meshIndex != -1)
  137. mappings.Add(new VFXMapping("mesh", meshIndex));
  138. if (transformIndex != -1)
  139. mappings.Add(new VFXMapping("transform", transformIndex));
  140. if (subMaskIndex != -1)
  141. mappings.Add(new VFXMapping("subMeshMask", subMaskIndex));
  142. outSystemDescs.Add(new VFXEditorSystemDesc()
  143. {
  144. tasks = new VFXEditorTaskDesc[1] { task },
  145. values = mappings.ToArray(),
  146. type = VFXSystemType.Mesh,
  147. layer = uint.MaxValue,
  148. });
  149. }
  150. public override void GenerateAttributeLayout(Dictionary<VFXContext, List<VFXContextLink>[]> effectiveFlowInputLinks)
  151. {
  152. }
  153. public override string GetAttributeDataDeclaration(VFXAttributeMode mode)
  154. {
  155. throw new NotImplementedException();
  156. }
  157. public override string GetLoadAttributeCode(VFXAttribute attrib, VFXAttributeLocation location)
  158. {
  159. throw new NotImplementedException();
  160. }
  161. public override string GetStoreAttributeCode(VFXAttribute attrib, string value)
  162. {
  163. throw new NotImplementedException();
  164. }
  165. }
  166. }