VFXAssetEditor.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using UnityEditorInternal;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using UnityEngine.VFX;
  9. using UnityEditor.Callbacks;
  10. using UnityEditor.VFX;
  11. using UnityEditor.VFX.UI;
  12. using UnityObject = UnityEngine.Object;
  13. class VFXExternalShaderProcessor : AssetPostprocessor
  14. {
  15. public const string k_ShaderDirectory = "Shaders";
  16. public const string k_ShaderExt = ".vfxshader";
  17. public static bool allowExternalization { get { return EditorPrefs.GetBool(VFXViewPreference.allowShaderExternalizationKey, false); } }
  18. void OnPreprocessAsset()
  19. {
  20. if (!allowExternalization)
  21. return;
  22. bool isVFX = assetPath.EndsWith(VisualEffectResource.Extension);
  23. if (isVFX)
  24. {
  25. string vfxName = Path.GetFileNameWithoutExtension(assetPath);
  26. string vfxDirectory = Path.GetDirectoryName(assetPath);
  27. string shaderDirectory = vfxDirectory + "/" + k_ShaderDirectory + "/" + vfxName;
  28. if (!Directory.Exists(shaderDirectory))
  29. {
  30. return;
  31. }
  32. VisualEffectAsset asset = AssetDatabase.LoadAssetAtPath<VisualEffectAsset>(assetPath);
  33. if (asset == null)
  34. return;
  35. bool oneFound = false;
  36. VisualEffectResource resource = asset.GetResource();
  37. if (resource == null)
  38. return;
  39. VFXShaderSourceDesc[] descs = resource.shaderSources;
  40. foreach (var shaderPath in Directory.GetFiles(shaderDirectory))
  41. {
  42. if (shaderPath.EndsWith(k_ShaderExt))
  43. {
  44. System.IO.StreamReader file = new System.IO.StreamReader(shaderPath);
  45. string shaderLine = file.ReadLine();
  46. file.Close();
  47. if (shaderLine == null || !shaderLine.StartsWith("//"))
  48. continue;
  49. string[] shaderParams = shaderLine.Split(',');
  50. string shaderName = shaderParams[0].Substring(2);
  51. int index;
  52. if (!int.TryParse(shaderParams[1], out index))
  53. continue;
  54. if (index < 0 || index >= descs.Length)
  55. continue;
  56. if (descs[index].name != shaderName)
  57. continue;
  58. string shaderSource = File.ReadAllText(shaderPath);
  59. //remove the first two lines that where added when externalized
  60. shaderSource = shaderSource.Substring(shaderSource.IndexOf("\n", shaderSource.IndexOf("\n") + 1) + 1);
  61. descs[index].source = shaderSource;
  62. oneFound = true;
  63. }
  64. }
  65. if (oneFound)
  66. {
  67. resource.shaderSources = descs;
  68. }
  69. }
  70. }
  71. }
  72. [CustomEditor(typeof(VisualEffectAsset))]
  73. [CanEditMultipleObjects]
  74. class VisualEffectAssetEditor : Editor
  75. {
  76. #if UNITY_2021_1_OR_NEWER
  77. [OnOpenAsset(OnOpenAssetAttributeMode.Validate)]
  78. public static bool WillOpenInUnity(int instanceID)
  79. {
  80. var obj = EditorUtility.InstanceIDToObject(instanceID);
  81. if (obj is VFXGraph || obj is VFXModel || obj is VFXUI)
  82. return true;
  83. else if (obj is VisualEffectAsset)
  84. return true;
  85. else if (obj is VisualEffectSubgraph)
  86. return true;
  87. return false;
  88. }
  89. #endif
  90. [OnOpenAsset(1)]
  91. public static bool OnOpenVFX(int instanceID, int line)
  92. {
  93. var obj = EditorUtility.InstanceIDToObject(instanceID);
  94. if (obj is VFXGraph || obj is VFXModel || obj is VFXUI)
  95. {
  96. // for visual effect graph editor ScriptableObject select them when double clicking on them.
  97. //Since .vfx importer is a copyasset, the default is to open it with an external editor.
  98. Selection.activeInstanceID = instanceID;
  99. return true;
  100. }
  101. else if (obj is VisualEffectAsset)
  102. {
  103. VFXViewWindow.GetWindow<VFXViewWindow>().LoadAsset(obj as VisualEffectAsset, null);
  104. return true;
  105. }
  106. else if (obj is VisualEffectSubgraph)
  107. {
  108. VisualEffectResource resource = VisualEffectResource.GetResourceAtPath(AssetDatabase.GetAssetPath(obj));
  109. VFXViewWindow.GetWindow<VFXViewWindow>().LoadResource(resource, null);
  110. return true;
  111. }
  112. else if (obj is Material || obj is ComputeShader)
  113. {
  114. string path = AssetDatabase.GetAssetPath(instanceID);
  115. if (path.EndsWith(VisualEffectResource.Extension))
  116. {
  117. var resource = VisualEffectResource.GetResourceAtPath(path);
  118. if (resource != null)
  119. {
  120. int index = resource.GetShaderIndex(obj);
  121. resource.ShowGeneratedShaderFile(index, line);
  122. return true;
  123. }
  124. }
  125. }
  126. return false;
  127. }
  128. ReorderableList m_ReorderableList;
  129. List<IVFXSubRenderer> m_OutputContexts = new List<IVFXSubRenderer>();
  130. VFXGraph m_CurrentGraph;
  131. void OnReorder(ReorderableList list)
  132. {
  133. for (int i = 0; i < m_OutputContexts.Count(); ++i)
  134. {
  135. m_OutputContexts[i].vfxSystemSortPriority = i;
  136. }
  137. }
  138. private void DrawOutputContextItem(Rect rect, int index, bool isActive, bool isFocused)
  139. {
  140. var context = m_OutputContexts[index] as VFXContext;
  141. var systemName = context.GetGraph().systemNames.GetUniqueSystemName(context.GetData());
  142. var contextLetter = context.letter;
  143. var contextName = string.IsNullOrEmpty(context.label) ? context.libraryName : context.label;
  144. var fullName = string.Format("{0}{1}/{2}", systemName, contextLetter != '\0' ? "/" + contextLetter : string.Empty, contextName);
  145. EditorGUI.LabelField(rect, EditorGUIUtility.TempContent(fullName));
  146. }
  147. private void DrawHeader(Rect rect)
  148. {
  149. EditorGUI.LabelField(rect, EditorGUIUtility.TrTextContent("Output Render Order"));
  150. }
  151. static Mesh s_CubeWireFrame;
  152. void OnEnable()
  153. {
  154. m_OutputContexts.Clear();
  155. VisualEffectAsset target = this.target as VisualEffectAsset;
  156. var resource = target.GetResource();
  157. if (resource != null) //Can be null if VisualEffectAsset is in Asset Bundle
  158. {
  159. m_CurrentGraph = resource.GetOrCreateGraph();
  160. m_CurrentGraph.systemNames.Sync(m_CurrentGraph);
  161. m_OutputContexts.AddRange(m_CurrentGraph.children.OfType<IVFXSubRenderer>().OrderBy(t => t.vfxSystemSortPriority));
  162. }
  163. m_ReorderableList = new ReorderableList(m_OutputContexts, typeof(IVFXSubRenderer));
  164. m_ReorderableList.displayRemove = false;
  165. m_ReorderableList.displayAdd = false;
  166. m_ReorderableList.onReorderCallback = OnReorder;
  167. m_ReorderableList.drawHeaderCallback = DrawHeader;
  168. m_ReorderableList.drawElementCallback = DrawOutputContextItem;
  169. if (m_VisualEffectGO == null)
  170. {
  171. m_PreviewUtility = new PreviewRenderUtility();
  172. m_PreviewUtility.camera.fieldOfView = 60.0f;
  173. m_PreviewUtility.camera.allowHDR = true;
  174. m_PreviewUtility.camera.allowMSAA = false;
  175. m_PreviewUtility.camera.farClipPlane = 10000.0f;
  176. m_PreviewUtility.camera.clearFlags = CameraClearFlags.SolidColor;
  177. m_PreviewUtility.ambientColor = new Color(.1f, .1f, .1f, 1.0f);
  178. m_PreviewUtility.lights[0].intensity = 1.4f;
  179. m_PreviewUtility.lights[0].transform.rotation = Quaternion.Euler(40f, 40f, 0);
  180. m_PreviewUtility.lights[1].intensity = 1.4f;
  181. m_VisualEffectGO = new GameObject("VisualEffect (Preview)");
  182. m_VisualEffectGO.hideFlags = HideFlags.DontSave;
  183. m_VisualEffect = m_VisualEffectGO.AddComponent<VisualEffect>();
  184. m_PreviewUtility.AddManagedGO(m_VisualEffectGO);
  185. m_VisualEffectGO.transform.localPosition = Vector3.zero;
  186. m_VisualEffectGO.transform.localRotation = Quaternion.identity;
  187. m_VisualEffectGO.transform.localScale = Vector3.one;
  188. m_VisualEffect.visualEffectAsset = target;
  189. m_CurrentBounds = new Bounds(Vector3.zero, Vector3.one);
  190. m_FrameCount = 0;
  191. m_Distance = 10;
  192. m_Angles = Vector3.forward;
  193. if (s_CubeWireFrame == null)
  194. {
  195. s_CubeWireFrame = new Mesh();
  196. var vertices = new Vector3[]
  197. {
  198. new Vector3(-0.5f, -0.5f, -0.5f),
  199. new Vector3(-0.5f, -0.5f, 0.5f),
  200. new Vector3(-0.5f, 0.5f, 0.5f),
  201. new Vector3(-0.5f, 0.5f, -0.5f),
  202. new Vector3(0.5f, -0.5f, -0.5f),
  203. new Vector3(0.5f, -0.5f, 0.5f),
  204. new Vector3(0.5f, 0.5f, 0.5f),
  205. new Vector3(0.5f, 0.5f, -0.5f)
  206. };
  207. var indices = new int[]
  208. {
  209. 0, 1,
  210. 0, 3,
  211. 0, 4,
  212. 6, 2,
  213. 6, 5,
  214. 6, 7,
  215. 1, 2,
  216. 1, 5,
  217. 3, 7,
  218. 3, 2,
  219. 4, 5,
  220. 4, 7
  221. };
  222. s_CubeWireFrame.vertices = vertices;
  223. s_CubeWireFrame.SetIndices(indices, MeshTopology.Lines, 0);
  224. }
  225. }
  226. var targetResources = targets.Cast<VisualEffectAsset>().Select(t => t.GetResource()).Where(t => t != null).ToArray();
  227. if (targetResources.Any())
  228. {
  229. resourceObject = new SerializedObject(targetResources);
  230. resourceUpdateModeProperty = resourceObject.FindProperty("m_Infos.m_UpdateMode");
  231. cullingFlagsProperty = resourceObject.FindProperty("m_Infos.m_CullingFlags");
  232. motionVectorRenderModeProperty = resourceObject.FindProperty("m_Infos.m_RendererSettings.motionVectorGenerationMode");
  233. prewarmDeltaTime = resourceObject.FindProperty("m_Infos.m_PreWarmDeltaTime");
  234. prewarmStepCount = resourceObject.FindProperty("m_Infos.m_PreWarmStepCount");
  235. initialEventName = resourceObject.FindProperty("m_Infos.m_InitialEventName");
  236. }
  237. }
  238. PreviewRenderUtility m_PreviewUtility;
  239. GameObject m_VisualEffectGO;
  240. VisualEffect m_VisualEffect;
  241. Vector3 m_Angles;
  242. float m_Distance;
  243. Bounds m_CurrentBounds;
  244. int m_FrameCount = 0;
  245. const int kSafeFrame = 2;
  246. public override bool HasPreviewGUI()
  247. {
  248. return !serializedObject.isEditingMultipleObjects;
  249. }
  250. void ComputeFarNear()
  251. {
  252. if (m_CurrentBounds.size != Vector3.zero)
  253. {
  254. float maxBounds = Mathf.Sqrt(m_CurrentBounds.size.x * m_CurrentBounds.size.x + m_CurrentBounds.size.y * m_CurrentBounds.size.y + m_CurrentBounds.size.z * m_CurrentBounds.size.z);
  255. m_PreviewUtility.camera.farClipPlane = m_Distance + maxBounds * 1.1f;
  256. m_PreviewUtility.camera.nearClipPlane = Mathf.Max(0.0001f, (m_Distance - maxBounds));
  257. m_PreviewUtility.camera.nearClipPlane = Mathf.Max(0.0001f, (m_Distance - maxBounds));
  258. }
  259. }
  260. public override void OnInteractivePreviewGUI(Rect r, GUIStyle background)
  261. {
  262. if (m_VisualEffectGO == null)
  263. OnEnable();
  264. bool isRepaint = (Event.current.type == EventType.Repaint);
  265. m_Angles = VFXPreviewGUI.Drag2D(m_Angles, r);
  266. Renderer renderer = m_VisualEffectGO.GetComponent<Renderer>();
  267. if (renderer == null)
  268. return;
  269. if (renderer.bounds.size != Vector3.zero)
  270. {
  271. m_CurrentBounds = renderer.bounds;
  272. //make sure that none of the bounds values are 0
  273. if (m_CurrentBounds.size.x == 0)
  274. {
  275. Vector3 size = m_CurrentBounds.size;
  276. size.x = (m_CurrentBounds.size.y + m_CurrentBounds.size.z) * 0.1f;
  277. m_CurrentBounds.size = size;
  278. }
  279. if (m_CurrentBounds.size.y == 0)
  280. {
  281. Vector3 size = m_CurrentBounds.size;
  282. size.y = (m_CurrentBounds.size.x + m_CurrentBounds.size.z) * 0.1f;
  283. m_CurrentBounds.size = size;
  284. }
  285. if (m_CurrentBounds.size.z == 0)
  286. {
  287. Vector3 size = m_CurrentBounds.size;
  288. size.z = (m_CurrentBounds.size.x + m_CurrentBounds.size.y) * 0.1f;
  289. m_CurrentBounds.size = size;
  290. }
  291. }
  292. if (m_FrameCount == kSafeFrame) // wait to frame before asking the renderer bounds as it is a computed value.
  293. {
  294. float maxBounds = Mathf.Sqrt(m_CurrentBounds.size.x * m_CurrentBounds.size.x + m_CurrentBounds.size.y * m_CurrentBounds.size.y + m_CurrentBounds.size.z * m_CurrentBounds.size.z);
  295. m_Distance = Mathf.Max(0.01f, maxBounds * 1.25f);
  296. ComputeFarNear();
  297. }
  298. else
  299. {
  300. ComputeFarNear();
  301. }
  302. m_FrameCount++;
  303. if (Event.current.isScrollWheel)
  304. {
  305. m_Distance *= 1 + (Event.current.delta.y * .015f);
  306. }
  307. if (m_Mat == null)
  308. m_Mat = (Material)EditorGUIUtility.LoadRequired("SceneView/HandleLines.mat");
  309. if (isRepaint)
  310. {
  311. m_PreviewUtility.BeginPreview(r, background);
  312. Quaternion rot = Quaternion.Euler(0, m_Angles.x, 0) * Quaternion.Euler(m_Angles.y, 0, 0);
  313. m_PreviewUtility.camera.transform.position = m_CurrentBounds.center + rot * new Vector3(0, 0, -m_Distance);
  314. m_PreviewUtility.camera.transform.localRotation = rot;
  315. m_PreviewUtility.DrawMesh(s_CubeWireFrame, Matrix4x4.TRS(m_CurrentBounds.center, Quaternion.identity, m_CurrentBounds.size), m_Mat, 0);
  316. m_PreviewUtility.Render(true);
  317. m_PreviewUtility.EndAndDrawPreview(r);
  318. // Ask for repaint so the effect is animated.
  319. Repaint();
  320. }
  321. }
  322. Material m_Mat;
  323. void OnDisable()
  324. {
  325. if (!UnityObject.ReferenceEquals(m_VisualEffectGO, null))
  326. {
  327. UnityObject.DestroyImmediate(m_VisualEffectGO);
  328. }
  329. if (m_PreviewUtility != null)
  330. {
  331. m_PreviewUtility.Cleanup();
  332. }
  333. }
  334. private static readonly GUIContent[] k_CullingOptionsContents = new GUIContent[]
  335. {
  336. EditorGUIUtility.TrTextContent("Recompute bounds and simulate when visible"),
  337. EditorGUIUtility.TrTextContent("Always recompute bounds, simulate only when visible"),
  338. EditorGUIUtility.TrTextContent("Always recompute bounds and simulate")
  339. };
  340. static readonly VFXCullingFlags[] k_CullingOptionsValue = new VFXCullingFlags[]
  341. {
  342. VFXCullingFlags.CullSimulation | VFXCullingFlags.CullBoundsUpdate,
  343. VFXCullingFlags.CullSimulation,
  344. VFXCullingFlags.CullNone,
  345. };
  346. private string UpdateModeToString(VFXUpdateMode mode)
  347. {
  348. return ObjectNames.NicifyVariableName(mode.ToString());
  349. }
  350. SerializedObject resourceObject;
  351. SerializedProperty resourceUpdateModeProperty;
  352. SerializedProperty cullingFlagsProperty;
  353. SerializedProperty motionVectorRenderModeProperty;
  354. SerializedProperty prewarmDeltaTime;
  355. SerializedProperty prewarmStepCount;
  356. SerializedProperty initialEventName;
  357. private static readonly float k_MinimalCommonDeltaTime = 1.0f / 800.0f;
  358. public override void OnInspectorGUI()
  359. {
  360. resourceObject.Update();
  361. GUI.enabled = AssetDatabase.IsOpenForEdit(this.target, StatusQueryOptions.UseCachedIfPossible);
  362. VFXUpdateMode initialUpdateMode = (VFXUpdateMode)0;
  363. bool? initialFixedDeltaTime = null;
  364. bool? initialProcessEveryFrame = null;
  365. bool? initialIgnoreGameTimeScale = null;
  366. if (resourceUpdateModeProperty.hasMultipleDifferentValues)
  367. {
  368. var resourceUpdateModeProperties = resourceUpdateModeProperty.serializedObject.targetObjects
  369. .Select(o => new SerializedObject(o)
  370. .FindProperty(resourceUpdateModeProperty.propertyPath))
  371. .ToArray(); //N.B.: This will create garbage
  372. var allDeltaTime = resourceUpdateModeProperties.Select(o => ((VFXUpdateMode)o.intValue & VFXUpdateMode.DeltaTime) == VFXUpdateMode.DeltaTime)
  373. .Distinct();
  374. var allProcessEveryFrame = resourceUpdateModeProperties.Select(o => ((VFXUpdateMode)o.intValue & VFXUpdateMode.ExactFixedTimeStep) == VFXUpdateMode.ExactFixedTimeStep)
  375. .Distinct();
  376. var allIgnoreScale = resourceUpdateModeProperties.Select(o => ((VFXUpdateMode)o.intValue & VFXUpdateMode.IgnoreTimeScale) == VFXUpdateMode.IgnoreTimeScale)
  377. .Distinct();
  378. if (allDeltaTime.Count() == 1)
  379. initialFixedDeltaTime = !allDeltaTime.First();
  380. if (allProcessEveryFrame.Count() == 1)
  381. initialProcessEveryFrame = allProcessEveryFrame.First();
  382. if (allIgnoreScale.Count() == 1)
  383. initialIgnoreGameTimeScale = allIgnoreScale.First();
  384. }
  385. else
  386. {
  387. initialUpdateMode = (VFXUpdateMode)resourceUpdateModeProperty.intValue;
  388. initialFixedDeltaTime = !((initialUpdateMode & VFXUpdateMode.DeltaTime) == VFXUpdateMode.DeltaTime);
  389. initialProcessEveryFrame = (initialUpdateMode & VFXUpdateMode.ExactFixedTimeStep) == VFXUpdateMode.ExactFixedTimeStep;
  390. initialIgnoreGameTimeScale = (initialUpdateMode & VFXUpdateMode.IgnoreTimeScale) == VFXUpdateMode.IgnoreTimeScale;
  391. }
  392. EditorGUI.showMixedValue = !initialFixedDeltaTime.HasValue;
  393. var deltaTimeContent = EditorGUIUtility.TrTextContent("Fixed Delta Time", "If enabled, use visual effect manager fixed delta time mode, otherwise, use the default Time.deltaTime.");
  394. var processEveryFrameContent = EditorGUIUtility.TrTextContent("Exact Fixed Time", "Only relevant when using Fixed Delta Time. When enabled, several updates can be processed per frame (e.g.: if a frame is 10ms and the fixed frame rate is set to 5 ms, the effect will update twice with a 5ms deltaTime instead of once with a 10ms deltaTime). This method is expensive and should only be used for high-end scenarios.");
  395. var ignoreTimeScaleContent = EditorGUIUtility.TrTextContent("Ignore Time Scale", "When enabled, the computed visual effect delta time ignores the game Time Scale value (Play Rate is still applied).");
  396. EditorGUI.BeginChangeCheck();
  397. VisualEffectEditor.ShowHeader(EditorGUIUtility.TrTextContent("Update mode"), false, false);
  398. bool newFixedDeltaTime = EditorGUILayout.Toggle(deltaTimeContent, initialFixedDeltaTime ?? false);
  399. bool newExactFixedTimeStep = false;
  400. EditorGUI.showMixedValue = !initialProcessEveryFrame.HasValue;
  401. EditorGUI.BeginDisabledGroup((!initialFixedDeltaTime.HasValue || !initialFixedDeltaTime.Value) && !resourceUpdateModeProperty.hasMultipleDifferentValues);
  402. newExactFixedTimeStep = EditorGUILayout.Toggle(processEveryFrameContent, initialProcessEveryFrame ?? false);
  403. EditorGUI.EndDisabledGroup();
  404. EditorGUI.showMixedValue = !initialIgnoreGameTimeScale.HasValue;
  405. bool newIgnoreTimeScale = EditorGUILayout.Toggle(ignoreTimeScaleContent, initialIgnoreGameTimeScale ?? false);
  406. if (EditorGUI.EndChangeCheck())
  407. {
  408. if (!resourceUpdateModeProperty.hasMultipleDifferentValues)
  409. {
  410. var newUpdateMode = (VFXUpdateMode)0;
  411. if (!newFixedDeltaTime)
  412. newUpdateMode = newUpdateMode | VFXUpdateMode.DeltaTime;
  413. if (newExactFixedTimeStep)
  414. newUpdateMode = newUpdateMode | VFXUpdateMode.ExactFixedTimeStep;
  415. if (newIgnoreTimeScale)
  416. newUpdateMode = newUpdateMode | VFXUpdateMode.IgnoreTimeScale;
  417. resourceUpdateModeProperty.intValue = (int)newUpdateMode;
  418. resourceObject.ApplyModifiedProperties();
  419. }
  420. else
  421. {
  422. var resourceUpdateModeProperties = resourceUpdateModeProperty.serializedObject.targetObjects.Select(o => new SerializedObject(o).FindProperty(resourceUpdateModeProperty.propertyPath));
  423. foreach (var property in resourceUpdateModeProperties)
  424. {
  425. var updateMode = (VFXUpdateMode)property.intValue;
  426. if (initialFixedDeltaTime.HasValue)
  427. {
  428. if (!newFixedDeltaTime)
  429. updateMode = updateMode | VFXUpdateMode.DeltaTime;
  430. else
  431. updateMode = updateMode & ~VFXUpdateMode.DeltaTime;
  432. }
  433. else
  434. {
  435. if (newFixedDeltaTime)
  436. updateMode = updateMode & ~VFXUpdateMode.DeltaTime;
  437. }
  438. if (newExactFixedTimeStep)
  439. updateMode = updateMode | VFXUpdateMode.ExactFixedTimeStep;
  440. else if (initialProcessEveryFrame.HasValue)
  441. updateMode = updateMode & ~VFXUpdateMode.ExactFixedTimeStep;
  442. if (newIgnoreTimeScale)
  443. updateMode = updateMode | VFXUpdateMode.IgnoreTimeScale;
  444. else if (initialIgnoreGameTimeScale.HasValue)
  445. updateMode = updateMode & ~VFXUpdateMode.IgnoreTimeScale;
  446. property.intValue = (int)updateMode;
  447. property.serializedObject.ApplyModifiedProperties();
  448. }
  449. }
  450. }
  451. VisualEffectAsset asset = (VisualEffectAsset)target;
  452. VisualEffectResource resource = asset.GetResource();
  453. //The following should be working, and works for newly created systems, but fails for old systems,
  454. //due probably to incorrectly pasting the VFXData when creating them.
  455. // bool hasAutomaticBoundsSystems = resource.GetOrCreateGraph().children
  456. // .OfType<VFXDataParticle>().Any(d => d.boundsMode == BoundsSettingMode.Automatic);
  457. bool hasAutomaticBoundsSystems = resource.GetOrCreateGraph().children
  458. .OfType<VFXBasicInitialize>().Any(d => (d.GetData() as VFXDataParticle).boundsMode == BoundsSettingMode.Automatic);
  459. using (new EditorGUI.DisabledScope(hasAutomaticBoundsSystems))
  460. {
  461. EditorGUILayout.BeginHorizontal();
  462. EditorGUI.showMixedValue = cullingFlagsProperty.hasMultipleDifferentValues;
  463. string forceSimulateTooltip = hasAutomaticBoundsSystems
  464. ? " When using systems with Bounds Mode set to Automatic, this has to be set to Always recompute bounds and simulate."
  465. : "";
  466. EditorGUILayout.PrefixLabel(EditorGUIUtility.TrTextContent("Culling Flags", "Specifies how the system recomputes its bounds and simulates when off-screen." + forceSimulateTooltip));
  467. EditorGUI.BeginChangeCheck();
  468. int newOption =
  469. EditorGUILayout.Popup(
  470. Array.IndexOf(k_CullingOptionsValue, (VFXCullingFlags)cullingFlagsProperty.intValue),
  471. k_CullingOptionsContents);
  472. if (EditorGUI.EndChangeCheck())
  473. {
  474. cullingFlagsProperty.intValue = (int)k_CullingOptionsValue[newOption];
  475. resourceObject.ApplyModifiedProperties();
  476. }
  477. }
  478. EditorGUILayout.EndHorizontal();
  479. VisualEffectEditor.ShowHeader(EditorGUIUtility.TrTextContent("Initial state"), false, false);
  480. if (prewarmDeltaTime != null && prewarmStepCount != null)
  481. {
  482. if (!prewarmDeltaTime.hasMultipleDifferentValues && !prewarmStepCount.hasMultipleDifferentValues)
  483. {
  484. var currentDeltaTime = prewarmDeltaTime.floatValue;
  485. var currentStepCount = prewarmStepCount.intValue;
  486. var currentTotalTime = currentDeltaTime * currentStepCount;
  487. EditorGUI.BeginChangeCheck();
  488. currentTotalTime = EditorGUILayout.FloatField(EditorGUIUtility.TrTextContent("PreWarm Total Time", "Sets the time in seconds to advance the current effect to when it is initially played. "), currentTotalTime);
  489. if (EditorGUI.EndChangeCheck())
  490. {
  491. if (currentStepCount <= 0)
  492. {
  493. prewarmStepCount.intValue = currentStepCount = 1;
  494. }
  495. currentDeltaTime = currentTotalTime / currentStepCount;
  496. prewarmDeltaTime.floatValue = currentDeltaTime;
  497. resourceObject.ApplyModifiedProperties();
  498. }
  499. EditorGUI.BeginChangeCheck();
  500. currentStepCount = EditorGUILayout.IntField(EditorGUIUtility.TrTextContent("PreWarm Step Count", "Sets the number of simulation steps the prewarm should be broken down to. "), currentStepCount);
  501. if (EditorGUI.EndChangeCheck())
  502. {
  503. if (currentStepCount <= 0 && currentTotalTime != 0.0f)
  504. {
  505. prewarmStepCount.intValue = currentStepCount = 1;
  506. }
  507. currentDeltaTime = currentTotalTime == 0.0f ? 0.0f : currentTotalTime / currentStepCount;
  508. prewarmDeltaTime.floatValue = currentDeltaTime;
  509. prewarmStepCount.intValue = currentStepCount;
  510. resourceObject.ApplyModifiedProperties();
  511. }
  512. EditorGUI.BeginChangeCheck();
  513. currentDeltaTime = EditorGUILayout.FloatField(EditorGUIUtility.TrTextContent("PreWarm Delta Time", "Sets the time in seconds for each step to achieve the desired total prewarm time."), currentDeltaTime);
  514. if (EditorGUI.EndChangeCheck())
  515. {
  516. if (currentDeltaTime < k_MinimalCommonDeltaTime)
  517. {
  518. prewarmDeltaTime.floatValue = currentDeltaTime = k_MinimalCommonDeltaTime;
  519. }
  520. if (currentDeltaTime > currentTotalTime)
  521. {
  522. currentTotalTime = currentDeltaTime;
  523. }
  524. if (currentTotalTime != 0.0f)
  525. {
  526. var candidateStepCount_A = Mathf.FloorToInt(currentTotalTime / currentDeltaTime);
  527. var candidateStepCount_B = Mathf.RoundToInt(currentTotalTime / currentDeltaTime);
  528. var totalTime_A = currentDeltaTime * candidateStepCount_A;
  529. var totalTime_B = currentDeltaTime * candidateStepCount_B;
  530. if (Mathf.Abs(totalTime_A - currentTotalTime) < Mathf.Abs(totalTime_B - currentTotalTime))
  531. {
  532. currentStepCount = candidateStepCount_A;
  533. }
  534. else
  535. {
  536. currentStepCount = candidateStepCount_B;
  537. }
  538. prewarmStepCount.intValue = currentStepCount;
  539. }
  540. prewarmDeltaTime.floatValue = currentDeltaTime;
  541. resourceObject.ApplyModifiedProperties();
  542. }
  543. }
  544. else
  545. {
  546. //Multi selection case, can't resolve total time easily
  547. EditorGUI.BeginChangeCheck();
  548. EditorGUI.showMixedValue = prewarmStepCount.hasMultipleDifferentValues;
  549. EditorGUILayout.PropertyField(prewarmStepCount, EditorGUIUtility.TrTextContent("PreWarm Step Count", "Sets the number of simulation steps the prewarm should be broken down to."));
  550. EditorGUI.showMixedValue = prewarmDeltaTime.hasMultipleDifferentValues;
  551. EditorGUILayout.PropertyField(prewarmDeltaTime, EditorGUIUtility.TrTextContent("PreWarm Delta Time", "Sets the time in seconds for each step to achieve the desired total prewarm time."));
  552. if (EditorGUI.EndChangeCheck())
  553. {
  554. if (prewarmDeltaTime.floatValue < k_MinimalCommonDeltaTime)
  555. prewarmDeltaTime.floatValue = k_MinimalCommonDeltaTime;
  556. resourceObject.ApplyModifiedProperties();
  557. }
  558. }
  559. }
  560. if (initialEventName != null)
  561. {
  562. EditorGUI.BeginChangeCheck();
  563. EditorGUI.showMixedValue = initialEventName.hasMultipleDifferentValues;
  564. EditorGUILayout.PropertyField(initialEventName, new GUIContent("Initial Event Name", "Sets the name of the event which triggers once the system is activated. Default: ‘OnPlay’."));
  565. if (EditorGUI.EndChangeCheck())
  566. {
  567. resourceObject.ApplyModifiedProperties();
  568. }
  569. }
  570. if (!serializedObject.isEditingMultipleObjects)
  571. {
  572. asset = (VisualEffectAsset)target;
  573. resource = asset.GetResource();
  574. m_OutputContexts.Clear();
  575. m_OutputContexts.AddRange(resource.GetOrCreateGraph().children.OfType<IVFXSubRenderer>().OrderBy(t => t.vfxSystemSortPriority));
  576. m_ReorderableList.DoLayoutList();
  577. VisualEffectEditor.ShowHeader(EditorGUIUtility.TrTextContent("Shaders"), false, false);
  578. string assetPath = AssetDatabase.GetAssetPath(asset);
  579. UnityObject[] objects = AssetDatabase.LoadAllAssetsAtPath(assetPath);
  580. string directory = Path.GetDirectoryName(assetPath) + "/" + VFXExternalShaderProcessor.k_ShaderDirectory + "/" + asset.name + "/";
  581. foreach (var obj in objects)
  582. {
  583. if (obj is Material || obj is ComputeShader)
  584. {
  585. GUILayout.BeginHorizontal();
  586. Rect r = GUILayoutUtility.GetRect(0, 18, GUILayout.ExpandWidth(true));
  587. int buttonsWidth = VFXExternalShaderProcessor.allowExternalization ? 240 : 160;
  588. int index = resource.GetShaderIndex(obj);
  589. var shader = obj;
  590. if (obj is Material) // Retrieve the shader from the material
  591. shader = ((Material)(obj)).shader;
  592. if (shader == null)
  593. continue;
  594. Rect labelR = r;
  595. labelR.width -= buttonsWidth;
  596. GUI.Label(labelR, shader.name);
  597. if (index >= 0)
  598. {
  599. if (VFXExternalShaderProcessor.allowExternalization && index < resource.GetShaderSourceCount())
  600. {
  601. string shaderSourceName = resource.GetShaderSourceName(index);
  602. string externalPath = directory + shaderSourceName;
  603. externalPath = directory + shaderSourceName.Replace('/', '_') + VFXExternalShaderProcessor.k_ShaderExt;
  604. Rect buttonRect = r;
  605. buttonRect.xMin = labelR.xMax;
  606. buttonRect.width = 80;
  607. labelR.width += 80;
  608. if (System.IO.File.Exists(externalPath))
  609. {
  610. if (GUI.Button(buttonRect, "Reveal External"))
  611. {
  612. EditorUtility.RevealInFinder(externalPath);
  613. }
  614. }
  615. else
  616. {
  617. if (GUI.Button(buttonRect, "Externalize"))
  618. {
  619. Directory.CreateDirectory(directory);
  620. File.WriteAllText(externalPath, "//" + shaderSourceName + "," + index.ToString() + "\n//Don't delete the previous line or this one\n" + resource.GetShaderSource(index));
  621. }
  622. }
  623. }
  624. Rect buttonR = r;
  625. buttonR.xMin = labelR.xMax;
  626. buttonR.width = 110;
  627. labelR.width += 110;
  628. if (GUI.Button(buttonR, "Show Generated"))
  629. {
  630. resource.ShowGeneratedShaderFile(index);
  631. }
  632. }
  633. Rect selectButtonR = r;
  634. selectButtonR.xMin = labelR.xMax;
  635. selectButtonR.width = 50;
  636. if (GUI.Button(selectButtonR, "Select"))
  637. {
  638. Selection.activeObject = shader;
  639. }
  640. GUILayout.EndHorizontal();
  641. }
  642. }
  643. }
  644. GUI.enabled = false;
  645. }
  646. }
  647. static class VFXPreviewGUI
  648. {
  649. static int sliderHash = "Slider".GetHashCode();
  650. public static Vector2 Drag2D(Vector2 scrollPosition, Rect position)
  651. {
  652. int id = GUIUtility.GetControlID(sliderHash, FocusType.Passive);
  653. Event evt = Event.current;
  654. switch (evt.GetTypeForControl(id))
  655. {
  656. case EventType.MouseDown:
  657. if (position.Contains(evt.mousePosition) && position.width > 50)
  658. {
  659. GUIUtility.hotControl = id;
  660. evt.Use();
  661. EditorGUIUtility.SetWantsMouseJumping(1);
  662. }
  663. break;
  664. case EventType.MouseDrag:
  665. if (GUIUtility.hotControl == id)
  666. {
  667. scrollPosition -= -evt.delta * (evt.shift ? 3 : 1) / Mathf.Min(position.width, position.height) * 140.0f;
  668. scrollPosition.y = Mathf.Clamp(scrollPosition.y, -90, 90);
  669. evt.Use();
  670. GUI.changed = true;
  671. }
  672. break;
  673. case EventType.MouseUp:
  674. if (GUIUtility.hotControl == id)
  675. GUIUtility.hotControl = 0;
  676. EditorGUIUtility.SetWantsMouseJumping(0);
  677. break;
  678. }
  679. return scrollPosition;
  680. }
  681. }