Last active
January 21, 2019 21:53
-
-
Save hvent90/67099503eff08a314dd013443d1d4c9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// !!IMPORTANT!! | |
// The file name of this script MUST be RaymarchPostProcess.cs in order for it to be properly serialized! | |
// ------------------------------------------------ | |
using System; | |
using UnityEngine; | |
using UnityEngine.Rendering; | |
using UnityEngine.Rendering.PostProcessing; | |
[Serializable] | |
public sealed class ShaderParameter : ParameterOverride<Shader> { } | |
[Serializable] | |
[PostProcess(typeof(RaymarchPostProcessRenderer), PostProcessEvent.AfterStack, "Custom/RaymarchPostProcess")] | |
public sealed class RaymarchPostProcess : PostProcessEffectSettings | |
{ | |
public IntParameter maxIterations = new IntParameter { value = 64 }; | |
public FloatParameter maxDistance = new FloatParameter { value = 100f }; | |
public FloatParameter minDistance = new FloatParameter { value = 0.01f }; | |
} | |
public sealed class RaymarchPostProcessRenderer : PostProcessEffectRenderer<RaymarchPostProcess> | |
{ | |
[SerializeField] | |
private Shader _shader; | |
public Material _raymarchMaterial | |
{ | |
get | |
{ | |
if (!_raymarchMat && _shader) | |
{ | |
_raymarchMat = new Material(_shader); | |
_raymarchMat.hideFlags = HideFlags.HideAndDontSave; | |
} | |
return _raymarchMat; | |
} | |
} | |
private Material _raymarchMat; | |
public Camera _camera | |
{ | |
get | |
{ | |
if (!_cam) | |
{ | |
_cam = Camera.main; | |
} | |
return _cam; | |
} | |
} | |
private Camera _cam; | |
public override void Render(PostProcessRenderContext context) | |
{ | |
if (!_cam) | |
{ | |
_cam = Camera.main; | |
} | |
var sheet = context.propertySheets.Get(Shader.Find("Raymarch/RaymarchHDRP")); | |
sheet.properties.SetMatrix("_CamFrustum", CamFrustum(_cam)); | |
sheet.properties.SetMatrix("_CamToWorld", _cam.cameraToWorldMatrix); | |
sheet.properties.SetVector("_CamWorldSpace", _cam.transform.position); | |
sheet.properties.SetInt("_MaxIterations", settings.maxIterations); | |
sheet.properties.SetFloat("_MaxDistance", settings.maxDistance); | |
sheet.properties.SetFloat("_MinDistance", settings.minDistance); | |
context.command.BlitFullscreenTriangle(context.source, context.destination, sheet, 0); | |
} | |
private Matrix4x4 CamFrustum(Camera cam) | |
{ | |
Matrix4x4 frustum = Matrix4x4.identity; | |
float fov = Mathf.Tan((cam.fieldOfView * 0.5f) * Mathf.Deg2Rad); | |
Vector3 goUp = Vector3.up * fov; | |
Vector3 goRight = Vector3.right * fov * cam.aspect; | |
Vector3 TL = (-Vector3.forward - goRight + goUp); | |
Vector3 TR = (-Vector3.forward + goRight + goUp); | |
Vector3 BR = (-Vector3.forward + goRight - goUp); | |
Vector3 BL = (-Vector3.forward - goRight - goUp); | |
frustum.SetRow(0, TL); | |
frustum.SetRow(1, TR); | |
frustum.SetRow(2, BR); | |
frustum.SetRow(3, BL); | |
return frustum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment