Files
3d-bxqz/Packages/com.waveharmonic.crest.shallow-water/Editor/Scripts/Previews.cs
zhangjiajia 81ffaaeca6 'push'
2026-05-06 16:56:59 +08:00

74 lines
2.7 KiB
C#

// Crest Water System
// Copyright © 2024 Wave Harmonic. All rights reserved.
using UnityEditor;
using UnityEngine;
using UnityEngine.Rendering;
using WaveHarmonic.Crest.Editor;
namespace WaveHarmonic.Crest.ShallowWater.Editor
{
[CustomPreview(typeof(ShallowWaterSimulation))]
sealed class Preview : TexturePreview
{
RenderTexture _TemporaryRenderTexture;
public override GUIContent GetPreviewTitle() => new("Shallow Water Simulation");
protected override Texture OriginalTexture
{
get
{
if (_TemporaryRenderTexture != null) return _TemporaryRenderTexture;
// Return on of the textures to prove initialization.
var target = this.target as ShallowWaterSimulation;
return target._GroundHeight;
}
}
public override void OnPreviewSettings()
{
base.OnPreviewSettings();
// OnPreviewSettings is called after OnPreviewGUI so release here.
RenderTexture.ReleaseTemporary(_TemporaryRenderTexture);
_TemporaryRenderTexture = null;
}
public override void Cleanup()
{
base.Cleanup();
RenderTexture.ReleaseTemporary(_TemporaryRenderTexture);
}
public override void OnPreviewGUI(Rect rect, GUIStyle background)
{
var target = this.target as ShallowWaterSimulation;
var descriptor = target._GroundHeight.descriptor;
descriptor.dimension = TextureDimension.Tex2DArray;
descriptor.volumeDepth = target._WaterLevel != null ? 6 : 5;
descriptor.useMipMap = false;
// Copy textures to a single texture array.
_TemporaryRenderTexture = RenderTexture.GetTemporary(descriptor);
Graphics.CopyTexture(target._GroundHeight, 0, _TemporaryRenderTexture, 0);
Graphics.CopyTexture(target._Height1, 0, _TemporaryRenderTexture, 1);
Graphics.CopyTexture(target._VelocityX, 0, 0, _TemporaryRenderTexture, 2, 0);
Graphics.CopyTexture(target._VelocityY, 0, 0, _TemporaryRenderTexture, 3, 0);
Graphics.CopyTexture(target._Mask, 0, _TemporaryRenderTexture, 4);
if (target._WaterLevel != null) Graphics.CopyTexture(target._WaterLevel, 0, _TemporaryRenderTexture, 5);
_TemporaryRenderTexture.name = _Slice switch
{
0 => "Ground Height",
1 => "Height",
2 => "Velocity X",
3 => "Velocity Y",
4 => "Mask",
5 => "Water Level",
_ => throw new System.NotImplementedException(),
};
base.OnPreviewGUI(rect, background);
}
}
}