From d907e23b54d40014f78e1faee519ec20da8b774d Mon Sep 17 00:00:00 2001 From: Tristan Genevet Date: Mon, 16 Mar 2020 16:18:24 +0100 Subject: [PATCH 1/7] safer default resources --- .../Editor/Utils/VFXResources.cs | 155 ++++++++++-------- 1 file changed, 88 insertions(+), 67 deletions(-) diff --git a/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs b/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs index f0418b9546d..103599c36dd 100644 --- a/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs +++ b/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs @@ -6,7 +6,7 @@ namespace UnityEditor.VFX { - class VFXResources : ScriptableObject + class VFXResources { public static VFXResources defaultResources { @@ -21,8 +21,7 @@ public static VFXResources defaultResources } private static VFXResources s_Instance; - private const string defaultFileName = "VFXDefaultResources.asset"; - private static string defaultPath { get { return VisualEffectGraphPackageInfo.assetPackagePath + "/"; } } // Change this to a getter once we handle package mode paths + private static string defaultPath { get { return VisualEffectGraphPackageInfo.assetPackagePath + "/"; } } private static T SafeLoadAssetAtPath(string assetPath) where T : Object { @@ -34,82 +33,104 @@ private static T SafeLoadAssetAtPath(string assetPath) where T : Object } return asset; } - - private static void Initialize() + private void Initialize() { - string[] guids = AssetDatabase.FindAssets("t:VFXResources"); - - VFXResources asset = null; + VFXResources newAsset = new VFXResources(); - if (guids.Length > 0) - asset = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guids[0])); + newAsset.shader = Shader.Find("Hidden/Default StaticMeshOutput"); - if (asset == null) + newAsset.animationCurve = new AnimationCurve(new Keyframe[] { - VFXResources newAsset = CreateInstance(); - - newAsset.particleTexture = SafeLoadAssetAtPath(defaultPath + "Textures/DefaultParticle.tga"); - newAsset.noiseTexture = SafeLoadAssetAtPath(defaultPath + "Textures/Noise.tga"); - newAsset.vectorField = SafeLoadAssetAtPath(defaultPath + "Textures/vectorfield.asset"); - newAsset.signedDistanceField = SafeLoadAssetAtPath(defaultPath + "Textures/SignedDistanceField.asset"); - newAsset.mesh = Resources.GetBuiltinResource("New-Capsule.fbx"); - - newAsset.shader = Shader.Find("Hidden/Default StaticMeshOutput"); + new Keyframe(0.0f, 0.0f, 0.0f, 0.0f), + new Keyframe(0.25f, 0.25f, 0.0f, 0.0f), + new Keyframe(1.0f, 0.0f, 0.0f, 0.0f), + }); - newAsset.animationCurve = new AnimationCurve(new Keyframe[] - { - new Keyframe(0.0f, 0.0f, 0.0f, 0.0f), - new Keyframe(0.25f, 0.25f, 0.0f, 0.0f), - new Keyframe(1.0f, 0.0f, 0.0f, 0.0f), - }); + newAsset.gradient = new Gradient(); + newAsset.gradient.colorKeys = new GradientColorKey[] + { + new GradientColorKey(Color.white, 0.0f), + new GradientColorKey(Color.gray, 1.0f), + }; + newAsset.gradient.alphaKeys = new GradientAlphaKey[] + { + new GradientAlphaKey(0.0f, 0.0f), + new GradientAlphaKey(1.0f, 0.1f), + new GradientAlphaKey(0.8f, 0.8f), + new GradientAlphaKey(0.0f, 1.0f), + }; + + newAsset.gradientMapRamp = new Gradient(); + newAsset.gradientMapRamp.colorKeys = new GradientColorKey[] + { + new GradientColorKey(new Color(0.0f, 0.0f, 0.0f), 0.0f), + new GradientColorKey(new Color(0.75f, 0.15f, 0.0f), 0.3f), + new GradientColorKey(new Color(1.25f, 0.56f, 0.12f), 0.5f), + new GradientColorKey(new Color(3.5f, 2.0f, 0.5f), 0.7f), + new GradientColorKey(new Color(4.0f, 3.5f, 1.2f), 0.9f), + new GradientColorKey(new Color(12.0f, 10.0f, 2.5f), 1.0f), + }; + newAsset.gradientMapRamp.alphaKeys = new GradientAlphaKey[] + { + new GradientAlphaKey(0.0f, 0.0f), + new GradientAlphaKey(1.0f, 1.0f), + }; - newAsset.gradient = new Gradient(); - newAsset.gradient.colorKeys = new GradientColorKey[] - { - new GradientColorKey(Color.white, 0.0f), - new GradientColorKey(Color.gray, 1.0f), - }; - newAsset.gradient.alphaKeys = new GradientAlphaKey[] - { - new GradientAlphaKey(0.0f, 0.0f), - new GradientAlphaKey(1.0f, 0.1f), - new GradientAlphaKey(0.8f, 0.8f), - new GradientAlphaKey(0.0f, 1.0f), - }; + s_Instance = newAsset; + } + Texture2D m_ParticleTexture; + public Texture2D particleTexture { + get + { + if (m_ParticleTexture == null) + m_ParticleTexture = SafeLoadAssetAtPath(defaultPath + "Textures/DefaultParticle.tga"); + return m_ParticleTexture; + } + } - newAsset.gradientMapRamp = new Gradient(); - newAsset.gradientMapRamp.colorKeys = new GradientColorKey[] - { - new GradientColorKey(new Color(0.0f, 0.0f, 0.0f), 0.0f), - new GradientColorKey(new Color(0.75f, 0.15f, 0.0f), 0.3f), - new GradientColorKey(new Color(1.25f, 0.56f, 0.12f), 0.5f), - new GradientColorKey(new Color(3.5f, 2.0f, 0.5f), 0.7f), - new GradientColorKey(new Color(4.0f, 3.5f, 1.2f), 0.9f), - new GradientColorKey(new Color(12.0f, 10.0f, 2.5f), 1.0f), - }; - newAsset.gradientMapRamp.alphaKeys = new GradientAlphaKey[] - { - new GradientAlphaKey(0.0f, 0.0f), - new GradientAlphaKey(1.0f, 1.0f), - }; + Texture2D m_NoiseTexture; + public Texture2D noiseTexture { + get + { + if (m_NoiseTexture == null) + m_NoiseTexture = SafeLoadAssetAtPath(defaultPath + "Textures/Noise.tga"); + return m_NoiseTexture; + } + } + Texture3D m_VectorField; + public Texture3D vectorField { + get + { + if( m_VectorField == null) + m_VectorField = SafeLoadAssetAtPath(defaultPath + "Textures/vectorfield.asset"); + return m_VectorField; + } + } + Texture3D m_SignedDistanceField; - AssetDatabase.CreateAsset(newAsset, "Assets/" + defaultFileName); - asset = SafeLoadAssetAtPath("Assets/" + defaultFileName); + public Texture3D signedDistanceField { + get + { + if (m_SignedDistanceField == null) + m_SignedDistanceField = SafeLoadAssetAtPath(defaultPath + "Textures/SignedDistanceField.asset"); + return m_SignedDistanceField; } - s_Instance = asset; } - [Header("Default Resources")] - public Texture2D particleTexture; - public Texture2D noiseTexture; - public Texture3D vectorField; - public Texture3D signedDistanceField; - public Mesh mesh; - public AnimationCurve animationCurve; - public Gradient gradient; - public Gradient gradientMapRamp; - public Shader shader; + Mesh m_Mesh; + public Mesh mesh { + get + { + if(m_Mesh == null) + m_Mesh = Resources.GetBuiltinResource("New-Capsule.fbx"); + return m_Mesh; + } + } + public AnimationCurve animationCurve { get; private set; } + public Gradient gradient { get; private set; } + public Gradient gradientMapRamp { get; private set; } + public Shader shader { get; private set; } } } From b641a699ca6f83250617be1fce6c0b8feb3d19dd Mon Sep 17 00:00:00 2001 From: Tristan Genevet Date: Tue, 17 Mar 2020 13:07:50 +0100 Subject: [PATCH 2/7] small fix --- com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs b/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs index 103599c36dd..4a9535bd9bf 100644 --- a/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs +++ b/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs @@ -33,7 +33,7 @@ private static T SafeLoadAssetAtPath(string assetPath) where T : Object } return asset; } - private void Initialize() + private static void Initialize() { VFXResources newAsset = new VFXResources(); From cd8ca1ff28dab16f675611cf72f579605876a16d Mon Sep 17 00:00:00 2001 From: Tristan Genevet Date: Thu, 2 Apr 2020 15:13:09 +0200 Subject: [PATCH 3/7] SanitizeGraph called from OnPreprocessAsset. So that importer never modifies asset. --- .../Editor/Models/VFXGraph.cs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/com.unity.visualeffectgraph/Editor/Models/VFXGraph.cs b/com.unity.visualeffectgraph/Editor/Models/VFXGraph.cs index 9afdaeb4930..ff1de808464 100644 --- a/com.unity.visualeffectgraph/Editor/Models/VFXGraph.cs +++ b/com.unity.visualeffectgraph/Editor/Models/VFXGraph.cs @@ -18,6 +18,19 @@ namespace UnityEditor.VFX [InitializeOnLoad] class VFXGraphPreprocessor : AssetPostprocessor { + void OnPreprocessAsset() + { + bool isVFX = assetPath.EndsWith(VisualEffectResource.Extension); + if (isVFX) + { + VisualEffectResource resource = VisualEffectResource.GetResourceAtPath(assetPath); + if (resource == null) + return; + Debug.Log("Sanitizing graph"); + resource.GetOrCreateGraph().SanitizeGraph(); + } + } + static string[] OnAddResourceDependencies(string assetPath) { VisualEffectResource resource = VisualEffectResource.GetResourceAtPath(assetPath); @@ -685,8 +698,7 @@ public void CompileForImport() } } } - - SanitizeGraph(); + // Graph must have been sanitized at this point by the VFXGraphPreprocessor.OnPreprocess BuildSubgraphDependencies(); PrepareSubgraphs(); From 02fdf1b4a80de860ef44eb1aa0df3a5121ac9cb2 Mon Sep 17 00:00:00 2001 From: Tristan Genevet Date: Thu, 2 Apr 2020 15:14:16 +0200 Subject: [PATCH 4/7] Optional VFXResources asset can be created by user and override code defaults. --- .../Assets/VFXDefaultResources.asset | 112 --------- .../Assets/VFXDefaultResources.asset.meta | 8 - .../Editor/Utils/VFXResources.cs | 233 ++++++++++++++---- .../Editor/VFXAssetEditorUtility.cs | 16 ++ 4 files changed, 204 insertions(+), 165 deletions(-) delete mode 100644 TestProjects/VisualEffectGraph/Assets/VFXDefaultResources.asset delete mode 100644 TestProjects/VisualEffectGraph/Assets/VFXDefaultResources.asset.meta diff --git a/TestProjects/VisualEffectGraph/Assets/VFXDefaultResources.asset b/TestProjects/VisualEffectGraph/Assets/VFXDefaultResources.asset deleted file mode 100644 index 9e63d918c6b..00000000000 --- a/TestProjects/VisualEffectGraph/Assets/VFXDefaultResources.asset +++ /dev/null @@ -1,112 +0,0 @@ -%YAML 1.1 -%TAG !u! tag:unity3d.com,2011: ---- !u!114 &11400000 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: cd0a560c562a33e4b94f515804e2bd27, type: 3} - m_Name: VFXDefaultResources - m_EditorClassIdentifier: - particleTexture: {fileID: 2800000, guid: 276d9e395ae18fe40a9b4988549f2349, type: 3} - noiseTexture: {fileID: 2800000, guid: 1d8481de16af723418a688958c41224b, type: 3} - vectorField: {fileID: 11700000, guid: 08937e3134903c5488be506a2dac71e9, type: 2} - signedDistanceField: {fileID: 11700000, guid: 5c2949c31aafddd4e8011ffdebb8fdf7, - type: 2} - mesh: {fileID: 10208, guid: 0000000000000000e000000000000000, type: 0} - animationCurve: - serializedVersion: 2 - m_Curve: - - serializedVersion: 3 - time: 0 - value: 0 - inSlope: 0 - outSlope: 0 - tangentMode: 0 - weightedMode: 0 - inWeight: 0 - outWeight: 0 - - serializedVersion: 3 - time: 0.25 - value: 0.25 - inSlope: 0 - outSlope: 0 - tangentMode: 0 - weightedMode: 0 - inWeight: 0 - outWeight: 0 - - serializedVersion: 3 - time: 1 - value: 0 - inSlope: 0 - outSlope: 0 - tangentMode: 0 - weightedMode: 0 - inWeight: 0 - outWeight: 0 - m_PreInfinity: 2 - m_PostInfinity: 2 - m_RotationOrder: 4 - gradient: - serializedVersion: 2 - key0: {r: 1, g: 1, b: 1, a: 0} - key1: {r: 0.5, g: 0.5, b: 0.5, a: 1} - key2: {r: 0, g: 0, b: 0, a: 0.8} - key3: {r: 0, g: 0, b: 0, a: 0} - key4: {r: 0, g: 0, b: 0, a: 0} - key5: {r: 0, g: 0, b: 0, a: 0} - key6: {r: 0, g: 0, b: 0, a: 0} - key7: {r: 0, g: 0, b: 0, a: 0} - ctime0: 0 - ctime1: 65535 - ctime2: 0 - ctime3: 0 - ctime4: 0 - ctime5: 0 - ctime6: 0 - ctime7: 0 - atime0: 0 - atime1: 6554 - atime2: 52428 - atime3: 65535 - atime4: 0 - atime5: 0 - atime6: 0 - atime7: 0 - m_Mode: 0 - m_NumColorKeys: 2 - m_NumAlphaKeys: 4 - gradientMapRamp: - serializedVersion: 2 - key0: {r: 0, g: 0, b: 0, a: 0} - key1: {r: 0.75, g: 0.15, b: 0, a: 1} - key2: {r: 1.25, g: 0.56, b: 0.12, a: 0} - key3: {r: 3.5, g: 2, b: 0.5, a: 0} - key4: {r: 4, g: 3.5, b: 1.2, a: 0} - key5: {r: 12, g: 10, b: 2.5, a: 0} - key6: {r: 0, g: 0, b: 0, a: 0} - key7: {r: 0, g: 0, b: 0, a: 0} - ctime0: 0 - ctime1: 19661 - ctime2: 32768 - ctime3: 45875 - ctime4: 58982 - ctime5: 65535 - ctime6: 0 - ctime7: 0 - atime0: 0 - atime1: 65535 - atime2: 0 - atime3: 0 - atime4: 0 - atime5: 0 - atime6: 0 - atime7: 0 - m_Mode: 0 - m_NumColorKeys: 6 - m_NumAlphaKeys: 2 - shader: {fileID: 4800000, guid: cd270bc83dc0ce644bf351c3f5b7f30f, type: 3} diff --git a/TestProjects/VisualEffectGraph/Assets/VFXDefaultResources.asset.meta b/TestProjects/VisualEffectGraph/Assets/VFXDefaultResources.asset.meta deleted file mode 100644 index 642759099b6..00000000000 --- a/TestProjects/VisualEffectGraph/Assets/VFXDefaultResources.asset.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 38f456076c02c0240bb8ec1657a1940b -NativeFormatImporter: - externalObjects: {} - mainObjectFileID: 11400000 - userData: - assetBundleName: - assetBundleVariant: diff --git a/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs b/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs index 4a9535bd9bf..2faa7b8d3a0 100644 --- a/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs +++ b/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs @@ -6,20 +6,129 @@ namespace UnityEditor.VFX { - class VFXResources + class VFXResources : ScriptableObject { - public static VFXResources defaultResources + public static Values defaultResources { get { - if (s_Instance == null) + if (s_Values == null) { Initialize(); } - return s_Instance; + return s_Values; } } private static VFXResources s_Instance; + private static Values s_Values; + + void OnEnable() + { + if( s_Instance != null) + { + Debug.LogError("Having more than on VFXResources in you project is unsupported"); + } + s_Instance = this; + } + + void OnDisable() + { + if( s_Instance == this) + s_Instance = null; + } + + public class Values + { + public AnimationCurve animationCurve + { + get + { + if (s_Instance != null) + return s_Instance.animationCurve; + + return defaultAnimationCurve; + } + } + public Gradient gradient + { + get + { + if (s_Instance != null) + return s_Instance.gradient; + return defaultGradient; + } + } + public Gradient gradientMapRamp + { + get + { + if (s_Instance != null) + return s_Instance.gradientMapRamp; + return defaultGradientMapRamp; + } + } + + public Shader shader + { + get + { + if (s_Instance != null && s_Instance.shader != null) + return s_Instance.shader; + + return defaultShader; + } + } + + + public Texture2D particleTexture + { + get + { + if (s_Instance != null && s_Instance.particleTexture != null) + return s_Instance.particleTexture; + return defaultParticleTexture; + } + } + + public Texture2D noiseTexture + { + get + { + if (s_Instance != null && s_Instance.noiseTexture != null) + return s_Instance.noiseTexture; + return defaultNoiseTexture; + } + } + public Texture3D vectorField + { + get + { + if (s_Instance != null && s_Instance.vectorField != null) + return s_Instance.vectorField; + return defaultVectorField; + } + } + public Texture3D signedDistanceField + { + get + { + if (s_Instance != null && s_Instance.signedDistanceField != null) + return s_Instance.signedDistanceField; + return defaultSignedDistanceField; + } + } + + public Mesh mesh + { + get + { + if (s_Instance != null && s_Instance.mesh != null) + return s_Instance.mesh; + + return defaultMesh; + } + } + } private static string defaultPath { get { return VisualEffectGraphPackageInfo.assetPackagePath + "/"; } } @@ -35,25 +144,24 @@ private static T SafeLoadAssetAtPath(string assetPath) where T : Object } private static void Initialize() { + s_Values = new Values(); - VFXResources newAsset = new VFXResources(); - - newAsset.shader = Shader.Find("Hidden/Default StaticMeshOutput"); + defaultShader = Shader.Find("Hidden/Default StaticMeshOutput"); - newAsset.animationCurve = new AnimationCurve(new Keyframe[] + defaultAnimationCurve = new AnimationCurve(new Keyframe[] { new Keyframe(0.0f, 0.0f, 0.0f, 0.0f), new Keyframe(0.25f, 0.25f, 0.0f, 0.0f), new Keyframe(1.0f, 0.0f, 0.0f, 0.0f), }); - newAsset.gradient = new Gradient(); - newAsset.gradient.colorKeys = new GradientColorKey[] + defaultGradient = new Gradient(); + defaultGradient.colorKeys = new GradientColorKey[] { new GradientColorKey(Color.white, 0.0f), new GradientColorKey(Color.gray, 1.0f), }; - newAsset.gradient.alphaKeys = new GradientAlphaKey[] + defaultGradient.alphaKeys = new GradientAlphaKey[] { new GradientAlphaKey(0.0f, 0.0f), new GradientAlphaKey(1.0f, 0.1f), @@ -61,8 +169,8 @@ private static void Initialize() new GradientAlphaKey(0.0f, 1.0f), }; - newAsset.gradientMapRamp = new Gradient(); - newAsset.gradientMapRamp.colorKeys = new GradientColorKey[] + defaultGradientMapRamp = new Gradient(); + defaultGradientMapRamp.colorKeys = new GradientColorKey[] { new GradientColorKey(new Color(0.0f, 0.0f, 0.0f), 0.0f), new GradientColorKey(new Color(0.75f, 0.15f, 0.0f), 0.3f), @@ -71,66 +179,101 @@ private static void Initialize() new GradientColorKey(new Color(4.0f, 3.5f, 1.2f), 0.9f), new GradientColorKey(new Color(12.0f, 10.0f, 2.5f), 1.0f), }; - newAsset.gradientMapRamp.alphaKeys = new GradientAlphaKey[] + defaultGradientMapRamp.alphaKeys = new GradientAlphaKey[] { new GradientAlphaKey(0.0f, 0.0f), new GradientAlphaKey(1.0f, 1.0f), }; - - s_Instance = newAsset; } - Texture2D m_ParticleTexture; - public Texture2D particleTexture { + static Texture2D m_DefaultParticleTexture; + public static Texture2D defaultParticleTexture { get { - if (m_ParticleTexture == null) - m_ParticleTexture = SafeLoadAssetAtPath(defaultPath + "Textures/DefaultParticle.tga"); - return m_ParticleTexture; + if (m_DefaultParticleTexture == null) + m_DefaultParticleTexture = SafeLoadAssetAtPath(defaultPath + "Textures/DefaultParticle.tga"); + return m_DefaultParticleTexture; } } - Texture2D m_NoiseTexture; - public Texture2D noiseTexture { + static Texture2D m_DefaultNoiseTexture; + public static Texture2D defaultNoiseTexture { get { - if (m_NoiseTexture == null) - m_NoiseTexture = SafeLoadAssetAtPath(defaultPath + "Textures/Noise.tga"); - return m_NoiseTexture; + if (m_DefaultNoiseTexture == null) + m_DefaultNoiseTexture = SafeLoadAssetAtPath(defaultPath + "Textures/Noise.tga"); + return m_DefaultNoiseTexture; } } - Texture3D m_VectorField; - public Texture3D vectorField { + static Texture3D m_DefaultVectorField; + public static Texture3D defaultVectorField { get { - if( m_VectorField == null) - m_VectorField = SafeLoadAssetAtPath(defaultPath + "Textures/vectorfield.asset"); - return m_VectorField; + if( m_DefaultVectorField == null) + m_DefaultVectorField = SafeLoadAssetAtPath(defaultPath + "Textures/vectorfield.asset"); + return m_DefaultVectorField; } } - Texture3D m_SignedDistanceField; + static Texture3D m_DefaultSignedDistanceField; - public Texture3D signedDistanceField { + public static Texture3D defaultSignedDistanceField { get { - if (m_SignedDistanceField == null) - m_SignedDistanceField = SafeLoadAssetAtPath(defaultPath + "Textures/SignedDistanceField.asset"); - return m_SignedDistanceField; + if (m_DefaultSignedDistanceField == null) + m_DefaultSignedDistanceField = SafeLoadAssetAtPath(defaultPath + "Textures/SignedDistanceField.asset"); + return m_DefaultSignedDistanceField; } } - Mesh m_Mesh; - public Mesh mesh { + static Mesh m_DefaultMesh; + static public Mesh defaultMesh { get { - if(m_Mesh == null) - m_Mesh = Resources.GetBuiltinResource("New-Capsule.fbx"); - return m_Mesh; + if(m_DefaultMesh == null) + m_DefaultMesh = Resources.GetBuiltinResource("New-Capsule.fbx"); + return m_DefaultMesh; } } - public AnimationCurve animationCurve { get; private set; } - public Gradient gradient { get; private set; } - public Gradient gradientMapRamp { get; private set; } - public Shader shader { get; private set; } + + [SerializeField] + AnimationCurve animationCurve = null; + + [SerializeField] + Gradient gradient = null; + + [SerializeField] + Gradient gradientMapRamp = null; + + [SerializeField] + Shader shader = null; + + [SerializeField] + Texture2D particleTexture = null; + + [SerializeField] + Texture2D noiseTexture = null; + + [SerializeField] + Texture3D vectorField = null; + + [SerializeField] + Texture3D signedDistanceField = null; + + [SerializeField] + Mesh mesh = null; + + static AnimationCurve defaultAnimationCurve; + static Gradient defaultGradient; + static Gradient defaultGradientMapRamp; + static Shader defaultShader; + + public void SetDefaults() + { + if( s_Values == null) + Initialize(); + animationCurve = defaultAnimationCurve; + gradient = defaultGradient; + gradientMapRamp = defaultGradientMapRamp; + } } } diff --git a/com.unity.visualeffectgraph/Editor/VFXAssetEditorUtility.cs b/com.unity.visualeffectgraph/Editor/VFXAssetEditorUtility.cs index 3b65d2973b3..4934abbac61 100644 --- a/com.unity.visualeffectgraph/Editor/VFXAssetEditorUtility.cs +++ b/com.unity.visualeffectgraph/Editor/VFXAssetEditorUtility.cs @@ -112,6 +112,22 @@ public static void CreateVisualEffectAsset() ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, action, "New VFX.vfx", texture, null); } + [MenuItem("Assets/Create/Visual Effects/Visual Effect Defaults", false, 307)] + public static void CreateVisualEffectDefaults() + { + var obj = VFXResources.CreateInstance(); + obj.SetDefaults(); + AssetDatabase.CreateAsset(obj, "Assets/Visual Effects Defaults.asset"); + Selection.activeObject = obj; + } + + [MenuItem("Assets/Create/Visual Effects/Visual Effect Defaults", true)] + public static bool IsCreateVisualEffectDefaultsActive() + { + var resources = Resources.FindObjectsOfTypeAll(); + return resources == null || resources.Length == 0; + } + internal class DoCreateNewVFX : EndNameEditAction { public override void Action(int instanceId, string pathName, string resourceFile) From ca7858c26b7839e52e420b60423ebdd0c9360a7f Mon Sep 17 00:00:00 2001 From: Tristan Genevet Date: Tue, 14 Apr 2020 16:58:48 +0200 Subject: [PATCH 5/7] Don't sanitize if the dependencies are not already here at first import. --- .../Editor/Models/VFXGraph.cs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/com.unity.visualeffectgraph/Editor/Models/VFXGraph.cs b/com.unity.visualeffectgraph/Editor/Models/VFXGraph.cs index ff1de808464..9cc3afba35b 100644 --- a/com.unity.visualeffectgraph/Editor/Models/VFXGraph.cs +++ b/com.unity.visualeffectgraph/Editor/Models/VFXGraph.cs @@ -27,7 +27,8 @@ void OnPreprocessAsset() if (resource == null) return; Debug.Log("Sanitizing graph"); - resource.GetOrCreateGraph().SanitizeGraph(); + + resource.GetOrCreateGraph().SanitizeForImport(); } } @@ -678,6 +679,24 @@ IEnumerable GetAllGraphs() where T : VisualEffectObject //Explicit compile must be used if we want to force compilation even if a dependency is needed, which me must not do on a deleted library import. public static bool explicitCompile { get; set; } = false; + + public void SanitizeForImport() + { + if (!explicitCompile) + { + HashSet dependentAsset = new HashSet(); + GetImportDependentAssets(dependentAsset); + + foreach (var instanceID in dependentAsset) + { + if (EditorUtility.InstanceIDToObject(instanceID) == null) + { + return; + } + } + } + SanitizeGraph(); + } public void CompileForImport() { if (!GetResource().isSubgraph) @@ -693,7 +712,6 @@ public void CompileForImport() { if (EditorUtility.InstanceIDToObject(instanceID) == null) { - //Debug.LogWarning("Refusing to compile " + AssetDatabase.GetAssetPath(this) + "because dependency is not yet loaded"); return; } } From a4f2a793ac79ae190718df2878790d1e91002821 Mon Sep 17 00:00:00 2001 From: Tristan Genevet Date: Thu, 16 Apr 2020 13:05:32 +0200 Subject: [PATCH 6/7] removed comment. --- com.unity.visualeffectgraph/Editor/Models/VFXGraph.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/com.unity.visualeffectgraph/Editor/Models/VFXGraph.cs b/com.unity.visualeffectgraph/Editor/Models/VFXGraph.cs index 9cc3afba35b..ca8a898fde4 100644 --- a/com.unity.visualeffectgraph/Editor/Models/VFXGraph.cs +++ b/com.unity.visualeffectgraph/Editor/Models/VFXGraph.cs @@ -26,7 +26,6 @@ void OnPreprocessAsset() VisualEffectResource resource = VisualEffectResource.GetResourceAtPath(assetPath); if (resource == null) return; - Debug.Log("Sanitizing graph"); resource.GetOrCreateGraph().SanitizeForImport(); } From 6bdcba8b05ec813a15a1787c890300af9bdd33da Mon Sep 17 00:00:00 2001 From: Tristan Genevet Date: Thu, 16 Apr 2020 13:47:21 +0200 Subject: [PATCH 7/7] Fix for detection of Default resource asset --- .../Editor/Utils/VFXResources.cs | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs b/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs index 2faa7b8d3a0..6ebbd43cb95 100644 --- a/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs +++ b/com.unity.visualeffectgraph/Editor/Utils/VFXResources.cs @@ -24,25 +24,19 @@ public static Values defaultResources void OnEnable() { - if( s_Instance != null) - { + if (Resources.FindObjectsOfTypeAll().Length > 1) Debug.LogError("Having more than on VFXResources in you project is unsupported"); - } s_Instance = this; } - void OnDisable() - { - if( s_Instance == this) - s_Instance = null; - } - public class Values { public AnimationCurve animationCurve { get { + if (s_Instance == null) + s_Instance = FindObjectOfType(); if (s_Instance != null) return s_Instance.animationCurve; @@ -53,6 +47,8 @@ public Gradient gradient { get { + if (s_Instance == null) + s_Instance = FindObjectOfType(); if (s_Instance != null) return s_Instance.gradient; return defaultGradient; @@ -62,6 +58,8 @@ public Gradient gradientMapRamp { get { + if (s_Instance == null) + s_Instance = FindObjectOfType(); if (s_Instance != null) return s_Instance.gradientMapRamp; return defaultGradientMapRamp; @@ -72,6 +70,8 @@ public Shader shader { get { + if (s_Instance == null) + s_Instance = FindObjectOfType(); if (s_Instance != null && s_Instance.shader != null) return s_Instance.shader; @@ -84,6 +84,8 @@ public Texture2D particleTexture { get { + if (s_Instance == null) + s_Instance = FindObjectOfType(); if (s_Instance != null && s_Instance.particleTexture != null) return s_Instance.particleTexture; return defaultParticleTexture; @@ -94,6 +96,8 @@ public Texture2D noiseTexture { get { + if (s_Instance == null) + s_Instance = FindObjectOfType(); if (s_Instance != null && s_Instance.noiseTexture != null) return s_Instance.noiseTexture; return defaultNoiseTexture; @@ -103,6 +107,8 @@ public Texture3D vectorField { get { + if (s_Instance == null) + s_Instance = FindObjectOfType(); if (s_Instance != null && s_Instance.vectorField != null) return s_Instance.vectorField; return defaultVectorField; @@ -112,6 +118,8 @@ public Texture3D signedDistanceField { get { + if (s_Instance == null) + s_Instance = FindObjectOfType(); if (s_Instance != null && s_Instance.signedDistanceField != null) return s_Instance.signedDistanceField; return defaultSignedDistanceField; @@ -122,6 +130,8 @@ public Mesh mesh { get { + if (s_Instance == null) + s_Instance = FindObjectOfType(); if (s_Instance != null && s_Instance.mesh != null) return s_Instance.mesh;