111 lines
2.9 KiB
C#
111 lines
2.9 KiB
C#
using UnityEngine;
|
|
|
|
namespace FloodSWE.Debugging
|
|
{
|
|
[ExecuteAlways]
|
|
public sealed class SweHeightmapPreview : MonoBehaviour
|
|
{
|
|
[Header("Source")]
|
|
public SweTileSimulator simulator;
|
|
public RenderTexture overrideTexture;
|
|
|
|
[Header("Target")]
|
|
public Renderer targetRenderer;
|
|
public bool createQuadIfMissing = true;
|
|
public Vector2 quadSize = new Vector2(10.0f, 10.0f);
|
|
|
|
[Header("Display")]
|
|
[Range(0.0f, 5.0f)]
|
|
public float intensity = 1.0f;
|
|
public float minValue = 0.0f;
|
|
public float maxValue = 1.0f;
|
|
|
|
[Header("Material")]
|
|
public Shader previewShader;
|
|
|
|
private Material runtimeMaterial;
|
|
|
|
private void OnEnable()
|
|
{
|
|
EnsureTarget();
|
|
EnsureMaterial();
|
|
ApplyTexture();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (runtimeMaterial != null)
|
|
{
|
|
DestroyImmediate(runtimeMaterial);
|
|
runtimeMaterial = null;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
ApplyTexture();
|
|
}
|
|
|
|
private void EnsureTarget()
|
|
{
|
|
if (targetRenderer != null || !createQuadIfMissing)
|
|
{
|
|
return;
|
|
}
|
|
|
|
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
|
|
quad.name = "SWE_HeightmapPreview";
|
|
quad.transform.SetParent(transform, false);
|
|
quad.transform.localRotation = Quaternion.Euler(90f, 0f, 0f);
|
|
quad.transform.localScale = new Vector3(quadSize.x, quadSize.y, 1f);
|
|
|
|
targetRenderer = quad.GetComponent<Renderer>();
|
|
}
|
|
|
|
private void EnsureMaterial()
|
|
{
|
|
if (targetRenderer == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Shader shaderToUse = previewShader != null
|
|
? previewShader
|
|
: Shader.Find("FloodSWE/HeightmapPreview");
|
|
|
|
if (shaderToUse == null)
|
|
{
|
|
Debug.LogWarning("SweHeightmapPreview: preview shader not found.");
|
|
return;
|
|
}
|
|
|
|
runtimeMaterial = new Material(shaderToUse);
|
|
targetRenderer.sharedMaterial = runtimeMaterial;
|
|
}
|
|
|
|
private void ApplyTexture()
|
|
{
|
|
if (runtimeMaterial == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
RenderTexture tex = overrideTexture;
|
|
if (tex == null && simulator != null)
|
|
{
|
|
tex = simulator.debugWater;
|
|
}
|
|
|
|
if (tex == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
runtimeMaterial.SetTexture("_MainTex", tex);
|
|
runtimeMaterial.SetFloat("_Intensity", intensity);
|
|
runtimeMaterial.SetFloat("_MinValue", minValue);
|
|
runtimeMaterial.SetFloat("_MaxValue", maxValue);
|
|
}
|
|
}
|
|
}
|