Files
2026-02-21 16:45:37 +08:00

136 lines
4.4 KiB
C#

using UnityEngine;
namespace UnityStandardAssets.ImageEffects
{
[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Image Effects/Rendering/Sun Shafts")]
public class SunShafts : PostEffectsBase
{
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);
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>().hdr) ? RenderTextureFormat.Default : RenderTextureFormat.DefaultHDR);
RenderTexture renderTexture = (RenderTexture.active = RenderTexture.GetTemporary(source.width, source.height, 0, format));
GL.ClearWithSkybox(false, GetComponent<Camera>());
sunShaftsMaterial.SetTexture("_Skybox", renderTexture);
Graphics.Blit(source, temporary, sunShaftsMaterial, 3);
RenderTexture.ReleaseTemporary(renderTexture);
}
else
{
Graphics.Blit(source, temporary, sunShaftsMaterial, 2);
}
DrawBorder(temporary, simpleClearMaterial);
radialBlurIterations = Mathf.Clamp(radialBlurIterations, 1, 4);
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 temporary3 = RenderTexture.GetTemporary(width, height, 0);
Graphics.Blit(temporary, temporary3, sunShaftsMaterial, 1);
RenderTexture.ReleaseTemporary(temporary);
num2 = sunShaftBlurRadius * (((float)i * 2f + 1f) * 6f) / 768f;
sunShaftsMaterial.SetVector("_BlurRadius4", new Vector4(num2, num2, 0f, 0f));
temporary = RenderTexture.GetTemporary(width, height, 0);
Graphics.Blit(temporary3, temporary, sunShaftsMaterial, 1);
RenderTexture.ReleaseTemporary(temporary3);
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(temporary);
}
}
}