Integrate CultMesh UI and URP cleanup
This commit is contained in:
parent
932cc9f906
commit
4d7bb65868
131 changed files with 7209 additions and 5084 deletions
|
|
@ -11,6 +11,7 @@ using UnityEngine.Rendering;
|
|||
using UnityEngine.Serialization;
|
||||
using static Unity.Mathematics.math;
|
||||
using static Unity.Mathematics.noise;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// Drives the volume render.
|
||||
|
|
@ -19,6 +20,7 @@ using static Unity.Mathematics.noise;
|
|||
[RequireComponent( typeof( Camera ) )]
|
||||
public class VolumeSampling : MonoBehaviour
|
||||
{
|
||||
private static readonly List<VolumeSampling> ActiveSamplers = new List<VolumeSampling>();
|
||||
public Transform GridMesh;
|
||||
public Camera GridCamera;
|
||||
public GameSettings Settings;
|
||||
|
|
@ -29,16 +31,58 @@ public class VolumeSampling : MonoBehaviour
|
|||
public RenderTexture NebulaTint;
|
||||
public AetheriaUnityRenderSplatViewportSource SplatViewportSource;
|
||||
public AetheriaRenderSplatLayerRenderer SplatLayerRenderer;
|
||||
public AetheriaSceneRenderSplatSource SceneSplatSource;
|
||||
|
||||
[SerializeField]
|
||||
private bool useRuntimeRenderSplats;
|
||||
|
||||
[SerializeField]
|
||||
private bool renderSplatsEveryFrame;
|
||||
|
||||
[SerializeField]
|
||||
private float splatRenderIntervalSeconds = 0.25f;
|
||||
|
||||
private MeshRenderer _gridMeshRenderer;
|
||||
private float _flowScroll;
|
||||
private Transform _gridTransform;
|
||||
private Camera _ownerCamera;
|
||||
private Vector2 _lastSplatCenter;
|
||||
private Vector2 _lastSplatSize;
|
||||
private Vector2 _publishedSplatCenter;
|
||||
private Vector2 _publishedSplatSize;
|
||||
private float _nextSplatRenderTime;
|
||||
private bool _hasRenderedSplats;
|
||||
private GlobalKeyword _keywordFlowGlobal;
|
||||
private GlobalKeyword _keywordFlowSlope;
|
||||
private GlobalKeyword _keywordNoiseSlope;
|
||||
|
||||
public static void RenderForCamera(Camera camera)
|
||||
{
|
||||
if (camera == null)
|
||||
return;
|
||||
|
||||
for (var i = 0; i < ActiveSamplers.Count; i++)
|
||||
{
|
||||
var sampler = ActiveSamplers[i];
|
||||
if (sampler != null && sampler.TargetsCamera(camera))
|
||||
sampler.RenderSamplingPass();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (!ActiveSamplers.Contains(this))
|
||||
ActiveSamplers.Add(this);
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
ActiveSamplers.Remove(this);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_ownerCamera = GetComponent<Camera>();
|
||||
_keywordFlowGlobal = GlobalKeyword.Create("FLOW_GLOBAL");
|
||||
_keywordFlowSlope = GlobalKeyword.Create("FLOW_SLOPE");
|
||||
_keywordNoiseSlope = GlobalKeyword.Create("NOISE_SLOPE");
|
||||
|
|
@ -53,12 +97,28 @@ public class VolumeSampling : MonoBehaviour
|
|||
{
|
||||
if(_gridTransform==null)
|
||||
_gridTransform = GridCamera?.transform;
|
||||
// Shader needs to know the position and scale of cameras used to render input textures
|
||||
if(GridCamera != null)
|
||||
Shader.SetGlobalVector("_GridTransform", new Vector4(_gridTransform.position.x,_gridTransform.position.z,GridCamera.orthographicSize*2));
|
||||
|
||||
RenderSplatTextures();
|
||||
_flowScroll += Settings.DefaultEnvironment.Flow.GlobalScrollSpeed * Time.deltaTime;
|
||||
UpdateGridMeshBounds();
|
||||
}
|
||||
|
||||
private bool TargetsCamera(Camera camera)
|
||||
{
|
||||
_ownerCamera ??= GetComponent<Camera>();
|
||||
return camera == _ownerCamera;
|
||||
}
|
||||
|
||||
private void RenderSamplingPass()
|
||||
{
|
||||
if (_gridTransform == null)
|
||||
_gridTransform = GridCamera?.transform;
|
||||
|
||||
RenderSplatTexturesIfNeeded();
|
||||
PublishSamplingGlobals();
|
||||
}
|
||||
|
||||
private void UpdateGridMeshBounds()
|
||||
{
|
||||
var environment = Settings.DefaultEnvironment;
|
||||
|
||||
if (GridMesh != null)
|
||||
|
|
@ -70,7 +130,19 @@ public class VolumeSampling : MonoBehaviour
|
|||
// GridMesh.position = new Vector3(_gridTransform.position.x, environment.Grid.Offset, _gridTransform.position.z);
|
||||
// GridMesh.localScale = new Vector3(GridCamera.orthographicSize, 1, GridCamera.orthographicSize);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void PublishSamplingGlobals()
|
||||
{
|
||||
var environment = Settings.DefaultEnvironment;
|
||||
|
||||
ResolveSplatViewport(out var center, out var size);
|
||||
_publishedSplatCenter = center;
|
||||
_publishedSplatSize = size;
|
||||
|
||||
// Shader sampling must use the same world-space viewport that produced the splat textures.
|
||||
Shader.SetGlobalVector("_GridTransform", new Vector4(center.x, center.y, size.x));
|
||||
|
||||
// Volumetric sampling parameters are used by several shaders
|
||||
Shader.SetGlobalTexture("_NebulaSurfaceHeight", NebulaSurfaceHeight);
|
||||
Shader.SetGlobalTexture("_NebulaPatchHeight", NebulaPatchHeight);
|
||||
|
|
@ -105,7 +177,6 @@ public class VolumeSampling : MonoBehaviour
|
|||
Shader.SetGlobalFloat("_FlowAmplitude", environment.Flow.GlobalAmplitude);
|
||||
Shader.SetGlobalFloat("_FlowSlopeAmplitude", environment.Flow.SlopeAmplitude);
|
||||
Shader.SetGlobalFloat("_FlowSwirlAmplitude", environment.Flow.SwirlAmplitude);
|
||||
_flowScroll += environment.Flow.GlobalScrollSpeed * Time.deltaTime;
|
||||
Shader.SetGlobalFloat("_FlowScroll", _flowScroll);
|
||||
Shader.SetGlobalFloat("_FlowPeriod", environment.Flow.Period);
|
||||
|
||||
|
|
@ -114,20 +185,33 @@ public class VolumeSampling : MonoBehaviour
|
|||
Shader.SetKeyword(_keywordNoiseSlope, environment.Noise.SlopeExponent != 0);
|
||||
}
|
||||
|
||||
private void RenderSplatTextures()
|
||||
private void RenderSplatTexturesIfNeeded()
|
||||
{
|
||||
ResolveSplatRenderers();
|
||||
if (SplatViewportSource == null)
|
||||
if (SplatLayerRenderer == null)
|
||||
return;
|
||||
|
||||
var size = GridCamera != null
|
||||
? new Vector2(GridCamera.orthographicSize * 2, GridCamera.orthographicSize * 2)
|
||||
: new Vector2(1024, 1024);
|
||||
var center = _gridTransform != null
|
||||
? new Vector2(_gridTransform.position.x, _gridTransform.position.z)
|
||||
: Vector2.zero;
|
||||
SplatViewportSource.SetViewport(center, size);
|
||||
SplatViewportSource.RenderLatest();
|
||||
ResolveSplatViewport(out var center, out var size);
|
||||
var document = ResolveSceneSplatDocument(center, size);
|
||||
if (document == null && useRuntimeRenderSplats)
|
||||
document = ResolveRuntimeSplatDocument(center, size);
|
||||
if (document == null)
|
||||
return;
|
||||
|
||||
var viewportChanged = !_hasRenderedSplats ||
|
||||
!Approximately(center, _lastSplatCenter) ||
|
||||
!Approximately(size, _lastSplatSize);
|
||||
var intervalElapsed = Time.unscaledTime >= _nextSplatRenderTime;
|
||||
if (!renderSplatsEveryFrame && !viewportChanged && !intervalElapsed)
|
||||
return;
|
||||
|
||||
SplatLayerRenderer.Render(document, Mathf.RoundToInt(size.x), Mathf.RoundToInt(size.y));
|
||||
_lastSplatCenter = center;
|
||||
_lastSplatSize = size;
|
||||
_publishedSplatCenter = center;
|
||||
_publishedSplatSize = size;
|
||||
_nextSplatRenderTime = Time.unscaledTime + Mathf.Max(0.02f, splatRenderIntervalSeconds);
|
||||
_hasRenderedSplats = true;
|
||||
|
||||
if (SplatLayerRenderer == null)
|
||||
return;
|
||||
|
|
@ -138,6 +222,52 @@ public class VolumeSampling : MonoBehaviour
|
|||
NebulaTint = ResolveLayerTexture(AetheriaRuntimeRenderSplatLayerKeys.FogTint, NebulaTint);
|
||||
}
|
||||
|
||||
private AetheriaRuntimeRenderSplatsViewportDocument ResolveSceneSplatDocument(Vector2 center, Vector2 size)
|
||||
{
|
||||
if (SceneSplatSource == null)
|
||||
SceneSplatSource = GetComponent<AetheriaSceneRenderSplatSource>();
|
||||
return SceneSplatSource != null
|
||||
? SceneSplatSource.BuildDocument(BuildViewportBounds(center, size))
|
||||
: null;
|
||||
}
|
||||
|
||||
private AetheriaRuntimeRenderSplatsViewportDocument ResolveRuntimeSplatDocument(Vector2 center, Vector2 size)
|
||||
{
|
||||
if (SplatViewportSource == null)
|
||||
return null;
|
||||
|
||||
SplatViewportSource.SetViewport(center, size);
|
||||
return SplatViewportSource.CurrentDocument;
|
||||
}
|
||||
|
||||
private void ResolveSplatViewport(out Vector2 center, out Vector2 size)
|
||||
{
|
||||
size = GridCamera != null
|
||||
? new Vector2(GridCamera.orthographicSize * 2, GridCamera.orthographicSize * 2)
|
||||
: new Vector2(1024, 1024);
|
||||
center = _gridTransform != null
|
||||
? new Vector2(_gridTransform.position.x, _gridTransform.position.z)
|
||||
: Vector2.zero;
|
||||
}
|
||||
|
||||
private static AetheriaRuntimeRtsViewportBounds BuildViewportBounds(Vector2 center, Vector2 size)
|
||||
{
|
||||
var halfSize = size * 0.5f;
|
||||
return new AetheriaRuntimeRtsViewportBounds
|
||||
{
|
||||
MinX = center.x - halfSize.x,
|
||||
MinY = center.y - halfSize.y,
|
||||
MaxX = center.x + halfSize.x,
|
||||
MaxY = center.y + halfSize.y
|
||||
};
|
||||
}
|
||||
|
||||
private static bool Approximately(Vector2 lhs, Vector2 rhs)
|
||||
{
|
||||
return Mathf.Abs(lhs.x - rhs.x) < 0.001f &&
|
||||
Mathf.Abs(lhs.y - rhs.y) < 0.001f;
|
||||
}
|
||||
|
||||
private RenderTexture ResolveLayerTexture(string layerKey, RenderTexture fallback)
|
||||
{
|
||||
return SplatLayerRenderer.TryGetTexture(layerKey, out var texture) && texture != null
|
||||
|
|
@ -152,9 +282,13 @@ public class VolumeSampling : MonoBehaviour
|
|||
if (SplatLayerRenderer == null)
|
||||
SplatLayerRenderer = gameObject.AddComponent<AetheriaRenderSplatLayerRenderer>();
|
||||
|
||||
if (!useRuntimeRenderSplats)
|
||||
return;
|
||||
|
||||
if (SplatViewportSource == null)
|
||||
SplatViewportSource = GetComponent<AetheriaUnityRenderSplatViewportSource>();
|
||||
if (SplatViewportSource == null)
|
||||
SplatViewportSource = gameObject.AddComponent<AetheriaUnityRenderSplatViewportSource>();
|
||||
SplatViewportSource.RenderInLateUpdate = false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue