mirror of
https://github.com/ApfelTeeSaft/Slender.git
synced 2026-08-27 03:43:28 +00:00
142 lines
3.8 KiB
C#
142 lines
3.8 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
[Serializable]
|
|
[ExecuteInEditMode]
|
|
[RequireComponent(typeof(Camera))]
|
|
[AddComponentMenu("Image Effects/Global Fog")]
|
|
public class GlobalFog : PostEffectsBase
|
|
{
|
|
[Serializable]
|
|
public enum FogMode
|
|
{
|
|
AbsoluteYAndDistance = 0,
|
|
AbsoluteY = 1,
|
|
Distance = 2,
|
|
RelativeYAndDistance = 3
|
|
}
|
|
|
|
public FogMode fogMode;
|
|
|
|
private float CAMERA_NEAR;
|
|
|
|
private float CAMERA_FAR;
|
|
|
|
private float CAMERA_FOV;
|
|
|
|
private float CAMERA_ASPECT_RATIO;
|
|
|
|
public float startDistance;
|
|
|
|
public float globalDensity;
|
|
|
|
public float heightScale;
|
|
|
|
public float height;
|
|
|
|
public Color globalFogColor;
|
|
|
|
public Shader fogShader;
|
|
|
|
private Material fogMaterial;
|
|
|
|
public GlobalFog()
|
|
{
|
|
fogMode = FogMode.AbsoluteYAndDistance;
|
|
CAMERA_NEAR = 0.5f;
|
|
CAMERA_FAR = 50f;
|
|
CAMERA_FOV = 60f;
|
|
CAMERA_ASPECT_RATIO = 1.333333f;
|
|
startDistance = 200f;
|
|
globalDensity = 1f;
|
|
heightScale = 100f;
|
|
globalFogColor = Color.grey;
|
|
}
|
|
|
|
public virtual void OnDisable()
|
|
{
|
|
if ((bool)fogMaterial)
|
|
{
|
|
UnityEngine.Object.DestroyImmediate(fogMaterial);
|
|
}
|
|
}
|
|
|
|
public override bool CheckResources()
|
|
{
|
|
CheckSupport(true);
|
|
fogMaterial = CheckShaderAndCreateMaterial(fogShader, fogMaterial);
|
|
if (!isSupported)
|
|
{
|
|
ReportAutoDisable();
|
|
}
|
|
return isSupported;
|
|
}
|
|
|
|
public virtual void OnRenderImage(RenderTexture source, RenderTexture destination)
|
|
{
|
|
if (!CheckResources())
|
|
{
|
|
Graphics.Blit(source, destination);
|
|
return;
|
|
}
|
|
CAMERA_NEAR = GetComponent<Camera>().nearClipPlane;
|
|
CAMERA_FAR = GetComponent<Camera>().farClipPlane;
|
|
CAMERA_FOV = GetComponent<Camera>().fieldOfView;
|
|
CAMERA_ASPECT_RATIO = GetComponent<Camera>().aspect;
|
|
Matrix4x4 identity = Matrix4x4.identity;
|
|
Vector4 vector = default(Vector4);
|
|
Vector3 vector2 = default(Vector3);
|
|
float num = CAMERA_FOV * 0.5f;
|
|
Vector3 vector3 = GetComponent<Camera>().transform.right * CAMERA_NEAR * Mathf.Tan(num * ((float)Math.PI / 180f)) * CAMERA_ASPECT_RATIO;
|
|
Vector3 vector4 = GetComponent<Camera>().transform.up * CAMERA_NEAR * Mathf.Tan(num * ((float)Math.PI / 180f));
|
|
Vector3 vector5 = GetComponent<Camera>().transform.forward * CAMERA_NEAR - vector3 + vector4;
|
|
float num2 = vector5.magnitude * CAMERA_FAR / CAMERA_NEAR;
|
|
vector5.Normalize();
|
|
vector5 *= num2;
|
|
Vector3 vector6 = GetComponent<Camera>().transform.forward * CAMERA_NEAR + vector3 + vector4;
|
|
vector6.Normalize();
|
|
vector6 *= num2;
|
|
Vector3 vector7 = GetComponent<Camera>().transform.forward * CAMERA_NEAR + vector3 - vector4;
|
|
vector7.Normalize();
|
|
vector7 *= num2;
|
|
Vector3 vector8 = GetComponent<Camera>().transform.forward * CAMERA_NEAR - vector3 - vector4;
|
|
vector8.Normalize();
|
|
vector8 *= num2;
|
|
identity.SetRow(0, vector5);
|
|
identity.SetRow(1, vector6);
|
|
identity.SetRow(2, vector7);
|
|
identity.SetRow(3, vector8);
|
|
fogMaterial.SetMatrix("_FrustumCornersWS", identity);
|
|
fogMaterial.SetVector("_CameraWS", GetComponent<Camera>().transform.position);
|
|
fogMaterial.SetVector("_StartDistance", new Vector4(1f / startDistance, num2 - startDistance));
|
|
fogMaterial.SetVector("_Y", new Vector4(height, 1f / heightScale));
|
|
fogMaterial.SetFloat("_GlobalDensity", globalDensity * 0.01f);
|
|
fogMaterial.SetColor("_FogColor", globalFogColor);
|
|
CustomGraphicsBlit(source, destination, fogMaterial, (int)fogMode);
|
|
}
|
|
|
|
public static void CustomGraphicsBlit(RenderTexture source, RenderTexture dest, Material fxMaterial, int passNr)
|
|
{
|
|
RenderTexture.active = dest;
|
|
fxMaterial.SetTexture("_MainTex", source);
|
|
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();
|
|
}
|
|
|
|
public override void Main()
|
|
{
|
|
}
|
|
}
|