Skip to content

Commit

Permalink
codebase cleanup and fixed a small issue with scene fog solution
Browse files Browse the repository at this point in the history
  • Loading branch information
frostbone25 committed Oct 10, 2022
1 parent 0839de4 commit a19847b
Show file tree
Hide file tree
Showing 7 changed files with 441 additions and 432 deletions.
511 changes: 257 additions & 254 deletions CubemapFog/Assets/CubemapFog/Editor/GenerateSkyCubemap.cs

Large diffs are not rendered by default.

67 changes: 46 additions & 21 deletions CubemapFog/Assets/CubemapFog/PostProcess/CubemapFog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;

namespace VRProject1_PostProcessing
namespace CubemapFog
{
[Serializable]
[PostProcess(typeof(CubemapFogRenderer), PostProcessEvent.BeforeTransparent, "Custom/CustomFog")]
public sealed class CubemapFog : PostProcessEffectSettings
{
[Range(0f, 1f)]
public FloatParameter intensity = new FloatParameter() { value = 1.0f };

[Header("Main Properties")]
[Header("Cubemap")]
public TextureParameter fogCubemap = new TextureParameter();
public BoolParameter fogOccludeSky = new BoolParameter();
public FloatParameter fogCubemapExposure = new FloatParameter() { value = 1.0f };

[Header("Mip Mapping Properties")]
public BoolParameter fogUseConstantMip = new BoolParameter();
public FloatParameter fogCubemapMipLevel = new FloatParameter();
public FloatParameter fogCubemapMipDistance = new FloatParameter();
public FloatParameter fogCubemapMipMultiplier = new FloatParameter() { value = 1.0f };

[Header("Fog")]
[Header("Main Fog Properties")]
[Range(0f, 1f)] public FloatParameter intensity = new FloatParameter() { value = 1.0f };
public BoolParameter fogOccludeSky = new BoolParameter();
public FloatParameter fogStartDistance = new FloatParameter();
public FloatParameter fogDensity = new FloatParameter() { value = 1.0f };

[Header("Height Fog")]
public BoolParameter heightEnable = new BoolParameter();
public FloatParameter heightFogStartDistance = new FloatParameter();
public FloatParameter heightFogDensity = new FloatParameter() { value = 1.0f };
public FloatParameter heightFogHeight = new FloatParameter();
public FloatParameter heightFogFallof = new FloatParameter() { value = 1.0f };

[Header("Debug")]
public BoolParameter viewFog = new BoolParameter();
public BoolParameter viewHeightFog = new BoolParameter();
public BoolParameter viewMipDistance = new BoolParameter();
public BoolParameter viewCubemap = new BoolParameter();
}
Expand All @@ -44,36 +44,61 @@ public sealed class CubemapFogRenderer : PostProcessEffectRenderer<CubemapFog>
public override void Render(PostProcessRenderContext context)
{
var sheet = context.propertySheets.Get(Shader.Find("Hidden/CubemapFog"));

//|||||||||||||||||||||||||||||||||||||| CUBEMAP PROPERTIES ||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||| CUBEMAP PROPERTIES ||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||| CUBEMAP PROPERTIES ||||||||||||||||||||||||||||||||||||||
var cubeTex = settings.fogCubemap.value == null ? RuntimeUtilities.blackTexture : settings.fogCubemap.value;

Vector4 debugModes = new Vector4(settings.viewFog.value ? 1 : 0, settings.viewHeightFog.value ? 1 : 0, settings.viewMipDistance.value ? 1 : 0, settings.viewCubemap.value ? 1 : 0);
sheet.properties.SetTexture("_Fog_Cubemap", cubeTex);
sheet.properties.SetFloat("_Fog_Cubemap_Exposure", settings.fogCubemapExposure.value);

//effect intensity
//|||||||||||||||||||||||||||||||||||||| MAIN PROPERTIES ||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||| MAIN PROPERTIES ||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||| MAIN PROPERTIES ||||||||||||||||||||||||||||||||||||||
sheet.properties.SetFloat("_Intensity", settings.intensity.value);
sheet.properties.SetFloat("_Fog_StartDistance", settings.fogStartDistance.value);
sheet.properties.SetFloat("_Fog_Density", settings.fogDensity.value);

if (settings.fogOccludeSky.value)
sheet.EnableKeyword("OCCLUDE_SKY");
else
sheet.DisableKeyword("OCCLUDE_SKY");

//|||||||||||||||||||||||||||||||||||||| MIP MAP PROPERTIES ||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||| MIP MAP PROPERTIES ||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||| MIP MAP PROPERTIES ||||||||||||||||||||||||||||||||||||||
if (settings.fogUseConstantMip.value)
sheet.EnableKeyword("USE_CONSTANT_MIP");
else
sheet.DisableKeyword("USE_CONSTANT_MIP");

//main properties
sheet.properties.SetTexture("_Fog_Cubemap", cubeTex);
sheet.properties.SetFloat("_Fog_OccludeSky", settings.fogOccludeSky.value ? 1 : 0);
sheet.properties.SetFloat("_Fog_Cubemap_Exposure", settings.fogCubemapExposure.value);
sheet.properties.SetFloat("_Fog_Cubemap_UseConstantMip", settings.fogUseConstantMip.value ? 1 : 0);
sheet.properties.SetFloat("_Fog_Cubemap_Mip_MinLevel", settings.fogCubemapMipLevel.value);
sheet.properties.SetFloat("_Fog_Cubemap_Mip_Distance", settings.fogCubemapMipDistance.value);
sheet.properties.SetFloat("_Fog_Cubemap_Mip_Multiplier", settings.fogCubemapMipMultiplier.value);

//fog props
sheet.properties.SetFloat("_Fog_StartDistance", settings.fogStartDistance.value);
sheet.properties.SetFloat("_Fog_Density", settings.fogDensity.value);
//|||||||||||||||||||||||||||||||||||||| HEIGHT FOG PROPERTIES ||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||| HEIGHT FOG PROPERTIES ||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||| HEIGHT FOG PROPERTIES ||||||||||||||||||||||||||||||||||||||
if (settings.heightEnable.value)
sheet.EnableKeyword("DO_HEIGHT_FOG");
else
sheet.DisableKeyword("DO_HEIGHT_FOG");

//height fog props
sheet.properties.SetFloat("_Fog_Height_StartDistance", settings.heightFogStartDistance.value);
sheet.properties.SetFloat("_Fog_Height_Density", settings.heightFogDensity.value);
sheet.properties.SetFloat("_Fog_Height", settings.heightFogHeight.value);
sheet.properties.SetFloat("_Fog_Height_Falloff", settings.heightFogFallof.value);

//other shader values
//|||||||||||||||||||||||||||||||||||||| DEBUG PROPERTIES ||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||| DEBUG PROPERTIES ||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||| DEBUG PROPERTIES ||||||||||||||||||||||||||||||||||||||
Vector4 debugModes = new Vector4(settings.viewFog.value ? 1 : 0, 0, settings.viewMipDistance.value ? 1 : 0, settings.viewCubemap.value ? 1 : 0);
sheet.properties.SetVector("DebugModes", debugModes);
sheet.properties.SetVector("CamValues", new Vector2(context.camera.farClipPlane, context.camera.nearClipPlane));

//|||||||||||||||||||||||||||||||||||||| FINAL ||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||| FINAL ||||||||||||||||||||||||||||||||||||||
//|||||||||||||||||||||||||||||||||||||| FINAL ||||||||||||||||||||||||||||||||||||||
Matrix4x4 clipToView = GL.GetGPUProjectionMatrix(context.camera.projectionMatrix, true).inverse;
sheet.properties.SetMatrix("_ClipToView", clipToView);

Expand Down
95 changes: 42 additions & 53 deletions CubemapFog/Assets/CubemapFog/PostProcess/CubemapFog.shader
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,39 @@
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#pragma shader_feature_local OCCLUDE_SKY
#pragma shader_feature_local USE_CONSTANT_MIP
#pragma shader_feature_local DO_HEIGHT_FOG
#pragma fragmentoption ARB_precision_hint_fastest
#include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"

TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
samplerCUBE _Fog_Cubemap;

float4 _MainTex_TexelSize;
float4x4 _ClipToView;
float4x4 _ViewProjInv;

float _Intensity;
float _Fog_Cubemap_UseConstantMip;
//cubemap properties
samplerCUBE _Fog_Cubemap;
float _Fog_Cubemap_Exposure;

//mip mapping properties
float _Fog_Cubemap_Mip_MinLevel;
float _Fog_Cubemap_Mip_Distance;
float _Fog_Cubemap_Mip_Multiplier;
float _Fog_Cubemap_Exposure;

//main fog properties
float _Intensity;
float _Fog_StartDistance;
float _Fog_Density;

//height fog
float _Fog_Height_StartDistance;
float _Fog_Height_Density;
float _Fog_Height;
float _Fog_Height_Falloff;
float _Fog_OccludeSky;

//other shader values
float2 CamValues; //x = farplane, y = nearplane
//debugging
float4 DebugModes; //x = classic fog, y = height fog, z = mip distance, w = cubemap only

struct NewAttributesDefault
Expand All @@ -52,6 +57,7 @@
float2 texcoordStereo : TEXCOORD1;
float3 viewSpaceDir : TEXCOORD2;
float3 ray : TEXCOORD3;

#if STEREO_INSTANCING_ENABLED
uint stereoTargetEyeIndex : SV_RenderTargetArrayIndex;
#endif
Expand All @@ -60,18 +66,26 @@
Varyings Vert(NewAttributesDefault v)
{
Varyings o;

o.vertex = float4(v.vertex.xy, 0.0, 1.0);
o.texcoord = TransformTriangleVertexToUV(v.vertex.xy);
o.viewSpaceDir = mul(_ClipToView, o.vertex).xyz;
o.ray = v.texcoord.xyz;

#if UNITY_UV_STARTS_AT_TOP
o.texcoord = o.texcoord * float2(1.0, -1.0) + float2(0.0, 1.0);
#endif

o.texcoordStereo = TransformStereoScreenSpaceTex(o.texcoord, 1.0);

return o;
}

float ComputeDistance(float3 ray, float depth)
{
return length(ray * depth) - _ProjectionParams.y;
}

float GetDepth(float2 uv)
{
return SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture, uv).r;
Expand All @@ -80,38 +94,21 @@
float4 GetWorldPositionFromDepth(float2 uv_depth)
{
float depth = GetDepth(uv_depth);

#if defined(SHADER_API_OPENGL)
depth = depth * 2.0 - 1.0;
#endif

float4 H = float4(uv_depth.x * 2.0 - 1.0, (uv_depth.y) * 2.0 - 1.0, depth, 1.0);

float4 D = mul(_ViewProjInv, H);
return D / D.w;
}

float CustomLuminance(float3 color)
{
float4 colorSpace = float4(0.22, 0.707, 0.071, 0.0);
return dot(color, colorSpace.rgb);
}

float ComputeDistance(float3 ray, float depth)
{
float dist;

dist = length(ray * depth);
dist -= _ProjectionParams.y;

return dist;
return D / D.w;
}

float4 Frag(Varyings i) : SV_Target
{
float2 uv = i.texcoordStereo.xy;

float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);

float zsample = GetDepth(uv);
float4 worldPos = GetWorldPositionFromDepth(uv);
float4 cameraWorldPos = mul(unity_ObjectToWorld, worldPos);
Expand All @@ -121,43 +118,35 @@
float computedDistance = ComputeDistance(cameraWorldDir.rgb, depth);

float fog = max(0.0f, (computedDistance - _Fog_StartDistance) * _Fog_Density);
fog = saturate(fog);

#ifdef DO_HEIGHT_FOG
float fog_height = max(0.0f, (computedDistance - _Fog_Height_StartDistance) * _Fog_Height_Density);
fog_height = lerp(fog_height, 0.0f, (worldPos.y * _Fog_Height_Falloff) - _Fog_Height);

fog = saturate(fog);
fog_height = saturate(fog_height);
fog = saturate(fog + fog_height);
#endif

if (depth == 1.0f && _Fog_OccludeSky < 1)
{
#ifdef OCCLUDE_SKY
#else
if (depth == 1.0f)
fog = 0.0f;
fog_height = 0.0f;
}
#endif

fog = lerp(fog, 0.0f, 1 - _Intensity);
fog_height = lerp(fog_height, 0.0f, 1 - _Intensity);

float4 cubemap = float4(0,0,0,0);
float mipDistance = 0.0f;

if (_Fog_Cubemap_UseConstantMip > 0) //use a constant mip level?
{
cubemap = texCUBElod(_Fog_Cubemap, float4(-cameraWorldDir.xyz, _Fog_Cubemap_Mip_MinLevel)) * _Fog_Cubemap_Exposure;
}
else //using a fog based mip plevel
{
mipDistance = 1 - max(0.0f, (computedDistance - _Fog_Cubemap_Mip_Distance) * _Fog_Cubemap_Mip_Multiplier);
mipDistance = saturate(mipDistance) * _Fog_Cubemap_Mip_MinLevel;

cubemap = texCUBElod(_Fog_Cubemap, float4(-cameraWorldDir.xyz, mipDistance)) * _Fog_Cubemap_Exposure;
}
#ifdef USE_CONSTANT_MIP
float mipDistance = _Fog_Cubemap_Mip_MinLevel;
#else
float mipDistance = 1 - max(0.0f, (computedDistance - _Fog_Cubemap_Mip_Distance) * _Fog_Cubemap_Mip_Multiplier);
mipDistance = saturate(mipDistance) * _Fog_Cubemap_Mip_MinLevel;
#endif
float4 cubemap = texCUBElod(_Fog_Cubemap, float4(-cameraWorldDir.xyz, mipDistance)) * _Fog_Cubemap_Exposure;

float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
fog = lerp(fog, 0.0f, 1 - _Intensity);
color.rgb = lerp(color.rgb, cubemap.rgb, fog);
color.rgb = lerp(color.rgb, cubemap.rgb, fog_height);

if (DebugModes.x > 0) //view classic fog
return float4(fog, fog, fog, 1.0);
else if (DebugModes.y > 0) //view height fog
return float4(fog_height, fog_height, fog_height, 1.0);
else if (DebugModes.z > 0) //view mip level distance
return float4(mipDistance, mipDistance, mipDistance, mipDistance);
else if (DebugModes.w > 0) //view cubemap only
Expand Down
10 changes: 6 additions & 4 deletions CubemapFog/Assets/CubemapFog/SceneObject/SceneCubemapFog.mat
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Material:
m_PrefabAsset: {fileID: 0}
m_Name: SceneCubemapFog
m_Shader: {fileID: 4800000, guid: 54eceac8c5670054b883ffe0676eb87d, type: 3}
m_ShaderKeywords: _FOG_CUBEMAP_USECONSTANTMIP_ON
m_ShaderKeywords: DO_HEIGHT_FOG OCCLUDE_SKY USE_CONSTANT_MIP UseConstantMip _FOG_CUBEMAP_USECONSTANTMIP_ON
_OCCLUDE_SKY
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
Expand Down Expand Up @@ -77,17 +78,18 @@ Material:
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _Fog_Cubemap_Exposure: 1
- _Fog_Cubemap_Mip_Distance: 1
- _Fog_Cubemap_Mip_Distance: 11.61
- _Fog_Cubemap_Mip_MinLevel: 4.31
- _Fog_Cubemap_Mip_Multiplier: 1
- _Fog_Cubemap_Mip_Multiplier: 0.22
- _Fog_Cubemap_UseConstantMip: 1
- _Fog_Density: 0.09
- _Fog_DoHeightFog: 1
- _Fog_Height: 2.24
- _Fog_Height_Density: 0.05
- _Fog_Height_Falloff: 0.14
- _Fog_Height_StartDistance: -1.65
- _Fog_OccludeSky: 1
- _Fog_StartDistance: 7.01
- _Fog_StartDistance: 2.94
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
Expand Down
Loading

0 comments on commit a19847b

Please # to comment.