Andre McGrail
3 年前
当前提交
fe0c7d8e
共有 15 个文件被更改,包括 141 次插入 和 356 次删除
-
15Assets/AddressableAssetsData/AddressableAssetSettings.asset
-
10Assets/AddressableAssetsData/AssetGroups/Utility.asset
-
24Assets/Data/UniversalRP/BoatDemoRenderer.asset
-
2Assets/Scripts/Effects/GlobalVolumeFeature.cs.meta
-
103Assets/Scripts/Effects/GlobalVolumeFeature.cs
-
36Assets/AddressableAssetsData/AssetGroups/Schemas/2bd013e8bb2a14fbc857108db02a8889_BundledAssetGroupSchema.asset
-
8Assets/AddressableAssetsData/AssetGroups/Schemas/2bd013e8bb2a14fbc857108db02a8889_BundledAssetGroupSchema.asset.meta
-
16Assets/AddressableAssetsData/AssetGroups/Schemas/2bd013e8bb2a14fbc857108db02a8889_ContentUpdateGroupSchema.asset
-
8Assets/AddressableAssetsData/AssetGroups/Schemas/2bd013e8bb2a14fbc857108db02a8889_ContentUpdateGroupSchema.asset.meta
-
8Assets/AddressableAssetsData/AssetGroups/VolumeProfiles.asset.meta
-
49Assets/AddressableAssetsData/AssetGroups/VolumeProfiles.asset
-
7Assets/Objects/misc/DefaultVolume.prefab.meta
-
90Assets/Objects/misc/DefaultVolume.prefab
-
121Assets/Scripts/GameSystem/DefaultVolume.cs
-
0/Assets/Scripts/Effects/GlobalVolumeFeature.cs.meta
|
|||
using System.Collections.Generic; |
|||
using UnityEngine; |
|||
using UnityEngine.Rendering; |
|||
using UnityEngine.Rendering.Universal; |
|||
|
|||
public class GlobalVolumeFeature : ScriptableRendererFeature |
|||
{ |
|||
class GlobalVolumePass : ScriptableRenderPass |
|||
{ |
|||
public VolumeProfile _baseProfile; |
|||
public List<VolumeProfile> _qualityProfiles; |
|||
public LayerMask _layerMask; |
|||
|
|||
private Volume vol; |
|||
private Volume qualityVol; |
|||
public static GameObject volumeHolder; |
|||
|
|||
// This method is called before executing the render pass.
|
|||
// It can be used to configure render targets and their clear state. Also to create temporary render target textures.
|
|||
// When empty this render pass will render to the active camera render target.
|
|||
// You should never call CommandBuffer.SetRenderTarget. Instead call <c>ConfigureTarget</c> and <c>ConfigureClear</c>.
|
|||
// The render pipeline will ensure target setup and clearing happens in a performant manner.
|
|||
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData) |
|||
{ |
|||
if(volumeHolder == null) |
|||
{ |
|||
volumeHolder = new GameObject("[DefaultVolume]"); |
|||
vol = volumeHolder.AddComponent<Volume>(); |
|||
vol.isGlobal = true; |
|||
qualityVol = volumeHolder.AddComponent<Volume>(); |
|||
qualityVol.isGlobal = true; |
|||
volumeHolder.hideFlags = HideFlags.HideAndDontSave; |
|||
} |
|||
|
|||
if (vol && _baseProfile) |
|||
{ |
|||
vol.sharedProfile = _baseProfile; |
|||
} |
|||
|
|||
if(qualityVol && _qualityProfiles != null) |
|||
{ |
|||
var index = QualitySettings.GetQualityLevel(); |
|||
|
|||
if(_qualityProfiles.Count >= index) |
|||
qualityVol.sharedProfile = _qualityProfiles?[QualitySettings.GetQualityLevel()]; |
|||
} |
|||
|
|||
} |
|||
|
|||
// Here you can implement the rendering logic.
|
|||
// Use <c>ScriptableRenderContext</c> to issue drawing commands or execute command buffers
|
|||
// https://docs.unity3d.com/ScriptReference/Rendering.ScriptableRenderContext.html
|
|||
// You don't have to call ScriptableRenderContext.submit, the render pipeline will call it at specific points in the pipeline.
|
|||
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) |
|||
{ |
|||
} |
|||
|
|||
// Cleanup any allocated resources that were created during the execution of this render pass.
|
|||
public override void OnCameraCleanup(CommandBuffer cmd) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
GlobalVolumePass m_ScriptablePass; |
|||
|
|||
public LayerMask _layerMask; |
|||
public VolumeProfile _baseProfile; |
|||
public List<VolumeProfile> _qualityProfiles = new List<VolumeProfile>(); |
|||
|
|||
/// <inheritdoc/>
|
|||
public override void Create() |
|||
{ |
|||
m_ScriptablePass = new GlobalVolumePass |
|||
{ |
|||
// Configures where the render pass should be injected.
|
|||
renderPassEvent = RenderPassEvent.BeforeRendering, |
|||
_baseProfile = this._baseProfile, |
|||
_layerMask = this._layerMask, |
|||
_qualityProfiles = this._qualityProfiles, |
|||
}; |
|||
} |
|||
|
|||
// Here you can inject one or multiple render passes in the renderer.
|
|||
// This method is called when setting up the renderer once per-camera.
|
|||
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData) |
|||
{ |
|||
if(GlobalVolumePass.volumeHolder == null) |
|||
{ |
|||
var old = GameObject.Find("[DefaultVolume]"); |
|||
if (Application.isPlaying) |
|||
{ |
|||
Destroy(old); |
|||
} |
|||
else |
|||
{ |
|||
DestroyImmediate(old); |
|||
} |
|||
} |
|||
renderer.EnqueuePass(m_ScriptablePass); |
|||
} |
|||
} |
|||
|
|||
|
|
|||
%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: e5d17a21594effb4e9591490b009e7aa, type: 3} |
|||
m_Name: 2bd013e8bb2a14fbc857108db02a8889_BundledAssetGroupSchema |
|||
m_EditorClassIdentifier: |
|||
m_Group: {fileID: 11400000, guid: 8092aa13f89a049a185fbb3e2e152534, type: 2} |
|||
m_Compression: 1 |
|||
m_IncludeInBuild: 1 |
|||
m_BundledAssetProviderType: |
|||
m_AssemblyName: Unity.ResourceManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null |
|||
m_ClassName: UnityEngine.ResourceManagement.ResourceProviders.BundledAssetProvider |
|||
m_ForceUniqueProvider: 0 |
|||
m_UseAssetBundleCache: 1 |
|||
m_UseAssetBundleCrc: 1 |
|||
m_Timeout: 0 |
|||
m_ChunkedTransfer: 0 |
|||
m_RedirectLimit: -1 |
|||
m_RetryCount: 0 |
|||
m_BuildPath: |
|||
m_Id: a97c2753c313940f3b39c7767c006f00 |
|||
m_LoadPath: |
|||
m_Id: 5baa6669c14d34bcc9d172addd4ffac6 |
|||
m_BundleMode: 0 |
|||
m_AssetBundleProviderType: |
|||
m_AssemblyName: Unity.ResourceManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null |
|||
m_ClassName: UnityEngine.ResourceManagement.ResourceProviders.AssetBundleProvider |
|||
m_BundleNaming: 0 |
|
|||
fileFormatVersion: 2 |
|||
guid: e16bafac92bd546f7b2411b6c28ab796 |
|||
NativeFormatImporter: |
|||
externalObjects: {} |
|||
mainObjectFileID: 0 |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
%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: 5834b5087d578d24c926ce20cd31e6d6, type: 3} |
|||
m_Name: 2bd013e8bb2a14fbc857108db02a8889_ContentUpdateGroupSchema |
|||
m_EditorClassIdentifier: |
|||
m_Group: {fileID: 11400000, guid: 8092aa13f89a049a185fbb3e2e152534, type: 2} |
|||
m_StaticContent: 0 |
|
|||
fileFormatVersion: 2 |
|||
guid: cd30c925ccb374e08be6afe141774141 |
|||
NativeFormatImporter: |
|||
externalObjects: {} |
|||
mainObjectFileID: 0 |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 8092aa13f89a049a185fbb3e2e152534 |
|||
NativeFormatImporter: |
|||
externalObjects: {} |
|||
mainObjectFileID: 0 |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
%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: bbb281ee3bf0b054c82ac2347e9e782c, type: 3} |
|||
m_Name: VolumeProfiles |
|||
m_EditorClassIdentifier: |
|||
m_GroupName: VolumeProfiles |
|||
m_Data: |
|||
m_SerializedData: [] |
|||
m_GUID: 2bd013e8bb2a14fbc857108db02a8889 |
|||
m_SerializeEntries: |
|||
- m_GUID: 200e02ce26d6c422db35ba0777d71a09 |
|||
m_Address: vol_quality_low |
|||
m_ReadOnly: 0 |
|||
m_SerializedLabels: [] |
|||
m_MainAsset: {fileID: 11400000, guid: 200e02ce26d6c422db35ba0777d71a09, type: 2} |
|||
m_TargetAsset: {fileID: 11400000, guid: 200e02ce26d6c422db35ba0777d71a09, type: 2} |
|||
- m_GUID: 4fe8789f8ee054ac49d9f7fe042e0918 |
|||
m_Address: vol_quality_medium |
|||
m_ReadOnly: 0 |
|||
m_SerializedLabels: [] |
|||
m_MainAsset: {fileID: 0} |
|||
m_TargetAsset: {fileID: 0} |
|||
- m_GUID: ad80f6934bc444a93bb29c1f6db70cf3 |
|||
m_Address: vol_quality_base |
|||
m_ReadOnly: 0 |
|||
m_SerializedLabels: [] |
|||
m_MainAsset: {fileID: 11400000, guid: ad80f6934bc444a93bb29c1f6db70cf3, type: 2} |
|||
m_TargetAsset: {fileID: 11400000, guid: ad80f6934bc444a93bb29c1f6db70cf3, type: 2} |
|||
- m_GUID: dbf7710ff9a33440f8390ed8e04dbb77 |
|||
m_Address: vol_quality_high |
|||
m_ReadOnly: 0 |
|||
m_SerializedLabels: [] |
|||
m_MainAsset: {fileID: 0} |
|||
m_TargetAsset: {fileID: 0} |
|||
m_ReadOnly: 0 |
|||
m_Settings: {fileID: 11400000, guid: 46fc3d082b08c4dacacae72d7016c235, type: 2} |
|||
m_SchemaSet: |
|||
m_Schemas: |
|||
- {fileID: 11400000, guid: e16bafac92bd546f7b2411b6c28ab796, type: 2} |
|||
- {fileID: 11400000, guid: cd30c925ccb374e08be6afe141774141, type: 2} |
|
|||
fileFormatVersion: 2 |
|||
guid: d4548d51034644d21971f134c82b6a9a |
|||
PrefabImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
%YAML 1.1 |
|||
%TAG !u! tag:unity3d.com,2011: |
|||
--- !u!1 &6171279960522736352 |
|||
GameObject: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
serializedVersion: 6 |
|||
m_Component: |
|||
- component: {fileID: 5039070232460465497} |
|||
- component: {fileID: 1523940916265185993} |
|||
- component: {fileID: 7861318668517722592} |
|||
- component: {fileID: 3109418422114241795} |
|||
m_Layer: 0 |
|||
m_Name: DefaultVolume |
|||
m_TagString: volume_manager |
|||
m_Icon: {fileID: 0} |
|||
m_NavMeshLayer: 0 |
|||
m_StaticEditorFlags: 0 |
|||
m_IsActive: 1 |
|||
--- !u!4 &5039070232460465497 |
|||
Transform: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 6171279960522736352} |
|||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} |
|||
m_LocalPosition: {x: 0, y: 0, z: 0} |
|||
m_LocalScale: {x: 1, y: 1, z: 1} |
|||
m_Children: [] |
|||
m_Father: {fileID: 0} |
|||
m_RootOrder: 0 |
|||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} |
|||
--- !u!114 &1523940916265185993 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 6171279960522736352} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: 95843df7486904214993a4dd7d1c5828, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|||
volBaseComponent: {fileID: 7861318668517722592} |
|||
volQualityComponent: {fileID: 3109418422114241795} |
|||
qualityVolumes: |
|||
- m_AssetGUID: 200e02ce26d6c422db35ba0777d71a09 |
|||
m_SubObjectName: |
|||
- m_AssetGUID: 4fe8789f8ee054ac49d9f7fe042e0918 |
|||
m_SubObjectName: |
|||
- m_AssetGUID: dbf7710ff9a33440f8390ed8e04dbb77 |
|||
m_SubObjectName: |
|||
--- !u!114 &7861318668517722592 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 6171279960522736352} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: 172515602e62fb746b5d573b38a5fe58, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|||
isGlobal: 1 |
|||
priority: -50 |
|||
blendDistance: 0 |
|||
weight: 1 |
|||
sharedProfile: {fileID: 11400000, guid: ad80f6934bc444a93bb29c1f6db70cf3, type: 2} |
|||
--- !u!114 &3109418422114241795 |
|||
MonoBehaviour: |
|||
m_ObjectHideFlags: 0 |
|||
m_CorrespondingSourceObject: {fileID: 0} |
|||
m_PrefabInstance: {fileID: 0} |
|||
m_PrefabAsset: {fileID: 0} |
|||
m_GameObject: {fileID: 6171279960522736352} |
|||
m_Enabled: 1 |
|||
m_EditorHideFlags: 0 |
|||
m_Script: {fileID: 11500000, guid: 172515602e62fb746b5d573b38a5fe58, type: 3} |
|||
m_Name: |
|||
m_EditorClassIdentifier: |
|||
isGlobal: 1 |
|||
priority: 50 |
|||
blendDistance: 0 |
|||
weight: 1 |
|||
sharedProfile: {fileID: 11400000, guid: dbf7710ff9a33440f8390ed8e04dbb77, type: 2} |
|
|||
using System.Collections; |
|||
using BoatAttack; |
|||
using UnityEngine; |
|||
using UnityEngine.AddressableAssets; |
|||
using UnityEngine.Rendering; |
|||
using UnityEngine.Rendering.Universal; |
|||
#if UNITY_EDITOR
|
|||
using UnityEditor; |
|||
#endif
|
|||
|
|||
[ExecuteAlways] |
|||
public class DefaultVolume : MonoBehaviour |
|||
{ |
|||
public static DefaultVolume Instance { get; private set; } |
|||
|
|||
public Volume volBaseComponent; |
|||
public Volume volQualityComponent; |
|||
public AssetReference[] qualityVolumes; |
|||
private static bool _loading; |
|||
|
|||
private void Awake() |
|||
{ |
|||
if (volBaseComponent && volQualityComponent) |
|||
{ |
|||
Init(); |
|||
} |
|||
else |
|||
{ |
|||
Utility.SafeDestroy(gameObject); |
|||
} |
|||
} |
|||
|
|||
private void Init() |
|||
{ |
|||
#if UNITY_EDITOR
|
|||
gameObject.hideFlags = HideFlags.HideAndDontSave; |
|||
#else
|
|||
DontDestroyOnLoad(gameObject); |
|||
#endif
|
|||
|
|||
if (Instance != null && Instance != this) |
|||
{ |
|||
if (Debug.isDebugBuild) |
|||
Debug.Log($"Extra Volume Manager cleaned up. GUID:{gameObject.GetInstanceID()}"); |
|||
StopAllCoroutines(); |
|||
Utility.SafeDestroy(gameObject); |
|||
} |
|||
else |
|||
{ |
|||
Instance = this; |
|||
gameObject.name = "[DefaultVolume]"; |
|||
if (Debug.isDebugBuild) |
|||
Debug.Log($"Default Volume is {gameObject.GetInstanceID()}"); |
|||
Utility.QualityLevelChange += UpdateVolume; |
|||
UpdateVolume(0, Utility.GetTrueQualityLevel()); // First time set
|
|||
} |
|||
} |
|||
|
|||
private void OnDestroy() |
|||
{ |
|||
StopAllCoroutines(); |
|||
if (Instance == this) |
|||
Instance = null; |
|||
Utility.QualityLevelChange -= UpdateVolume; |
|||
} |
|||
|
|||
private void UpdateVolume(int level, int realLevel) |
|||
{ |
|||
//Setup Quality Vol if needed
|
|||
if (qualityVolumes?.Length > realLevel && qualityVolumes[realLevel] != null) |
|||
{ |
|||
StartCoroutine(LoadAndApplyQualityVolume(realLevel)); |
|||
} |
|||
else |
|||
{ |
|||
volQualityComponent.sharedProfile = null; |
|||
} |
|||
} |
|||
|
|||
private IEnumerator LoadAndApplyQualityVolume(int index) |
|||
{ |
|||
while (_loading) { yield return null; } |
|||
_loading = true; |
|||
var vol = qualityVolumes[index]; |
|||
if (!vol.OperationHandle.IsValid() || !vol.OperationHandle.IsDone) |
|||
{ |
|||
qualityVolumes[index].LoadAssetAsync<VolumeProfile>(); |
|||
} |
|||
yield return vol.OperationHandle; |
|||
volQualityComponent.sharedProfile = vol.OperationHandle.Result as VolumeProfile; |
|||
_loading = false; |
|||
|
|||
if (!Debug.isDebugBuild) yield break; |
|||
Debug.Log(message: "Updated volumes:\n" + |
|||
$" Base Volume : {(volBaseComponent.sharedProfile ? volBaseComponent.sharedProfile.name : "none")}\n" + |
|||
$" Quality Volume : {(volQualityComponent.sharedProfile ? volQualityComponent.sharedProfile.name : "none")}\n" + |
|||
"Total Volume Stack is now:\n"); |
|||
} |
|||
|
|||
#if UNITY_EDITOR
|
|||
[InitializeOnLoadMethod] |
|||
#else
|
|||
[RuntimeInitializeOnLoadMethod] |
|||
#endif
|
|||
private static void LoadMe() |
|||
{ |
|||
if (!(Resources.FindObjectsOfTypeAll(typeof(DefaultVolume)) is DefaultVolume[] vols)) return; |
|||
|
|||
if (vols.Length != 0 || Instance != null) |
|||
{ |
|||
foreach (var vol in vols) |
|||
{ |
|||
if (vol.gameObject.activeSelf && vol.gameObject.activeInHierarchy) vol.Init(); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
Addressables.InstantiateAsync("volume_manager"); |
|||
} |
|||
} |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue