153 lines
5.5 KiB
C#
153 lines
5.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.Rendering;
|
|
using UnityEngine.Rendering.Universal;
|
|
|
|
public class BrokenGlassRenderFeature : ScriptableRendererFeature
|
|
{
|
|
class BrokenGlassPass : ScriptableRenderPass
|
|
{
|
|
static readonly string ProfilerTag = "Broken Glass Effect";
|
|
Material material;
|
|
RenderTargetIdentifier source;
|
|
RenderTargetHandle tempTexture;
|
|
|
|
public float Value = 1f;
|
|
public float Bullet_1 = 1f;
|
|
public float Bullet_2 = 1f;
|
|
public float Bullet_3 = 1f;
|
|
public float Bullet_4 = 1f;
|
|
public float Bullet_5 = 1f;
|
|
public float Bullet_6 = 1f;
|
|
public float Bullet_7 = 1f;
|
|
public float Bullet_8 = 1f;
|
|
public float Bullet_9 = 1f;
|
|
public float Bullet_10 = 1f;
|
|
public float Bullet_11 = 1f;
|
|
public float Bullet_12 = 1f;
|
|
public Texture2D GlassTexture;
|
|
|
|
public BrokenGlassPass(Material mat)
|
|
{
|
|
material = mat;
|
|
tempTexture.Init("_TempBrokenGlassTex");
|
|
}
|
|
|
|
public void Setup(RenderTargetIdentifier src)
|
|
{
|
|
source = src;
|
|
}
|
|
|
|
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
|
|
{
|
|
if (material == null)
|
|
{
|
|
Debug.LogError("BrokenGlassPass: Material is null!");
|
|
return;
|
|
}
|
|
|
|
if (GlassTexture == null)
|
|
{
|
|
Debug.LogError("BrokenGlassPass: GlassTexture is null!");
|
|
return;
|
|
}
|
|
|
|
CommandBuffer cmd = CommandBufferPool.Get(ProfilerTag);
|
|
|
|
source = renderingData.cameraData.renderer.cameraColorTarget;
|
|
|
|
RenderTextureDescriptor opaqueDesc = renderingData.cameraData.cameraTargetDescriptor;
|
|
opaqueDesc.depthBufferBits = 0;
|
|
|
|
cmd.GetTemporaryRT(tempTexture.id, opaqueDesc);
|
|
cmd.SetGlobalTexture("_MainTex2", GlassTexture);
|
|
cmd.SetGlobalFloat("_Value", Value);
|
|
cmd.SetGlobalFloat("_Bullet_1", Bullet_1);
|
|
cmd.SetGlobalFloat("_Bullet_2", Bullet_2);
|
|
cmd.SetGlobalFloat("_Bullet_3", Bullet_3);
|
|
cmd.SetGlobalFloat("_Bullet_4", Bullet_4);
|
|
cmd.SetGlobalFloat("_Bullet_5", Bullet_5);
|
|
cmd.SetGlobalFloat("_Bullet_6", Bullet_6);
|
|
cmd.SetGlobalFloat("_Bullet_7", Bullet_7);
|
|
cmd.SetGlobalFloat("_Bullet_8", Bullet_8);
|
|
cmd.SetGlobalFloat("_Bullet_9", Bullet_9);
|
|
cmd.SetGlobalFloat("_Bullet_10", Bullet_10);
|
|
cmd.SetGlobalFloat("_Bullet_11", Bullet_11);
|
|
cmd.SetGlobalFloat("_Bullet_12", Bullet_12);
|
|
|
|
Debug.Log($"BrokenGlassPass Parameters - Value: {Value}, Bullet_1: {Bullet_1}, Bullet_2: {Bullet_2}, Bullet_3: {Bullet_3}, Bullet_4: {Bullet_4}, Bullet_5: {Bullet_5}, Bullet_6: {Bullet_6}, Bullet_7: {Bullet_7}, Bullet_8: {Bullet_8}, Bullet_9: {Bullet_9}, Bullet_10: {Bullet_10}, Bullet_11: {Bullet_11}, Bullet_12: {Bullet_12}");
|
|
|
|
Blit(cmd, source, tempTexture.Identifier(), material, 0);
|
|
Blit(cmd, tempTexture.Identifier(), source);
|
|
|
|
context.ExecuteCommandBuffer(cmd);
|
|
CommandBufferPool.Release(cmd);
|
|
}
|
|
|
|
public override void FrameCleanup(CommandBuffer cmd)
|
|
{
|
|
if (tempTexture != RenderTargetHandle.CameraTarget)
|
|
cmd.ReleaseTemporaryRT(tempTexture.id);
|
|
}
|
|
}
|
|
|
|
[System.Serializable]
|
|
public class BrokenGlassSettings
|
|
{
|
|
public Material Material;
|
|
public Texture2D GlassTexture;
|
|
[Range(0f, 2f)] public float Value = 1f;
|
|
[Range(0f, 2f)] public float Bullet_1 = 1f;
|
|
[Range(0f, 2f)] public float Bullet_2 = 1f;
|
|
[Range(0f, 2f)] public float Bullet_3 = 1f;
|
|
[Range(0f, 2f)] public float Bullet_4 = 1f;
|
|
[Range(0f, 2f)] public float Bullet_5 = 1f;
|
|
[Range(0f, 2f)] public float Bullet_6 = 1f;
|
|
[Range(0f, 2f)] public float Bullet_7 = 1f;
|
|
[Range(0f, 2f)] public float Bullet_8 = 1f;
|
|
[Range(0f, 2f)] public float Bullet_9 = 1f;
|
|
[Range(0f, 2f)] public float Bullet_10 = 1f;
|
|
[Range(0f, 2f)] public float Bullet_11 = 1f;
|
|
[Range(0f, 2f)] public float Bullet_12 = 1f;
|
|
public RenderPassEvent RenderPassEvent = RenderPassEvent.AfterRenderingTransparents;
|
|
}
|
|
|
|
public BrokenGlassSettings settings = new BrokenGlassSettings();
|
|
BrokenGlassPass pass;
|
|
|
|
public override void Create()
|
|
{
|
|
if (settings.Material == null)
|
|
return;
|
|
|
|
pass = new BrokenGlassPass(settings.Material)
|
|
{
|
|
Value = settings.Value,
|
|
Bullet_1 = settings.Bullet_1,
|
|
Bullet_2 = settings.Bullet_2,
|
|
Bullet_3 = settings.Bullet_3,
|
|
Bullet_4 = settings.Bullet_4,
|
|
Bullet_5 = settings.Bullet_5,
|
|
Bullet_6 = settings.Bullet_6,
|
|
Bullet_7 = settings.Bullet_7,
|
|
Bullet_8 = settings.Bullet_8,
|
|
Bullet_9 = settings.Bullet_9,
|
|
Bullet_10 = settings.Bullet_10,
|
|
Bullet_11 = settings.Bullet_11,
|
|
Bullet_12 = settings.Bullet_12,
|
|
GlassTexture = settings.GlassTexture,
|
|
renderPassEvent = settings.RenderPassEvent
|
|
};
|
|
|
|
Debug.Log("BrokenGlassRenderFeature Created");
|
|
}
|
|
|
|
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
|
|
{
|
|
if (settings.Material != null)
|
|
{
|
|
renderer.EnqueuePass(pass);
|
|
}
|
|
Debug.Log($"Material is null: {settings.Material == null}");
|
|
}
|
|
}
|