Skip to content

Instantly share code, notes, and snippets.

@aras-p
Created August 18, 2014 09:13
Show Gist options
  • Save aras-p/ac4339c040afabea6ee7 to your computer and use it in GitHub Desktop.
Save aras-p/ac4339c040afabea6ee7 to your computer and use it in GitHub Desktop.
fog macros WIP
// ------------------------------------------------------------------
// Fog helpers
//
// multi_compile_fog Will compile fog variants.
// UNITY_FOG_COORDS(texcoordindex) Declares the fog data interpolator.
// UNITY_TRANSFER_FOG(outputStruct,clipspacePos) Outputs fog data from the vertex shader.
// UNITY_APPLY_FOG(fogData,col) Applies fog to color "col". Automatically applies black fog when in forward-additive pass.
// Can also use UNITY_APPLY_FOG_COLOR to supply your own fog color.
// In case someone by accident tries to compile fog code in one of the g-buffer or shadow passes:
// treat it as fog is off.
#if defined(UNITY_PASS_PREPASSBASE) || defined(UNITY_PASS_DEFERRED) || defined(UNITY_PASS_SHADOWCASTER)
#undef FOG_LINEAR
#undef FOG_EXP
#undef FOG_EXP2
#endif
#if defined(FOG_LINEAR) || defined(FOG_EXP) || defined(FOG_EXP2)
#define UNITY_FOG_COORDS(idx) float fogCoord : TEXCOORD##idx;
#define UNITY_TRANSFER_FOG(o,outpos) o.fogCoord = (outpos).z
#else
#define UNITY_FOG_COORDS(idx)
#define UNITY_TRANSFER_FOG(o,outpos)
#endif
#define UNITY_FOG_LERP_COLOR(col,fogCol,fogFac) col.rgb = lerp(fogCol.rgb, col.rgb, saturate(fogFac))
#if defined(FOG_LINEAR)
// factor = (end-z)/(end-start) = z * (-1/(end-start)) + (end/(end-start))
#define UNITY_APPLY_FOG_COLOR(coord,col,fogCol) float fogFac = coord * unity_FogParams.z + unity_FogParams.w; UNITY_FOG_LERP_COLOR(col,fogCol,fogFac)
#elif defined(FOG_EXP)
// factor = exp(-density*z)
#define UNITY_APPLY_FOG_COLOR(coord,col,fogCol) float fogFac = unity_FogParams.y * coord; fogFac = exp2(-fogFac); UNITY_FOG_LERP_COLOR(col,fogCol,fogFac)
#elif defined(FOG_EXP2)
// factor = exp(-(density*z)^2)
#define UNITY_APPLY_FOG_COLOR(coord,col,fogCol) float fogFac = unity_FogParams.x * coord; fogFac = exp2(-fogFac*fogFac); UNITY_FOG_LERP_COLOR(col,fogCol,fogFac)
#else
#define UNITY_APPLY_FOG_COLOR(coord,col,fogCol)
#endif
#ifdef UNITY_PASS_FORWARDADD
#define UNITY_APPLY_FOG(coord,col) UNITY_APPLY_FOG_COLOR(coord,col,fixed4(0,0,0,0))
#else
#define UNITY_APPLY_FOG(coord,col) UNITY_APPLY_FOG_COLOR(coord,col,unity_FogColor)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment