151 lines
5.0 KiB
C#
151 lines
5.0 KiB
C#
using UnityEngine;
|
|
|
|
namespace Artngame.SKYMASTER
|
|
{
|
|
[ExecuteInEditMode]
|
|
[RequireComponent(typeof(Camera))]
|
|
[AddComponentMenu("Image Effects/Rendering/Sun ShaftsSkyMaster")]
|
|
public class SunShaftsSkyMaster : PostEffectsBaseSkyMaster
|
|
{
|
|
public enum SunShaftsResolution
|
|
{
|
|
Low = 0,
|
|
Normal = 1,
|
|
High = 2
|
|
}
|
|
|
|
public enum ShaftsScreenBlendMode
|
|
{
|
|
Screen = 0,
|
|
Add = 1
|
|
}
|
|
|
|
public SunShaftsResolution resolution = SunShaftsResolution.Normal;
|
|
|
|
public ShaftsScreenBlendMode screenBlendMode;
|
|
|
|
public Transform sunTransform;
|
|
|
|
public int radialBlurIterations = 2;
|
|
|
|
public Color sunColor = Color.white;
|
|
|
|
public Color sunThreshold = new Color(0.87f, 0.74f, 0.65f);
|
|
|
|
public float sunShaftBlurRadius = 2.5f;
|
|
|
|
public float sunShaftIntensity = 1.15f;
|
|
|
|
public float maxRadius = 0.75f;
|
|
|
|
public bool useDepthTexture = true;
|
|
|
|
public Shader sunShaftsShader;
|
|
|
|
private Material sunShaftsMaterial;
|
|
|
|
public Shader simpleClearShader;
|
|
|
|
private Material simpleClearMaterial;
|
|
|
|
public override bool CheckResources()
|
|
{
|
|
CheckSupport(useDepthTexture);
|
|
sunShaftsMaterial = CheckShaderAndCreateMaterial(sunShaftsShader, sunShaftsMaterial);
|
|
simpleClearMaterial = CheckShaderAndCreateMaterial(simpleClearShader, simpleClearMaterial);
|
|
if (!isSupported)
|
|
{
|
|
ReportAutoDisable();
|
|
}
|
|
return isSupported;
|
|
}
|
|
|
|
private void OnRenderImage(RenderTexture source, RenderTexture destination)
|
|
{
|
|
if (!CheckResources())
|
|
{
|
|
Graphics.Blit(source, destination);
|
|
return;
|
|
}
|
|
if (useDepthTexture)
|
|
{
|
|
GetComponent<Camera>().depthTextureMode |= DepthTextureMode.Depth;
|
|
}
|
|
int num = 4;
|
|
if (resolution == SunShaftsResolution.Normal)
|
|
{
|
|
num = 2;
|
|
}
|
|
else if (resolution == SunShaftsResolution.High)
|
|
{
|
|
num = 1;
|
|
}
|
|
Vector3 vector = Vector3.one * 0.5f;
|
|
vector = ((!sunTransform) ? new Vector3(0.5f, 0.5f, 0f) : GetComponent<Camera>().WorldToViewportPoint(sunTransform.position));
|
|
int width = source.width / num;
|
|
int height = source.height / num;
|
|
RenderTexture temporary = RenderTexture.GetTemporary(width, height, 0);
|
|
RenderTexture temporary2 = RenderTexture.GetTemporary(width, height, 0);
|
|
sunShaftsMaterial.SetVector("_BlurRadius4", new Vector4(1f, 1f, 0f, 0f) * sunShaftBlurRadius);
|
|
sunShaftsMaterial.SetVector("_SunPosition", new Vector4(vector.x, vector.y, vector.z, maxRadius));
|
|
sunShaftsMaterial.SetVector("_SunThreshold", sunThreshold);
|
|
if (!useDepthTexture)
|
|
{
|
|
RenderTextureFormat format = (GetComponent<Camera>().allowHDR ? RenderTextureFormat.DefaultHDR : RenderTextureFormat.Default);
|
|
RenderTexture renderTexture = (RenderTexture.active = RenderTexture.GetTemporary(source.width, source.height, 0, format));
|
|
GL.ClearWithSkybox(clearDepth: false, GetComponent<Camera>());
|
|
sunShaftsMaterial.SetTexture("_Skybox", renderTexture);
|
|
Graphics.Blit(source, temporary2, sunShaftsMaterial, 3);
|
|
RenderTexture.ReleaseTemporary(renderTexture);
|
|
}
|
|
else
|
|
{
|
|
Graphics.Blit(source, temporary2, sunShaftsMaterial, 2);
|
|
}
|
|
Graphics.Blit(temporary2, temporary);
|
|
DrawBorder(temporary, simpleClearMaterial);
|
|
radialBlurIterations = Mathf.Clamp(radialBlurIterations, 1, 8);
|
|
float num2 = sunShaftBlurRadius * 0.0013020834f;
|
|
sunShaftsMaterial.SetVector("_BlurRadius4", new Vector4(num2, num2, 0f, 0f));
|
|
sunShaftsMaterial.SetVector("_SunPosition", new Vector4(vector.x, vector.y, vector.z, maxRadius));
|
|
for (int i = 0; i < radialBlurIterations; i++)
|
|
{
|
|
RenderTexture temporary4 = RenderTexture.GetTemporary(width, height, 0);
|
|
Graphics.Blit(temporary, temporary4, sunShaftsMaterial, 1);
|
|
if ((bool)temporary)
|
|
{
|
|
RenderTexture.ReleaseTemporary(temporary);
|
|
}
|
|
if ((bool)temporary2)
|
|
{
|
|
RenderTexture.ReleaseTemporary(temporary2);
|
|
}
|
|
num2 = sunShaftBlurRadius * (((float)i * 2f + 1f) * 6f) / 768f;
|
|
sunShaftsMaterial.SetVector("_BlurRadius4", new Vector4(num2, num2, 0f, 0f));
|
|
RenderTexture temporary5 = RenderTexture.GetTemporary(width, height, 0);
|
|
Graphics.Blit(temporary4, temporary5);
|
|
temporary2 = RenderTexture.GetTemporary(width, height, 0);
|
|
Graphics.Blit(temporary5, temporary4, sunShaftsMaterial, 1);
|
|
temporary = RenderTexture.GetTemporary(width, height, 0);
|
|
Graphics.Blit(temporary4, temporary);
|
|
RenderTexture.ReleaseTemporary(temporary5);
|
|
RenderTexture.ReleaseTemporary(temporary4);
|
|
num2 = sunShaftBlurRadius * (((float)i * 2f + 2f) * 6f) / 768f;
|
|
sunShaftsMaterial.SetVector("_BlurRadius4", new Vector4(num2, num2, 0f, 0f));
|
|
}
|
|
if (vector.z >= 0f)
|
|
{
|
|
sunShaftsMaterial.SetVector("_SunColor", new Vector4(sunColor.r, sunColor.g, sunColor.b, sunColor.a) * sunShaftIntensity);
|
|
}
|
|
else
|
|
{
|
|
sunShaftsMaterial.SetVector("_SunColor", Vector4.zero);
|
|
}
|
|
sunShaftsMaterial.SetTexture("_ColorBuffer", temporary);
|
|
Graphics.Blit(source, destination, sunShaftsMaterial, (screenBlendMode != ShaftsScreenBlendMode.Screen) ? 4 : 0);
|
|
RenderTexture.ReleaseTemporary(temporary2);
|
|
RenderTexture.ReleaseTemporary(temporary);
|
|
}
|
|
}
|
|
}
|