184 lines
5.8 KiB
C#
184 lines
5.8 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace UnityStandardAssets.ImageEffects
|
|
{
|
|
// Token: 0x0200007E RID: 126
|
|
[ExecuteInEditMode]
|
|
[RequireComponent(typeof(Camera))]
|
|
[AddComponentMenu("Image Effects/Rendering/Screen Space Ambient Occlusion")]
|
|
public class ScreenSpaceAmbientOcclusion : MonoBehaviour
|
|
{
|
|
// Token: 0x0600017D RID: 381 RVA: 0x00010454 File Offset: 0x0000E854
|
|
private static Material CreateMaterial(Shader shader)
|
|
{
|
|
if (!shader)
|
|
{
|
|
return null;
|
|
}
|
|
return new Material(shader)
|
|
{
|
|
hideFlags = HideFlags.HideAndDontSave
|
|
};
|
|
}
|
|
|
|
// Token: 0x0600017E RID: 382 RVA: 0x0001047E File Offset: 0x0000E87E
|
|
private static void DestroyMaterial(Material mat)
|
|
{
|
|
if (mat)
|
|
{
|
|
global::UnityEngine.Object.DestroyImmediate(mat);
|
|
mat = null;
|
|
}
|
|
}
|
|
|
|
// Token: 0x0600017F RID: 383 RVA: 0x00010494 File Offset: 0x0000E894
|
|
private void OnDisable()
|
|
{
|
|
ScreenSpaceAmbientOcclusion.DestroyMaterial(this.m_SSAOMaterial);
|
|
}
|
|
|
|
// Token: 0x06000180 RID: 384 RVA: 0x000104A4 File Offset: 0x0000E8A4
|
|
private void Start()
|
|
{
|
|
if (!SystemInfo.supportsImageEffects || !SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.Depth))
|
|
{
|
|
this.m_Supported = false;
|
|
base.enabled = false;
|
|
return;
|
|
}
|
|
this.CreateMaterials();
|
|
if (!this.m_SSAOMaterial || this.m_SSAOMaterial.passCount != 5)
|
|
{
|
|
this.m_Supported = false;
|
|
base.enabled = false;
|
|
return;
|
|
}
|
|
this.m_Supported = true;
|
|
}
|
|
|
|
// Token: 0x06000181 RID: 385 RVA: 0x00010512 File Offset: 0x0000E912
|
|
private void OnEnable()
|
|
{
|
|
base.GetComponent<Camera>().depthTextureMode |= DepthTextureMode.DepthNormals;
|
|
}
|
|
|
|
// Token: 0x06000182 RID: 386 RVA: 0x00010528 File Offset: 0x0000E928
|
|
private void CreateMaterials()
|
|
{
|
|
if (!this.m_SSAOMaterial && this.m_SSAOShader.isSupported)
|
|
{
|
|
this.m_SSAOMaterial = ScreenSpaceAmbientOcclusion.CreateMaterial(this.m_SSAOShader);
|
|
this.m_SSAOMaterial.SetTexture("_RandomTexture", this.m_RandomTexture);
|
|
}
|
|
}
|
|
|
|
// Token: 0x06000183 RID: 387 RVA: 0x0001057C File Offset: 0x0000E97C
|
|
[ImageEffectOpaque]
|
|
private void OnRenderImage(RenderTexture source, RenderTexture destination)
|
|
{
|
|
if (!this.m_Supported || !this.m_SSAOShader.isSupported)
|
|
{
|
|
base.enabled = false;
|
|
return;
|
|
}
|
|
this.CreateMaterials();
|
|
this.m_Downsampling = Mathf.Clamp(this.m_Downsampling, 1, 6);
|
|
this.m_Radius = Mathf.Clamp(this.m_Radius, 0.05f, 1f);
|
|
this.m_MinZ = Mathf.Clamp(this.m_MinZ, 1E-05f, 0.5f);
|
|
this.m_OcclusionIntensity = Mathf.Clamp(this.m_OcclusionIntensity, 0.5f, 4f);
|
|
this.m_OcclusionAttenuation = Mathf.Clamp(this.m_OcclusionAttenuation, 0.2f, 2f);
|
|
this.m_Blur = Mathf.Clamp(this.m_Blur, 0, 4);
|
|
RenderTexture renderTexture = RenderTexture.GetTemporary(source.width / this.m_Downsampling, source.height / this.m_Downsampling, 0);
|
|
float fieldOfView = base.GetComponent<Camera>().fieldOfView;
|
|
float farClipPlane = base.GetComponent<Camera>().farClipPlane;
|
|
float num = Mathf.Tan(fieldOfView * 0.017453292f * 0.5f) * farClipPlane;
|
|
float num2 = num * base.GetComponent<Camera>().aspect;
|
|
this.m_SSAOMaterial.SetVector("_FarCorner", new Vector3(num2, num, farClipPlane));
|
|
int num3;
|
|
int num4;
|
|
if (this.m_RandomTexture)
|
|
{
|
|
num3 = this.m_RandomTexture.width;
|
|
num4 = this.m_RandomTexture.height;
|
|
}
|
|
else
|
|
{
|
|
num3 = 1;
|
|
num4 = 1;
|
|
}
|
|
this.m_SSAOMaterial.SetVector("_NoiseScale", new Vector3((float)renderTexture.width / (float)num3, (float)renderTexture.height / (float)num4, 0f));
|
|
this.m_SSAOMaterial.SetVector("_Params", new Vector4(this.m_Radius, this.m_MinZ, 1f / this.m_OcclusionAttenuation, this.m_OcclusionIntensity));
|
|
bool flag = this.m_Blur > 0;
|
|
Graphics.Blit((!flag) ? source : null, renderTexture, this.m_SSAOMaterial, (int)this.m_SampleCount);
|
|
if (flag)
|
|
{
|
|
RenderTexture temporary = RenderTexture.GetTemporary(source.width, source.height, 0);
|
|
this.m_SSAOMaterial.SetVector("_TexelOffsetScale", new Vector4((float)this.m_Blur / (float)source.width, 0f, 0f, 0f));
|
|
this.m_SSAOMaterial.SetTexture("_SSAO", renderTexture);
|
|
Graphics.Blit(null, temporary, this.m_SSAOMaterial, 3);
|
|
RenderTexture.ReleaseTemporary(renderTexture);
|
|
RenderTexture temporary2 = RenderTexture.GetTemporary(source.width, source.height, 0);
|
|
this.m_SSAOMaterial.SetVector("_TexelOffsetScale", new Vector4(0f, (float)this.m_Blur / (float)source.height, 0f, 0f));
|
|
this.m_SSAOMaterial.SetTexture("_SSAO", temporary);
|
|
Graphics.Blit(source, temporary2, this.m_SSAOMaterial, 3);
|
|
RenderTexture.ReleaseTemporary(temporary);
|
|
renderTexture = temporary2;
|
|
}
|
|
this.m_SSAOMaterial.SetTexture("_SSAO", renderTexture);
|
|
Graphics.Blit(source, destination, this.m_SSAOMaterial, 4);
|
|
RenderTexture.ReleaseTemporary(renderTexture);
|
|
}
|
|
|
|
// Token: 0x04000313 RID: 787
|
|
[Range(0.05f, 1f)]
|
|
public float m_Radius = 0.4f;
|
|
|
|
// Token: 0x04000314 RID: 788
|
|
public ScreenSpaceAmbientOcclusion.SSAOSamples m_SampleCount = ScreenSpaceAmbientOcclusion.SSAOSamples.Medium;
|
|
|
|
// Token: 0x04000315 RID: 789
|
|
[Range(0.5f, 4f)]
|
|
public float m_OcclusionIntensity = 1.5f;
|
|
|
|
// Token: 0x04000316 RID: 790
|
|
[Range(0f, 4f)]
|
|
public int m_Blur = 2;
|
|
|
|
// Token: 0x04000317 RID: 791
|
|
[Range(1f, 6f)]
|
|
public int m_Downsampling = 2;
|
|
|
|
// Token: 0x04000318 RID: 792
|
|
[Range(0.2f, 2f)]
|
|
public float m_OcclusionAttenuation = 1f;
|
|
|
|
// Token: 0x04000319 RID: 793
|
|
[Range(1E-05f, 0.5f)]
|
|
public float m_MinZ = 0.01f;
|
|
|
|
// Token: 0x0400031A RID: 794
|
|
public Shader m_SSAOShader;
|
|
|
|
// Token: 0x0400031B RID: 795
|
|
private Material m_SSAOMaterial;
|
|
|
|
// Token: 0x0400031C RID: 796
|
|
public Texture2D m_RandomTexture;
|
|
|
|
// Token: 0x0400031D RID: 797
|
|
private bool m_Supported;
|
|
|
|
// Token: 0x0200007F RID: 127
|
|
public enum SSAOSamples
|
|
{
|
|
// Token: 0x0400031F RID: 799
|
|
Low,
|
|
// Token: 0x04000320 RID: 800
|
|
Medium,
|
|
// Token: 0x04000321 RID: 801
|
|
High
|
|
}
|
|
}
|
|
}
|