Skip to content

Instantly share code, notes, and snippets.

@MilkyDeveloper
Created September 18, 2020 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MilkyDeveloper/46d967924f89f9486bebca914b5b5b8f to your computer and use it in GitHub Desktop.
Save MilkyDeveloper/46d967924f89f9486bebca914b5b5b8f to your computer and use it in GitHub Desktop.
Code Snippets for MCBE Shader
// Desaturation Algorithm from Photoshop with an extra touch...
// A blue color multiplier, so psDesaturate(diffuse.rgb, 1.2)
// so it'll be more blue or "gloomy greyscale"
float3 psDesaturate( float3 color, float blue )
{
float bw = (min(color.r, min(color.g, color.b)) + max(color.r, max(color.g, color.b))) * 0.5;
return float3(bw, bw, bw*blue);
}
// Detection Code
float sunset = pow(clamp(1.0 - PSInput.fogClr.b * 1.7, 0.0, 1.0), 0.3);
float night = pow(clamp(1.0 - PSInput.fogClr.r * 1.5, 0.0, 1.0), 2.0);
float rain = smoothstep(0.8, 1.0, PSInput.fogControl.y); // Returns 0 for rainy weather
float4 tex1 = TEXTURE_1.Sample(TextureSampler1, float2(0.0,1.0));
float terrainDay = clamp((tex1.r-.5)*2.,.0,1.);
float terrainSunset = 2.*(.5-abs(.5-clamp((clamp((tex1.r-.5)*2.,.0,1.)-.4)*3.,0.,1.)));
float terrainCave = 1.0-PSInput.uv1.y;
float terrainNight = 1.0-clamp((tex1.r-.5)*2.,.0,1.);
// Sun Color
float3 sunColor = float3(253., 184., 19.)/float3(225., 165., 16.5);
if (sunset == 1.0) { sunColor.rgb = float3(sunColor.r/1.75, sunColor.g/1.40, sunColor.b/2.25); } else { sunColor = float3(253., 184., 19.)/float3(225., 165., 16.5); }
// High-def loop shadows
for(float i=0.;i<12.;i++) { if((PSInput.uv1.y<.875+i*.001)) {diffuse.rgb *= float3(1.-(i*.0075), 1.-(i*.0075), 1.-(i*.0075));} }
// Directional Lighting
float3 dirLight1 = normalize(cross(ddx(PSInput.wpos), ddy(PSInput.wpos)));
float3 dirLight2 = normalize(cross(ddx(PSInput.posi), ddy(PSInput.posi)));
float3 dirLight3 = normalize(cross(ddx(PSInput.posi.zyx), ddy(PSInput.posi.zyx))); // dirLight3.x is reccomended
float3 dirMain = lerp(float3(1.,1.,1.), float3(1.25,1.25,0.6), max(0.0, dirLight3.x)); // Main Directional Light
float3 dirMain2 = lerp(float3(1.,1.,1.), float3(1.15,1.15,0.65), max(0.0, dirLight2.z)); // Secondary Directional Light
float3 dirSunset = lerp(float3(1.,1.,1.), float3(1.4,1.4,0.4) , max(0.0, dirLight3.x)); // Directional light for Sunset / Sunrise
// Make sure we're not in a cave (no light sources)
if (terrainCave < 0.1 && terrainNight < 0.1) {
diffuse.rgb *= lerp(float3(1.,1.,1.), dirMain, terrainDay);
diffuse.rgb *= dirMain2;
diffuse.rgb *= lerp(float3(1.,1.,1.), dirSunset, terrainSunset);
}
// 👇 Is sucky directional lighting that overexposes everything
// I've hung on to it for a while but it's ineffecient and only looks good in a few scenarios
// 👆 Is true directional lighting
/*
// Directional Lights and Light Leaks (lightmap stuff)
#if defined(BLEND)
// Do nothing if it's a biome-blended block
#else
if (PSInput.color.g>0.60) { diffuse.rgb *= lerp(float3(1.110, 1.110, 1.075), sunColor, 0.3); }
if (PSInput.color.g>0.61) { diffuse.rgb *= lerp(float3(1.120, 1.120, 1.075), sunColor, 0.3); }
if (PSInput.color.g>0.62) { diffuse.rgb *= lerp(float3(1.125, 1.125, 1.075), sunColor, 0.3); }
if (PSInput.color.g>0.63) { diffuse.rgb *= lerp(float3(1.130, 1.130, 1.075), sunColor, 0.3); }
if (PSInput.color.g>0.64) { diffuse.rgb *= lerp(float3(1.135, 1.135, 1.075), sunColor, 0.3); }
if (PSInput.color.g>0.80) { diffuse.rgb *= lerp(float3(1.090, 1.090, 1.055), sunColor, 0.3); }
if (PSInput.color.g>0.81) { diffuse.rgb *= lerp(float3(1.100, 1.100, 1.055), sunColor, 0.3); }
if (PSInput.color.g>0.82) { diffuse.rgb *= lerp(float3(1.115, 1.115, 1.055), sunColor, 0.3); }
if (PSInput.color.g>0.83) { diffuse.rgb *= lerp(float3(1.120, 1.120, 1.055), sunColor, 0.3); }
if (PSInput.color.g>0.84) { diffuse.rgb *= lerp(float3(1.135, 1.125, 1.055), sunColor, 0.3); }
#endif
if (terrainCave < 0.1 || terrainNight < 0.1) {
diffuse.rgb += diffuse.rgb*(float3(1.5, 0.65, 0.0)*1.15)*pow(PSInput.uv1.x*1.2,4.5)*lerp(clamp(PSInput.rpos.x, 0.0, 2.5)/2., clamp(PSInput.rpos.z, 0.0, 2.5)/2., 0.5);
} else {
diffuse.rgb += diffuse.rgb*(float3(1.5, 0.65, 0.0)*1.15)*pow(PSInput.uv1.x*1.8,2.5)*lerp(clamp(PSInput.rpos.x, 0.0, 2.5)/2., clamp(PSInput.rpos.z, 0.0, 2.5)/2., 0.5);
}
// Sun Reflection on water :D
// Cool one liner
diffuse.rgb += lerp(0., clamp(1.0-length(-PSInput.wpos.z*10.)/PSInput.renderDistance*1., 0., 1.)*sunReflectionColor.rgb*length(-PSInput.wpos.x)/PSInput.renderDistance*1., sunset);
// Basic Sunbeams
float sunBeam = pow(sin(atan2(PSInput.wpos.y / 3., PSInput.wpos.z) / (3.141532 * 2.) * 75.1), 3.75) * 4.75;
float beamFar = clamp2(abs(-PSInput.wpos.x) / (PSInput.renderDistance / 1.6));
sunBeam = smoothstep(1., .2, length(PSInput.screenPos * PSInput.screenPos * PSInput.screenPos)) * lerp(clamp2(sunBeam) * smoothstep(.2, 1., length(PSInput.wpos.yz) / 16.), 1., beamFar * beamFar * beamFar);
if (sunBeam >= 0.8) { diffuse.rgb = lerp(diffuse.rgb, aSat(lerp(PSInput.fogClr.rgb, thiccFog.rgb, 0.5) * 1.6, 1.2) * sunBeam, clamp(pow(clamp(1.0 - PSInput.fogClr.b * 1.7, 0.0, 1.0), 0.3) / 2.0, 0.0, 0.6)); }
// Caves should be desaturated and bleh
diffuse.rgb = lerp(diffuse.rgb, diffuse.rgb / float3(7.0, 7.0, 9.0), terrainCave);
// So should the night
diffuse.rgb = lerp(diffuse.rgb, contrastFilter(diffuse.rgb / float3(4.5, 3.5, 3.5), 0.975), terrainNight);
// Last but not least the rain
diffuse.rgb = lerp(lerp(diffuse.rgb/2.5, psDesaturate(diffuse.rgb, 1.2), 0.5), diffuse.rgb, rain);
float3 thiccFog2 = lerp(diffuse.rgb, PSInput.fogClr.rgb*float3(1.3, 1.3, 0.7), clamp(length(-PSInput.wpos) / PSInput.renderDistance * 1.5, 0.0, 1.0)); // Fog everywhere
float3 thiccFog3 = lerp(diffuse.rgb, PSInput.fogClr.rgb*float3(0.7, 1.3, 1.3), clamp(length(-PSInput.wpos) / PSInput.renderDistance * 8.5, 0.0, 1.0)); // Cave fog
float3 thiccFog4 = lerp(diffuse.rgb, PSInput.fogClr.rgb*float3(0.7, 1.3, 1.3), clamp(length(-PSInput.wpos) / PSInput.renderDistance * 3.5, 0.0, 1.0)); // Night Fog, make it playable
if (PSInput.renderDistance < 40.) { diffuse.rgb = thiccFog2; } // No fog on big builds
// Cave fog ooga booga
diffuse.rgb = lerp(diffuse.rgb, thiccFog3, terrainCave);
// Night fog hoooot
diffuse.rgb = lerp(diffuse.rgb, thiccFog4, terrainNight);
// Cool border stuff
// Needs tuning, I shoud scrap it
// The effect is kinda cool though
/* float borderOrig = TEXTURE_0.Sample(TextureSampler0, PSInput.uv0);
float borderOffset = TEXTURE_0.Sample(TextureSampler0, PSInput.uv0 - 0.001);
if (length(borderOrig - borderOffset) > 0.25) {
//diffuse.rgb = float3(0.,0.,0.);
diffuse.rgb *= smoothstep(clamp(PSInput.rpos.xyz / 1.5, 0.8, 1.2), float3(1.15, 1.15, 1.15), lerp(clamp(PSInput.rpos.xyz / 1.5, 0.8, 1.2), float3(1.15, 1.15, 1.15), 0.5));
} */
// Also look at Natural Mystic, way more cool.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment