Skip to content

Instantly share code, notes, and snippets.

@hvent90
Last active January 21, 2019 00:41
Show Gist options
  • Save hvent90/e0f62fb1a02581e9856cbc63997a9081 to your computer and use it in GitHub Desktop.
Save hvent90/e0f62fb1a02581e9856cbc63997a9081 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.PostProcessing;
[Serializable]
[PostProcess(typeof(CloudsExporterRenderer), PostProcessEvent.AfterStack, "Custom/CloudsExporter")]
public sealed class CloudsExporter : PostProcessEffectSettings
{
public FloatParameter MinHeight = new FloatParameter { value = 0.0f };
public FloatParameter MaxHeight = new FloatParameter { value = 5.0f };
public FloatParameter FadeDist = new FloatParameter { value = 2f };
public FloatParameter Scale = new FloatParameter { value = 5f };
public TextureParameter ValueNoiseTable = new TextureParameter { value = null };
}
public sealed class CloudsExporterRenderer: PostProcessEffectRenderer<CloudsExporter>
{
public override void Render(PostProcessRenderContext context)
{
Transform Sun = GameObject.FindGameObjectWithTag("MainLight").transform;
Camera _Cam = Camera.main;
var shader = context.propertySheets.Get(Shader.Find("Raymarch/clouds2"));
//sheet.properties.SetFloat("_Blend", settings.blend);
if (_Cam == null)
_Cam = Camera.main;
shader.properties.SetTexture("_ValueNoise", settings.ValueNoiseTable);
if (Sun != null)
{
RenderSettings.skybox.SetVector("_SunDir", -Sun.forward);
shader.properties.SetVector("_SunDir", -Sun.forward);
}
else
{
RenderSettings.skybox.SetVector("_SunDir", Vector3.up);
shader.properties.SetVector("_SunDir", Vector3.up);
}
shader.properties.SetFloat("_MinHeight", settings.MinHeight);
shader.properties.SetFloat("_MaxHeight", settings.MaxHeight);
shader.properties.SetFloat("_FadeDist", settings.FadeDist);
shader.properties.SetFloat("_Scale", settings.Scale);
shader.properties.SetMatrix("_FrustumCornersWS", GetFrustumCorners(_Cam));
shader.properties.SetMatrix("_CameraInvViewMatrix", _Cam.cameraToWorldMatrix);
shader.properties.SetVector("_CameraWS", _Cam.transform.position);
CustomGraphicsBlit(context.source, context.destination, shader.properties, 0);
context.command.BlitFullscreenTriangle(context.source, context.destination, shader, 0);
}
private Matrix4x4 GetFrustumCorners(Camera cam)
{
float camFov = cam.fieldOfView;
float camAspect = cam.aspect;
Matrix4x4 frustumCorners = Matrix4x4.identity;
float fovWHalf = camFov * 0.5f;
float tan_fov = Mathf.Tan(fovWHalf * Mathf.Deg2Rad);
Vector3 toRight = Vector3.right * tan_fov * camAspect;
Vector3 toTop = Vector3.up * tan_fov;
Vector3 topLeft = (-Vector3.forward - toRight + toTop);
Vector3 topRight = (-Vector3.forward + toRight + toTop);
Vector3 bottomRight = (-Vector3.forward + toRight - toTop);
Vector3 bottomLeft = (-Vector3.forward - toRight - toTop);
frustumCorners.SetRow(0, topLeft);
frustumCorners.SetRow(1, topRight);
frustumCorners.SetRow(2, bottomRight);
frustumCorners.SetRow(3, bottomLeft);
return frustumCorners;
}
static void CustomGraphicsBlit(RenderTargetIdentifier source, RenderTargetIdentifier dest, MaterialPropertyBlock fxMaterial, int passNr)
{
//RenderTexture.active = dest.tex;
//fxMaterial.SetTexture("_MainTex", source);
GL.PushMatrix();
GL.LoadOrtho();
//fxMaterial.SetPass(passNr);
GL.Begin(GL.QUADS);
GL.MultiTexCoord2(0, 0.0f, 0.0f);
GL.Vertex3(0.0f, 0.0f, 3.0f); // BL
GL.MultiTexCoord2(0, 1.0f, 0.0f);
GL.Vertex3(1.0f, 0.0f, 2.0f); // BR
GL.MultiTexCoord2(0, 1.0f, 1.0f);
GL.Vertex3(1.0f, 1.0f, 1.0f); // TR
GL.MultiTexCoord2(0, 0.0f, 1.0f);
GL.Vertex3(0.0f, 1.0f, 0.0f); // TL
GL.End();
GL.PopMatrix();
}
}
[Serializable]
public sealed class TransformParameter : ParameterOverride<Transform> { }
[Serializable]
public sealed class ShaderParameter : ParameterOverride<Shader> { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment