246 lines
7.5 KiB
C#
246 lines
7.5 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace Artngame.SKYMASTER
|
|
{
|
|
[ExecuteInEditMode]
|
|
[RequireComponent(typeof(Camera))]
|
|
[AddComponentMenu("Image Effects/Rendering/Global Fog Sky Master")]
|
|
public class GlobalFogSkyMaster : PostEffectsBaseSkyMaster
|
|
{
|
|
[Tooltip("Apply distance-based fog?")]
|
|
public bool distanceFog = true;
|
|
|
|
[Tooltip("Distance fog is based on radial distance from camera when checked")]
|
|
public bool useRadialDistance;
|
|
|
|
[Tooltip("Apply height-based fog?")]
|
|
public bool heightFog = true;
|
|
|
|
[Tooltip("Fog top Y coordinate")]
|
|
public float height = 1f;
|
|
|
|
[Range(1E-05f, 10f)]
|
|
public float heightDensity = 2f;
|
|
|
|
[Tooltip("Push fog away from the camera by this amount")]
|
|
public float startDistance;
|
|
|
|
public Gradient DistGradient = new Gradient();
|
|
|
|
public Vector2 GradientBounds = Vector2.zero;
|
|
|
|
public float luminance = 0.8f;
|
|
|
|
public float lumFac = 0.9f;
|
|
|
|
public float ScatterFac = 24.4f;
|
|
|
|
public float TurbFac = 324.74f;
|
|
|
|
public float HorizFac = 1f;
|
|
|
|
public float turbidity = 10f;
|
|
|
|
public float reileigh = 0.8f;
|
|
|
|
public float mieCoefficient = 0.1f;
|
|
|
|
public float mieDirectionalG = 0.7f;
|
|
|
|
public float bias = 0.6f;
|
|
|
|
public float contrast = 4.12f;
|
|
|
|
public Transform Sun;
|
|
|
|
public bool FogSky;
|
|
|
|
public Vector3 TintColor = new Vector3(68f, 155f, 345f);
|
|
|
|
public float ClearSkyFac = 1f;
|
|
|
|
public Shader fogShader;
|
|
|
|
private Material fogMaterial;
|
|
|
|
public SkyMasterManager SkyManager;
|
|
|
|
private Texture2D colourPalette;
|
|
|
|
private bool Made_texture;
|
|
|
|
public int gradientResolution = 256;
|
|
|
|
private Color[] colourArray;
|
|
|
|
private void Update()
|
|
{
|
|
if (colourArray == null)
|
|
{
|
|
colourArray = new Color[gradientResolution];
|
|
}
|
|
for (int i = 0; i < colourArray.Length; i++)
|
|
{
|
|
colourArray[i] = DistGradient.Evaluate((float)i / (float)colourArray.Length);
|
|
}
|
|
if (!Made_texture)
|
|
{
|
|
colourPalette = new Texture2D(gradientResolution, 10, TextureFormat.ARGB32, mipChain: false);
|
|
colourPalette.filterMode = FilterMode.Point;
|
|
colourPalette.wrapMode = TextureWrapMode.Clamp;
|
|
Made_texture = true;
|
|
}
|
|
for (int j = 0; j < gradientResolution; j++)
|
|
{
|
|
for (int k = 0; k < 10; k++)
|
|
{
|
|
colourPalette.SetPixel(j, k, colourArray[j]);
|
|
}
|
|
}
|
|
colourPalette.Apply();
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
colourArray = new Color[gradientResolution];
|
|
for (int i = 0; i < colourArray.Length; i++)
|
|
{
|
|
colourArray[i] = DistGradient.Evaluate((float)i / (float)colourArray.Length);
|
|
}
|
|
if (!Made_texture)
|
|
{
|
|
colourPalette = new Texture2D(gradientResolution, 10, TextureFormat.ARGB32, mipChain: false);
|
|
colourPalette.filterMode = FilterMode.Point;
|
|
colourPalette.wrapMode = TextureWrapMode.Clamp;
|
|
Made_texture = true;
|
|
}
|
|
for (int j = 0; j < gradientResolution; j++)
|
|
{
|
|
for (int k = 0; k < 10; k++)
|
|
{
|
|
colourPalette.SetPixel(j, k, colourArray[j]);
|
|
}
|
|
}
|
|
colourPalette.Apply();
|
|
}
|
|
|
|
public override bool CheckResources()
|
|
{
|
|
CheckSupport(needDepth: true);
|
|
fogMaterial = CheckShaderAndCreateMaterial(fogShader, fogMaterial);
|
|
if (!isSupported)
|
|
{
|
|
ReportAutoDisable();
|
|
}
|
|
return isSupported;
|
|
}
|
|
|
|
[ImageEffectOpaque]
|
|
private void OnRenderImage(RenderTexture source, RenderTexture destination)
|
|
{
|
|
if (!CheckResources() || (!distanceFog && !heightFog))
|
|
{
|
|
Graphics.Blit(source, destination);
|
|
return;
|
|
}
|
|
Camera component = GetComponent<Camera>();
|
|
Transform transform = component.transform;
|
|
float nearClipPlane = component.nearClipPlane;
|
|
float farClipPlane = component.farClipPlane;
|
|
float fieldOfView = component.fieldOfView;
|
|
float aspect = component.aspect;
|
|
Matrix4x4 identity = Matrix4x4.identity;
|
|
float num = fieldOfView * 0.5f;
|
|
Vector3 vector = transform.right * nearClipPlane * Mathf.Tan(num * (MathF.PI / 180f)) * aspect;
|
|
Vector3 vector2 = transform.up * nearClipPlane * Mathf.Tan(num * (MathF.PI / 180f));
|
|
Vector3 vector3 = transform.forward * nearClipPlane - vector + vector2;
|
|
float num2 = vector3.magnitude * farClipPlane / nearClipPlane;
|
|
vector3.Normalize();
|
|
vector3 *= num2;
|
|
Vector3 vector4 = transform.forward * nearClipPlane + vector + vector2;
|
|
vector4.Normalize();
|
|
vector4 *= num2;
|
|
Vector3 vector5 = transform.forward * nearClipPlane + vector - vector2;
|
|
vector5.Normalize();
|
|
vector5 *= num2;
|
|
Vector3 vector6 = transform.forward * nearClipPlane - vector - vector2;
|
|
vector6.Normalize();
|
|
vector6 *= num2;
|
|
identity.SetRow(0, vector3);
|
|
identity.SetRow(1, vector4);
|
|
identity.SetRow(2, vector5);
|
|
identity.SetRow(3, vector6);
|
|
Vector3 position = transform.position;
|
|
float num3 = position.y - height;
|
|
float z = ((num3 <= 0f) ? 1f : 0f);
|
|
fogMaterial.SetMatrix("_FrustumCornersWS", identity);
|
|
fogMaterial.SetVector("_CameraWS", position);
|
|
fogMaterial.SetVector("_HeightParams", new Vector4(height, num3, z, heightDensity * 0.5f));
|
|
fogMaterial.SetVector("_DistanceParams", new Vector4(0f - Mathf.Max(startDistance, 0f), 0f, 0f, 0f));
|
|
fogMaterial.SetFloat("luminance", luminance);
|
|
fogMaterial.SetFloat("lumFac", lumFac);
|
|
fogMaterial.SetFloat("Multiplier1", ScatterFac);
|
|
fogMaterial.SetFloat("Multiplier2", TurbFac);
|
|
fogMaterial.SetFloat("Multiplier3", HorizFac);
|
|
fogMaterial.SetFloat("turbidity", turbidity);
|
|
fogMaterial.SetFloat("reileigh", reileigh);
|
|
fogMaterial.SetFloat("mieCoefficient", mieCoefficient);
|
|
fogMaterial.SetFloat("mieDirectionalG", mieDirectionalG);
|
|
fogMaterial.SetFloat("bias", bias);
|
|
fogMaterial.SetFloat("contrast", contrast);
|
|
fogMaterial.SetVector("v3LightDir", -Sun.forward);
|
|
fogMaterial.SetVector("_TintColor", new Vector4(TintColor.x, TintColor.y, TintColor.z, 1f));
|
|
float value = 0f;
|
|
if (FogSky)
|
|
{
|
|
value = 1f;
|
|
}
|
|
fogMaterial.SetFloat("FogSky", value);
|
|
fogMaterial.SetFloat("ClearSkyFac", ClearSkyFac);
|
|
FogMode fogMode = RenderSettings.fogMode;
|
|
float num4 = 0.01f;
|
|
float fogStartDistance = RenderSettings.fogStartDistance;
|
|
float fogEndDistance = RenderSettings.fogEndDistance;
|
|
bool flag = fogMode == FogMode.Linear;
|
|
float num5 = (flag ? (fogEndDistance - fogStartDistance) : 0f);
|
|
float num6 = ((Mathf.Abs(num5) > 0.0001f) ? (1f / num5) : 0f);
|
|
Vector4 value2 = default(Vector4);
|
|
value2.x = num4 * 1.2011224f;
|
|
value2.y = num4 * 1.442695f;
|
|
value2.z = (flag ? (0f - num6) : 0f);
|
|
value2.w = (flag ? (fogEndDistance * num6) : 0f);
|
|
fogMaterial.SetVector("_SceneFogParams", value2);
|
|
fogMaterial.SetVector("_SceneFogMode", new Vector4((float)fogMode, useRadialDistance ? 1 : 0, 0f, 0f));
|
|
int num7 = 0;
|
|
CustomGraphicsBlit(passNr: (!distanceFog || !heightFog) ? (distanceFog ? 1 : 2) : 0, source: source, dest: destination, fxMaterial: fogMaterial, DistGradient: DistGradient, GradientBounds: GradientBounds, colourPalette: colourPalette);
|
|
}
|
|
|
|
private static void CustomGraphicsBlit(RenderTexture source, RenderTexture dest, Material fxMaterial, int passNr, Gradient DistGradient, Vector2 GradientBounds, Texture2D colourPalette)
|
|
{
|
|
RenderTexture.active = dest;
|
|
fxMaterial.SetTexture("_MainTex", source);
|
|
fxMaterial.SetTexture("_ColorRamp", colourPalette);
|
|
if (GradientBounds != Vector2.zero)
|
|
{
|
|
fxMaterial.SetFloat("_Close", GradientBounds.x);
|
|
fxMaterial.SetFloat("_Far", GradientBounds.y);
|
|
}
|
|
GL.PushMatrix();
|
|
GL.LoadOrtho();
|
|
fxMaterial.SetPass(passNr);
|
|
GL.Begin(7);
|
|
GL.MultiTexCoord2(0, 0f, 0f);
|
|
GL.Vertex3(0f, 0f, 3f);
|
|
GL.MultiTexCoord2(0, 1f, 0f);
|
|
GL.Vertex3(1f, 0f, 2f);
|
|
GL.MultiTexCoord2(0, 1f, 1f);
|
|
GL.Vertex3(1f, 1f, 1f);
|
|
GL.MultiTexCoord2(0, 0f, 1f);
|
|
GL.Vertex3(0f, 1f, 0f);
|
|
GL.End();
|
|
GL.PopMatrix();
|
|
}
|
|
}
|
|
}
|