123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using UnityEditor.PackageManager;
- using UnityEngine.Rendering;
- namespace UnityEditor.VFX
- {
- class VFXResources : ScriptableObject
- {
- public static Values defaultResources
- {
- get
- {
- if (s_Values == null)
- {
- Initialize();
- }
- return s_Values;
- }
- }
- private static bool m_Searched; // the instance has been searched and it is null
- private static VFXResources s_Instance;
- private static Values s_Values;
- static void LoadUserResourcesIfNeeded()
- {
- if (s_Instance == null && (!m_Searched || !object.ReferenceEquals(s_Instance, null)))
- // if instance is null and either it has never been searched or it was found but it has been destroyed since last time
- {
- foreach (var guid in AssetDatabase.FindAssets("t:VFXResources"))
- {
- s_Instance = AssetDatabase.LoadAssetAtPath<VFXResources>(AssetDatabase.GUIDToAssetPath(guid));
- if (s_Instance != null)
- {
- return;
- }
- }
- s_Instance = null;
- m_Searched = true;
- }
- }
- void OnEnable()
- {
- if (AssetDatabase.FindAssets("t:VFXResources").Length > 1)
- Debug.LogError("Having more than one VFXResources in your project is unsupported");
- s_Instance = this;
- m_Searched = false;
- }
- public class Values
- {
- public AnimationCurve animationCurve
- {
- get
- {
- LoadUserResourcesIfNeeded();
- if (s_Instance != null)
- return s_Instance.animationCurve;
- return defaultAnimationCurve;
- }
- }
- public Gradient gradient
- {
- get
- {
- LoadUserResourcesIfNeeded();
- if (s_Instance != null)
- return s_Instance.gradient;
- return defaultGradient;
- }
- }
- public Gradient gradientMapRamp
- {
- get
- {
- LoadUserResourcesIfNeeded();
- if (s_Instance != null)
- return s_Instance.gradientMapRamp;
- return defaultGradientMapRamp;
- }
- }
- public Shader shader
- {
- get
- {
- LoadUserResourcesIfNeeded();
- if (s_Instance != null && s_Instance.shader != null)
- return s_Instance.shader;
- return defaultShader;
- }
- }
- public Texture2D particleTexture
- {
- get
- {
- LoadUserResourcesIfNeeded();
- if (s_Instance != null && s_Instance.particleTexture != null)
- return s_Instance.particleTexture;
- return defaultParticleTexture;
- }
- }
- public Texture2D noiseTexture
- {
- get
- {
- LoadUserResourcesIfNeeded();
- if (s_Instance != null && s_Instance.noiseTexture != null)
- return s_Instance.noiseTexture;
- return defaultNoiseTexture;
- }
- }
- public Texture3D vectorField
- {
- get
- {
- LoadUserResourcesIfNeeded();
- if (s_Instance != null && s_Instance.vectorField != null)
- return s_Instance.vectorField;
- return defaultVectorField;
- }
- }
- public Texture3D signedDistanceField
- {
- get
- {
- LoadUserResourcesIfNeeded();
- if (s_Instance != null && s_Instance.signedDistanceField != null)
- return s_Instance.signedDistanceField;
- return defaultSignedDistanceField;
- }
- }
- public Mesh mesh
- {
- get
- {
- LoadUserResourcesIfNeeded();
- if (s_Instance != null && s_Instance.mesh != null)
- return s_Instance.mesh;
- return defaultMesh;
- }
- }
- public string userTemplateDirectory
- {
- get
- {
- LoadUserResourcesIfNeeded();
- if (s_Instance != null)
- return s_Instance.userTemplateDirectory;
- return "";
- }
- set
- {
- if (s_Instance == null)
- s_Instance = FindObjectOfType<VFXResources>();
- if (s_Instance != null)
- s_Instance.userTemplateDirectory = value;
- }
- }
- }
- private static string defaultPath { get { return VisualEffectGraphPackageInfo.assetPackagePath + "/"; } }
- private static T SafeLoadAssetAtPath<T>(string assetPath) where T : Object
- {
- var asset = AssetDatabase.LoadAssetAtPath<T>(assetPath);
- if (asset == null)
- {
- AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceSynchronousImport | ImportAssetOptions.ForceUpdate);
- asset = AssetDatabase.LoadAssetAtPath<T>(assetPath);
- }
- return asset;
- }
- private static void Initialize()
- {
- s_Values = new Values();
- defaultShader = Shader.Find("Hidden/Default StaticMeshOutput");
- 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),
- });
- defaultGradient = new Gradient();
- defaultGradient.colorKeys = new GradientColorKey[]
- {
- new GradientColorKey(Color.white, 0.0f),
- new GradientColorKey(Color.gray, 1.0f),
- };
- defaultGradient.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),
- };
- 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),
- 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),
- };
- defaultGradientMapRamp.alphaKeys = new GradientAlphaKey[]
- {
- new GradientAlphaKey(0.0f, 0.0f),
- new GradientAlphaKey(1.0f, 1.0f),
- };
- }
- static Texture2D m_DefaultParticleTexture;
- public static Texture2D defaultParticleTexture
- {
- get
- {
- if (m_DefaultParticleTexture == null)
- m_DefaultParticleTexture = SafeLoadAssetAtPath<Texture2D>(defaultPath + "Textures/DefaultParticle.tga");
- return m_DefaultParticleTexture;
- }
- }
- static Texture2D m_DefaultNoiseTexture;
- public static Texture2D defaultNoiseTexture
- {
- get
- {
- if (m_DefaultNoiseTexture == null)
- m_DefaultNoiseTexture = SafeLoadAssetAtPath<Texture2D>(defaultPath + "Textures/Noise.tga");
- return m_DefaultNoiseTexture;
- }
- }
- static Texture3D m_DefaultVectorField;
- public static Texture3D defaultVectorField
- {
- get
- {
- if (m_DefaultVectorField == null)
- m_DefaultVectorField = SafeLoadAssetAtPath<Texture3D>(defaultPath + "Textures/vectorfield.asset");
- return m_DefaultVectorField;
- }
- }
- static Texture3D m_DefaultSignedDistanceField;
- public static Texture3D defaultSignedDistanceField
- {
- get
- {
- if (m_DefaultSignedDistanceField == null)
- m_DefaultSignedDistanceField = SafeLoadAssetAtPath<Texture3D>(defaultPath + "Textures/SignedDistanceField.asset");
- return m_DefaultSignedDistanceField;
- }
- }
- static Mesh m_DefaultMesh;
- static public Mesh defaultMesh
- {
- get
- {
- if (m_DefaultMesh == null)
- m_DefaultMesh = Resources.GetBuiltinResource<Mesh>("New-Capsule.fbx");
- return m_DefaultMesh;
- }
- }
- private static ComputeShader m_SdfNormalsComputeShader;
- public static ComputeShader sdfNormalsComputeShader
- {
- get
- {
- if (m_SdfNormalsComputeShader == null)
- m_SdfNormalsComputeShader = SafeLoadAssetAtPath<ComputeShader>(defaultPath + "Shaders/SDFBaker/GenSdfNormals.compute");
- return m_SdfNormalsComputeShader;
- }
- }
- private static ComputeShader m_SdfRayMapComputeShader;
- public static ComputeShader sdfRayMapComputeShader
- {
- get
- {
- if (m_SdfRayMapComputeShader == null)
- m_SdfRayMapComputeShader = SafeLoadAssetAtPath<ComputeShader>(defaultPath + "Shaders/SDFBaker/GenSdfRayMap.compute");
- return m_SdfRayMapComputeShader;
- }
- }
- private static Shader m_RayMapVoxelizeShader;
- public static Shader rayMapVoxelizeShader
- {
- get
- {
- if (m_RayMapVoxelizeShader == null)
- m_RayMapVoxelizeShader = SafeLoadAssetAtPath<Shader>(defaultPath + "Shaders/SDFBaker/RayMapVoxelize.shader");
- return m_RayMapVoxelizeShader;
- }
- }
- [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;
- [SerializeField]
- string userTemplateDirectory = "";
- 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;
- }
- }
- }
|