Skip to content

Commit

Permalink
Cleanup GetFogCoord by implementing it correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
whatdahopper committed May 29, 2023
1 parent de67532 commit e553ef9
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 50 deletions.
60 changes: 20 additions & 40 deletions BloomFog.cginc
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef BLOOM_FOG_CG_INCLUDED
#define BLOOM_FOG_CG_INCLUDED

// #pragma multi_compile __ ENABLE_BLOOM_FOG

#if ENABLE_BLOOM_FOG

uniform float _CustomFogOffset;
Expand All @@ -13,33 +11,14 @@ uniform sampler2D _BloomPrePassTexture;
uniform float2 _CustomFogTextureToScreenRatio;
uniform float _StereoCameraEyeOffset;

inline float4 GetFogCoord(float4 clipPos) {
inline float2 GetFogCoord(float4 clipPos) {
float4 screenPos = ComputeNonStereoScreenPos(clipPos);
float2 screenPosNormalized = screenPos.xy / screenPos.w;
float eyeOffset = (unity_StereoEyeIndex * (_StereoCameraEyeOffset * 2)) + -_StereoCameraEyeOffset;
#if UNITY_UV_STARTS_AT_TOP
float4 screenPos;
screenPos.xyw = clipPos.yxw * 0.5f;
screenPos.z = screenPos.x * _ProjectionParams.x;
screenPos.yz = screenPos.ww + screenPos.yz;
screenPos.x = (clipPos.w * eyeOffset) + screenPos.y;
return float4(((-screenPos.ww + screenPos.xz) * _CustomFogTextureToScreenRatio) + screenPos.ww, clipPos.zw);
#else
// This is so gross, I hate it.
// Required for Android... sigh.
// I'll eventually clean this up... or not.
float4 u_xlat1;
float2 u_xlat3;
float2 u_xlat16_2;
float u_xlat16_10;
u_xlat1.x = clipPos.y * _ProjectionParams.x;
u_xlat1.w = u_xlat1.x * 0.5f;
u_xlat1.xz = clipPos.xw * 0.5f;
u_xlat1.xy = u_xlat1.zz + u_xlat1.xw;
u_xlat3.x = (clipPos.w * eyeOffset) + u_xlat1.x;
u_xlat3.y = clipPos.w + -u_xlat1.y;
u_xlat16_2.xy = (-clipPos.ww * 0.5f) + u_xlat3.xy;
u_xlat16_10 = clipPos.w * 0.5f;
return float4((u_xlat16_2.xy * _CustomFogTextureToScreenRatio.xy) + u_xlat16_10, clipPos.zw);
#endif
return float2(
((eyeOffset + screenPosNormalized.x) + -0.5) * _CustomFogTextureToScreenRatio.x + 0.5,
(screenPosNormalized.y + -0.5) * _CustomFogTextureToScreenRatio.y + 0.5
);
}

inline float GetHeightFogIntensity(float3 worldPos, float fogHeightOffset, float fogHeightScale) {
Expand All @@ -57,40 +36,41 @@ inline float GetFogIntensity(float3 distance, float fogStartOffset, float fogSca
return -fogIntensity;
}

// v2f: BLOOM_FOG_COORDS(1, 2)
#define BLOOM_FOG_COORDS(fogCoordIndex, worldPosIndex) \
float4 fogCoord : TEXCOORD##fogCoordIndex; \
float2 fogCoord : TEXCOORD##fogCoordIndex; \
float3 worldPos : TEXCOORD##worldPosIndex;

#define BLOOM_FOG_SURFACE_INPUT \
float4 fogCoord; \
float2 fogCoord; \
float3 worldPos;

// vert: BLOOM_FOG_TRANSFER(o, v.vertex);
#define BLOOM_FOG_TRANSFER(outputStruct, inputVertex) \
outputStruct.worldPos = mul(unity_ObjectToWorld, inputVertex); \
outputStruct.fogCoord = GetFogCoord(UnityObjectToClipPos(inputVertex))
#define BLOOM_FOG_INITIALIZE(outputStruct, inputVertex) \
outputStruct.fogCoord = GetFogCoord(UnityObjectToClipPos(inputVertex)); \
outputStruct.worldPos = mul(unity_ObjectToWorld, inputVertex)

#define BLOOM_FOG_SAMPLE(fogData) \
tex2D(_BloomPrePassTexture, fogData.fogCoord)

// frag: BLOOM_FOG_APPLY(i, col, _FogStartOffset, _FogScale);
#define BLOOM_FOG_APPLY(fogData, col, fogStartOffset, fogScale) \
float3 fogDistance = fogData.worldPos + -_WorldSpaceCameraPos; \
float4 fogCol = -float4(col.rgb, 1) + tex2D(_BloomPrePassTexture, fogData.fogCoord.xy / fogData.fogCoord.ww); \
float4 fogCol = -float4(col.rgb, 1) + BLOOM_FOG_SAMPLE(fogData); \
fogCol.a = -col.a; \
col = col + ((GetFogIntensity(fogDistance, fogStartOffset, fogScale) + 1) * fogCol)

// frag: BLOOM_HEIGHT_FOG_APPLY(i, col, _FogStartOffset, _FogScale, _FogHeightOffset, _FogHeightScale);
#define BLOOM_HEIGHT_FOG_APPLY(fogData, col, fogStartOffset, fogScale, fogHeightOffset, fogHeightScale) \
float3 fogDistance = fogData.worldPos + -_WorldSpaceCameraPos; \
float4 fogCol = -float4(col.rgb, 1) + tex2D(_BloomPrePassTexture, fogData.fogCoord.xy / fogData.fogCoord.ww); \
float4 fogCol = -float4(col.rgb, 1) + BLOOM_FOG_SAMPLE(fogData); \
fogCol.a = -col.a; \
col = col + (((GetHeightFogIntensity(fogData.worldPos, fogHeightOffset, fogHeightScale) * GetFogIntensity(fogDistance, fogStartOffset, fogScale)) + 1) * fogCol)

#else

#define BLOOM_FOG_COORDS(fogCoordIndex, worldPosIndex)
#define BLOOM_FOG_SURFACE_INPUT
#define BLOOM_FOG_TRANSFER(outputStruct, inputVertex)

#define BLOOM_FOG_INITIALIZE(outputStruct, inputVertex)
#define BLOOM_FOG_APPLY(fogData, col, fogStartOffset, fogScale)

#define BLOOM_HEIGHT_FOG_APPLY(fogData, col, fogStartOffset, fogScale, fogHeightOffset, fogHeightScale)

#endif
Expand Down
3 changes: 0 additions & 3 deletions CustomTonemapping.cginc
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#ifndef CUSTOM_TONEMAPPING_CG_INCLUDED
#define CUSTOM_TONEMAPPING_CG_INCLUDED

// #pragma multi_compile __ ACES_TONE_MAPPING

#if ACES_TONE_MAPPING

// frag: ACES_TONE_MAPPING_APPLY(col);
#define ACES_TONE_MAPPING_APPLY(col) \
float3 shoulderLinearCol = col.rgb * 2.50999999 + 0.0299999993; \
shoulderLinearCol = col.rgb * shoulderLinearCol; \
Expand Down
2 changes: 1 addition & 1 deletion KGlow.shader
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
BLOOM_FOG_TRANSFER(o, v.vertex);
BLOOM_FOG_INITIALIZE(o, v.vertex);
return o;
}

Expand Down
2 changes: 1 addition & 1 deletion KStandard.shader
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

void vert (inout appdata_full v, out Input data) {
UNITY_INITIALIZE_OUTPUT(Input, data);
BLOOM_FOG_TRANSFER(data, v.vertex);
BLOOM_FOG_INITIALIZE(data, v.vertex);
}

void fogcolor (Input IN, SurfaceOutput o, inout fixed4 color) {
Expand Down
2 changes: 1 addition & 1 deletion KStandardLighting.shader
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

void vert (inout appdata_full v, out Input data) {
UNITY_INITIALIZE_OUTPUT(Input, data);
BLOOM_FOG_TRANSFER(data, v.vertex);
BLOOM_FOG_INITIALIZE(data, v.vertex);
}

void fogcolor (Input IN, SurfaceOutput o, inout fixed4 color) {
Expand Down
2 changes: 1 addition & 1 deletion KTexture.shader
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Shader "Unlit/KTexture" {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
BLOOM_FOG_TRANSFER(o, v.vertex);
BLOOM_FOG_INITIALIZE(o, v.vertex);
return o;
}

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Unlit Shader Usage:
- Include the CGINC in your shader, example: `#include "BloomFog.cginc"`
- Add `#pragma multi_compile __ ENABLE_BLOOM_FOG` in your shader to also build for `ENABLE_BLOOM_FOG`.
- In your `v2f`, add `BLOOM_FOG_COORDS(1, 2)`. (change `1` and `2` to an empty TEXCOORD number if TEXCOORD1/2 is populated)
- In your vertex function, at the end of it, add `BLOOM_FOG_TRANSFER(o, v.vertex);` right before the return.
- In your vertex function, at the end of it, add `BLOOM_FOG_INITIALIZE(o, v.vertex);` right before the return.
- In your fragment function, at the end of it, add `BLOOM_FOG_APPLY(i, col, 0.0, 5.0);` right before the return.
- Done, your shader should now handle Beat Saber's Bloom Fog correctly!

Expand All @@ -20,14 +20,14 @@ Shader Examples:
## CustomLighting.cginc
Allows you to use Beat Saber's custom directional lighting (max of 5 directional lights), useful if you want to make your objects reactive with the environment lighting.

Notes:
Info:
- This is experimental, and is not 100% finished yet.
- Only works on surface shaders, not unlit shaders.

Shader Examples:
- Bloom Fog + Surface Specular Custom Lighting (`KStandardLighting.shader`)

# Various Notes
# Various Infos
If you're using Bloom Fog/Height Fog in your custom notes, you should consider using the following values for Bloom Fog/Height Fog:
- `_FogHeightOffset`: 0
- `_FogHeightScale`: 2.5
Expand Down

0 comments on commit e553ef9

Please sign in to comment.