copmpsite - advance opengl shader
TRANSCRIPT
-
8/12/2019 copmpsite - Advance OpenGL Shader
1/34
const int shadowMapResolution = 2048;const float shadowDistance = 120.0f;const bool generateShadowMipmap = false;const float shadowIntervalSize = 4.0f;const bool shadowHardwareFiltering = true;
const int R8 = 0;const int RG8 = 0;const int RGB8 = 1;const int RGB16 = 2;const int gcolorFormat = RGB16;const int gdepthFormat = RGB8;const int gnormalFormat = RGB16;const int compositeFormat = RGB8;
const float eyeBrightnessHalflife = 10.0f;const float centerDepthHalflife = 2.0f;const float wetnessHalflife = 200.0f;const float drynessHalflife = 10.0f;
const int superSamplingLevel = 0;
const float sunPathRotation = -40.0f;const float ambientOcclusionLevel = 0.019f;
const int noiseTextureResolution = 64;
//END OF INTERNAL VARIABLES//
#define BANDING_FIX_FACTOR 1.0f
uniform sampler2D gcolor;
uniform sampler2D gdepth;uniform sampler2D gdepthtex;uniform sampler2D gnormal;uniform sampler2D composite;uniform sampler2DShadow shadow;uniform sampler2D noisetex;uniform sampler2D shadowcolor;//uniform sampler2D gaux1;//uniform sampler2D gaux2;//uniform sampler2D gaux3;//uniform sampler2D gaux4;
varying vec4 texcoord;
varying vec3 lightVector;varying vec3 upVector;
uniform int worldTime;
uniform mat4 gbufferProjection;uniform mat4 gbufferProjectionInverse;uniform mat4 gbufferModelViewInverse;uniform mat4 gbufferModelView;uniform mat4 shadowProjectionInverse;
-
8/12/2019 copmpsite - Advance OpenGL Shader
2/34
uniform mat4 shadowProjection;uniform mat4 shadowModelView;uniform mat4 shadowModelViewInverse;uniform vec3 sunPosition;uniform vec3 cameraPosition;uniform vec3 upPosition;
uniform float near;uniform float far;uniform float viewWidth;uniform float viewHeight;uniform float rainStrength;uniform float wetness;uniform float aspectRatio;uniform float frameTimeCounter;
uniform int isEyeInWater;uniform float eyeAltitude;uniform ivec2 eyeBrightness;uniform ivec2 eyeBrightnessSmooth;uniform int fogMode;
varying float timeSunrise;
varying float timeNoon;varying float timeSunset;varying float timeMidnight;varying float timeSkyDark;
varying vec3 colorSunlight;varying vec3 colorSkylight;varying vec3 colorSunglow;varying vec3 colorBouncedSunlight;varying vec3 colorScatteredSunlight;varying vec3 colorTorchlight;varying vec3 colorWaterMurk;varying vec3 colorWaterBlue;
/////////////////////////FUNCTIONS//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////FUNCTIONS/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Get gbuffer textures
vec3 GetAlbedoLinear(in vec2 coord) { //Function thatretrieves the diffuse texture and convert it into linear space.
return pow(texture2D(gcolor, coord).rgb, vec3(2.2f));}
vec3 GetAlbedoGamma(in vec2 coord) { //Function that retrieves the diffuse texture and leaves it in gamma space.
return texture2D(gcolor, coord).rgb;}
-
8/12/2019 copmpsite - Advance OpenGL Shader
3/34
vec3 GetNormals(in vec2 coord) { //Function thatretrieves the screen space surface normals. Used for lighting calculations
return texture2D(gnormal, texcoord.st).rgb * 2.0f - 1.0f;}
float GetDepth(in vec2 coord) { //Function that retrieves the scene depth. 0 - 1, higher values meaning farther away
return texture2D(gdepthtex, coord).r;}
float GetDepthLinear(in vec2 coord) { //Function that retrieves the scene depth. 0 - 1, higher values meaning farther away
return 2.0f * near * far / (far + near - (2.0f * texture2D(gdepthtex, coord).x - 1.0f) * (far - near));}
float ExpToLinearDepth(in float depth){
return 2.0f * near * far / (far + near - (2.0f * depth - 1.0f) * (far -near));}
//Lightmaps
float GetLightmapTorch(in vec2 coord) { //Function thatretrieves the lightmap of light emitted by emissive blocks like torches and lavafloat lightmap = texture2D(gdepth, coord).g;
//Apply inverse square law and normalize for natural light fallofflightmap = clamp(lightmap * 1.22f, 0.0f, 1.0f);lightmap = 1.0f - lightmap;lightmap *= 5.6f;lightmap = 1.0f / pow((lightmap + 0.2f), 2.0f);lightmap -= 0.02975f;
// if (lightmap
-
8/12/2019 copmpsite - Advance OpenGL Shader
4/34
s how reflective any surface/pixel is in the scene. Used for reflections and specularity
return texture2D(composite, texcoord.st).r;}
float GetGlossiness(in vec2 coord) { //Function that retrieves how reflective any surface/pixel is in the scene. Used for reflections and specularity
return texture2D(composite, texcoord.st).g;}
//Material IDsfloat GetMaterialIDs(in vec2 coord) { //Function that retrieves the texture that has all material IDs stored in it
return texture2D(gdepth, coord).r;}
bool GetSky(in vec2 coord) { //Function thatreturns true for any pixel that is part of the sky, and false for any pixel thatisn't part of the sky
float matID = GetMaterialIDs(coord); //Gets texture that hasall material IDs stored in it
matID = floor(matID * 255.0f); //Scale texturefrom 0-1 float to 0-255 integer format
if (matID == 0.0f) { //Checksto see if the current pixel's material ID is 0 = the sky
return true;//If the current pixel has the material ID of 0 (sky material ID), Return "thispixel is part of the sky"
} else {return false;
//Return "this pixel is not part of the sky"}
}
bool GetMaterialMask(in vec2 coord, in int ID, in float matID) { matID = floor(matID * 255.0f);
//Catch last part of skyif (matID > 254.0f) {
matID = 0.0f;}
if (matID == ID) {return true;
} else {return false;
}}
//Waterfloat GetWaterTex(in vec2 coord) { //Function thatreturns the texture used for water. 0 means "this pixel is not water". 0.5 and greater means "this pixel is water".
-
8/12/2019 copmpsite - Advance OpenGL Shader
5/34
return texture2D(composite, coord).b; //values from 0.5 to 1.0represent the amount of sky light hitting the surface of the water. It is usedto simulate fake sky reflections in composite1.fsh}
bool GetWaterMask(in vec2 coord, in float matID) {//Function that returns "true" if a pixel is water, and "false" if a pixel is not water.
matID = floor(matID * 255.0f);
if (matID >= 35.0f && matID
-
8/12/2019 copmpsite - Advance OpenGL Shader
6/34
worldPosition.xyz -= cameraPosition;worldPosition = gbufferModelView * worldPosition;return worldPosition;
}
void DoNightEye(inout vec3 color) { //Desaturates any colorinput at night, simulating the rods in the human eye
float amount = 0.8f; //How much will the new desaturated and tinted image be mixed with the original image
vec3 rodColor = vec3(0.2f, 0.5f, 1.0f); //Cyan color that humanspercieve when viewing extremely low light levels via rod cells in the eye
float colorDesat = dot(color, vec3(1.0f)); //Desaturated color
color = mix(color, vec3(colorDesat) * rodColor, timeSkyDark * amount);//color.rgb = color.rgb;
}
float ExponentialToLinearDepth(in float depth){
vec4 worldposition = vec4(depth);
worldposition = gbufferProjection * worldposition;return worldposition.z;}
void DoLowlightEye(inout vec3 color) { //Desaturates any color input at night, simulating the rods in the human eye
// float amount = 0.8f;//How much will the new desaturated and tinted image be mixed with the originalimage
// vec3 rodColor = vec3(0.2f, 0.5f, 1.0f); //Cyan color that humanspercieve when viewing extremely low light levels via rod cells in the eye
// float colorDesat = dot(color, vec3(1.0f)); //Desaturated color
// color = mix(color, vec3(colorDesat) * rodColor, amount);color.rgb = color.rgb;
}
void FixLightFalloff(inout float lightmap) { //Fixes the ugly lightmap falloff and creates a nice linear one
float additive = 5.35f;float exponent = 40.0f;
lightmap += additive;//Prevent ugly fast falloff
lightmap = pow(lightmap, exponent); //Curve light fa
llofflightmap = max(0.0f, lightmap); //Make sure light properly falls
off to zerolightmap /= pow(1.0f + additive, exponent);
}
float CalculateLuminance(in vec3 color) {return (color.r * 0.2126f + color.g * 0.7152f + color.b * 0.0722f);
}
-
8/12/2019 copmpsite - Advance OpenGL Shader
7/34
vec3 Glowmap(in vec3 albedo, in bool mask, in float curve, in vec3 emissiveColor) {
vec3 color = albedo * float(mask);color = pow(color, vec3(curve));color = vec3(CalculateLuminance(color));color *= emissiveColor;
return color;}
float ChebyshevUpperBound(in vec2 moments, in float distance) {if (distance
-
8/12/2019 copmpsite - Advance OpenGL Shader
8/34
vec2 count = vec2(0.0f); count.x = floor(mod(texcoord.s * viewWidth, 8.0f));
count.y = floor(mod(texcoord.t * viewHeight, 8.0f));
int dither = ditherPattern[int(count.x) + int(count.y) * 8];
return float(dither) / 65.0f;}
vec3 CalculateNoisePattern1(vec2 offset, float size) {vec2 coord = texcoord.st;
coord *= vec2(viewWidth, viewHeight);coord = mod(coord + offset, vec2(size));coord /= noiseTextureResolution;
return texture2D(noisetex, coord).xyz;}
void DrawDebugSquare(inout vec3 color) {
vec2 pix = vec2(1.0f / viewWidth, 1.0f / viewHeight);
vec2 offset = vec2(0.5f);vec2 size = vec2(0.0f);
size.x = 1.0f / 2.0f;size.y = 1.0f / 2.0f;
vec2 padding = pix * 0.0f;size += padding;
if ( texcoord.s + offset.s / 2.0f + padding.x / 2.0f > offset.s &&texcoord.s + offset.s / 2.0f + padding.x / 2.0f < offset.s + si
ze.x &&texcoord.t + offset.t / 2.0f + padding.y / 2.0f > offset.t &&
texcoord.t + offset.t / 2.0f + padding.y / 2.0f < offset.t + size.y)
{
int[16] ditherPattern = int[16] (0, 3, 0, 3,
2, 1, 2, 1,
0, 3, 0, 3,
2, 1, 2, 1);
vec2 count = vec2(0.0f); count.x = floor(mod(texcoord.s * viewWidth, 4.0f));
count.y = floor(mod(texcoord.t * viewHeight, 4.0f));
int dither = ditherPattern[int(count.x) + int(count.y) * 4];color.rgb = vec3(float(dither) / 3.0f);
}
-
8/12/2019 copmpsite - Advance OpenGL Shader
9/34
}
/////////////////////////STRUCTS////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////STRUCTS///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct MCLightmapStruct { //Lightmaps directly from MC enginefloat torch; //Light emitted from torches and
other emissive blocksfloat sky; //Light coming from the
skyfloat lightning; //Light coming from lightning
vec3 torchVector; //Vector in screen space that represents the direction of average light transfered
vec3 skyVector;} mcLightmap;
struct DiffuseAttributesStruct { //Diffuse surface shadin
g attributesfloat roughness; //Roughness of surface. More roughness will use Oren Nayar reflectance.
float translucency; //How translucent the surface is. Translucency represents how much energy will be transfered through the surface
vec3 translucencyColor; //Color that will be multiplied with sunlight for backsides of translucent materials.};
struct SpecularAttributesStruct { //Specular surface shading attributes
float specularity; //How reflective a surface isfloat extraSpecularity; //Additional reflectance for specular re
flections from sun onlyfloat glossiness; //How smooth or rough a specularsurface is
float metallic; //from 0 - 1. 0 representing non-metallic, 1 representing fully metallic.
float gain; //Adjust specularity further
float base; //Reflectance when the camera is facing directly at the surface normal. 0 allows only the fresnel effectto add specularity
float fresnelPower; //Curve of fresnel effect. Higher valuesmean the surface has to be viewed at more extreme angles to see reflectance};
struct SkyStruct { //All sky shading attributesvec3 albedo; //Diffuse texture aka "color tex
ture" of the skyvec3 tintColor; //Color that will be multiplied
with the sky to tint itvec3 sunglow; //Color that will be added to th
e sky simulating scattered light arond the sun/moonvec3 sunSpot; //Actual sun surface
};
-
8/12/2019 copmpsite - Advance OpenGL Shader
10/34
struct WaterStruct {vec3 albedo;
};
struct MaskStruct {
float matIDs;
bool sky;bool land;bool grass;bool leaves;bool ice;bool hand;bool translucent;bool glow;bool sunspot;bool goldBlock;bool ironBlock;bool diamondBlock;bool emeraldBlock;bool sand;bool sandstone;
bool stone;bool cobblestone;bool wool;bool clouds;
bool torch;bool lava;bool glowstone;bool fire;
bool water;
bool volumeCloud;
};
struct CloudsStruct {vec3 albedo;
};
struct AOStruct {float skylight;float scatteredUpLight;float bouncedSunlight;float scatteredSunlight;float constant;
};
struct SurfaceStruct { //Surface shading properties, attributes, and functions
//Attributes that change how shading is applied to each pixelDiffuseAttributesStruct diffuse; //Contai
ns all diffuse surface attributesSpecularAttributesStruct specular; //Contai
ns all specular surface attributes
-
8/12/2019 copmpsite - Advance OpenGL Shader
11/34
SkyStruct sky; //Sky shading attributesand properties
WaterStruct water; //Water shading attributes and properties
MaskStruct mask; //Material ID MasksCloudsStruct clouds;AOStruct ao; //ambient occlus
ion
//Properties that are required for lighting calculationvec3 albedo; //Diffuse textur
e aka "color texture"vec3 normal; //Screen-space s
urface normalsfloat depth; //Scene depthfloat linearDepth; //Linear depth
vec4 screenSpacePosition; //Vector representing the screen-space position of the surface
vec3 viewVector; //Vector representing the viewing direction
vec3 lightVector; //Vector representing sunlight direction
vec3 upVector; //Vector representing "up" directionfloat NdotL; //dot(normal, li
ghtVector). used for direct lighting calculationvec3 debug;
float shadow;} surface;
struct LightmapStruct { //Lighting information to light the scene. These are untextured colored lightmaps to be multiplied with albedo to get the final lit and textured image.
vec3 sunlight; //Direct light from the sun
vec3 skylight; //Ambient light from the skyvec3 bouncedSunlight; //Fake bounced light, coming from opposite of sun direction and adding to ambient light
vec3 scatteredSunlight; //Fake scattered sunlight, coming from same direction as sun and adding to ambient light
vec3 scatteredUpLight; //Fake GI from groundvec3 torchlight; //Light emitted from torches and
other emissive blocksvec3 lightning; //Light caused by lightningvec3 nolight; //Base ambient light added to ev
erything. For lighting caves so that the player can barely see even when no lights are present
vec3 specular; //Reflected direct light from su
nvec3 translucent; //Light on the backside of objec
ts representing thin translucent materialsvec3 sky; //Color and brightness o
f the sky itselfvec3 underwater; //underwater lightmap
} lightmap;
struct ShadingStruct { //Shading calculation variablesfloat direct;
-
8/12/2019 copmpsite - Advance OpenGL Shader
12/34
float waterDirect;float bounced; //Fake bounced sunlightfloat skylight; //Light coming from skyfloat scattered; //Fake scattered sunlightfloat scatteredUp; //Fake GI from groundfloat specular; //Reflected direct lightfloat translucent; //Backside of objects lit up from the su
n via thin translucent materialsfloat sunlightVisibility; //Shadows
} shading;
struct GlowStruct {vec3 torch;vec3 lava;vec3 glowstone;vec3 fire;
};
struct FinalStruct { //Final textured and lit images sorted by what is illuminating them.
GlowStruct glow; //Struct containing emissive material final images
vec3 sunlight; //Direct light from the sunvec3 skylight; //Ambient light from the skyvec3 bouncedSunlight; //Fake bounced light, coming from opposi
te of sun direction and adding to ambient lightvec3 scatteredSunlight; //Fake scattered sunlight, coming from s
ame direction as sun and adding to ambient lightvec3 scatteredUpLight; //Fake GI from groundvec3 torchlight; //Light emitted from torches and
other emissive blocksvec3 lightning; //Light caused by lightningvec3 nolight; //Base ambient light added to ev
erything. For lighting caves so that the player can barely see even when no lights are present
vec3 translucent; //Light on the backside of objects representing thin translucent materialsvec3 sky; //Color and brightness o
f the sky itselfvec3 underwater; //underwater colors
} final;
/////////////////////////STRUCT FUNCTIONS///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////STRUCT FUNCTIONS//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Maskvoid CalculateMasks(inout MaskStruct mask) {
mask.sky = GetMaterialMask(texcoord.st, 0, mask.matIDs);
mask.land = GetMaterialMask(texcoord.st, 1
-
8/12/2019 copmpsite - Advance OpenGL Shader
13/34
, mask.matIDs);mask.grass = GetMaterialMask(texcoord.st, 2, mask.m
atIDs);mask.leaves = GetMaterialMask(texcoord.st, 3
, mask.matIDs);mask.ice = GetMaterialMask(texcoord.st, 4
, mask.matIDs);mask.hand = GetMaterialMask(texcoord.st, 5
, mask.matIDs);mask.translucent = GetMaterialMask(texcoord.st, 6, mask.m
atIDs);
mask.glow = GetMaterialMask(texcoord.st, 10, mask.matIDs);
mask.sunspot = GetMaterialMask(texcoord.st, 11, mask.matIDs);
mask.goldBlock = GetMaterialMask(texcoord.st, 20, mask.matIDs);
mask.ironBlock = GetMaterialMask(texcoord.st, 21, mask.matIDs);
mask.diamondBlock = GetMaterialMask(texcoord.st, 22, mask.matIDs);
mask.emeraldBlock = GetMaterialMask(texcoord.st, 23, mask.
matIDs); mask.sand = GetMaterialMask(texcoord.st, 24, mask.matIDs);
mask.sandstone = GetMaterialMask(texcoord.st, 25, mask.matIDs);
mask.stone = GetMaterialMask(texcoord.st, 26, mask.matIDs);
mask.cobblestone = GetMaterialMask(texcoord.st, 27, mask.matIDs);
mask.wool = GetMaterialMask(texcoord.st, 28, mask.matIDs);
mask.clouds = GetMaterialMask(texcoord.st, 29, mask.matIDs);
mask.torch = GetMaterialMask(texcoord.st, 30, mask.matIDs);
mask.lava = GetMaterialMask(texcoord.st, 31, mask.matIDs);
mask.glowstone = GetMaterialMask(texcoord.st, 32, mask.matIDs);
mask.fire = GetMaterialMask(texcoord.st, 33, mask.matIDs);
mask.water = GetWaterMask(texcoord.st, mask.matIDs);
mask.volumeCloud = false;}
//Surfacevoid CalculateNdotL(inout SurfaceStruct surface) { //Calculates direct sunlight without visibility check
float direct = dot(surface.normal.rgb, surface.lightVector); direct = direct * 1.0f + 0.0f; //direct = clamp(direct, 0.0f, 1.0f);
-
8/12/2019 copmpsite - Advance OpenGL Shader
14/34
surface.NdotL = direct;}
float CalculateDirectLighting(in SurfaceStruct surface) {
//Tall grass translucent shadingif (surface.mask.grass) {
return 1.0f;
//Leaves} else if (surface.mask.leaves) {
// if (surface.NdotL > -0.01f) {// return surface.NdotL * 0.99f + 0.01f;// } else {// return abs(surface.NdotL) * 0.25f;// }
return 1.0f;
//clouds} else if (surface.mask.clouds) {
return 0.5f;
//Default lambert shading} else {
return max(0.0f, surface.NdotL * 0.99f + 0.01f);}
}
float CalculateSunlightVisibility(inout SurfaceStruct surface, in ShadingStruct shadingStruct) { //Calculates shadows
if (rainStrength >= 0.99f)
return 1.0f;
if (shadingStruct.direct > 0.0f) {float distance = sqrt( surface.screenSpacePosition.x * surface.
screenSpacePosition.x //Get surface distance in meters + surface.screenSpaceP
osition.y * surface.screenSpacePosition.y+ surface.screenSpaceP
osition.z * surface.screenSpacePosition.z);
vec4 worldposition = vec4(0.0f);worldposition = gbufferModelViewInverse * surface.scree
nSpacePosition; //Transform from screen space to world space
float yDistanceSquared = worldposition.y * worldposition.y;
worldposition = shadowModelView * worldposition; //Transform from world space to shadow space
float comparedepth = -worldposition.z;//Surface distance from sun to be compared to the shadow map
worldposition = shadowProjection * worldposition;
-
8/12/2019 copmpsite - Advance OpenGL Shader
15/34
worldposition /= worldposition.w;
float dist = sqrt(worldposition.x * worldposition.x + worldposition.y * worldposition.y);
float distortFactor = (1.0f - SHADOW_MAP_BIAS) + dist * SHADOW_MAP_BIAS;
worldposition.xy *= 1.0f / distortFactor;worldposition = worldposition * 0.5f + 0.5f; //Transf
orm from shadow space to shadow map coordinates
float shadowMult = 0.0f;//Multiplier used to fade out shadows at distance
float shading = 0.0f;
if (distance < shadowDistance && comparedepth > 0.0f &&//Avoid computing shadows past the shadow map projection
worldposition.s < 1.0f && worldposition.s > 0.0f && worldposition.t < 1.0f && worldposition.t > 0.0f) {
float fademult = 0.15f;
shadowMult = clamp((shadowDistance * 0.85f * fademult) - (distance * fademult), 0.0f, 1.0f); //Calculate shadowMult to fade shadows out
float diffthresh = dist * 1.0f + 0.10f; diffthresh *= 3.0f / (shadowMapResolution / 2048.0f);
//diffthresh /= shadingStruct.direct + 0.1f;
#if defined ENABLE_SOFT_SHADOWS
int count = 0;float spread = 1.0f / shadowMapResolution;
for (float i = -1.0f; i
-
8/12/2019 copmpsite - Advance OpenGL Shader
16/34
return 0.0f;}
}
float CalculateBouncedSunlight(in SurfaceStruct surface) {
float NdotL = surface.NdotL;float bounced = clamp(-NdotL + 0.95f, 0.0f, 1.95f) / 1.95f;
bounced = bounced * bounced * bounced;
return bounced;}
float CalculateScatteredSunlight(in SurfaceStruct surface) {
float NdotL = surface.NdotL;float scattered = clamp(NdotL * 0.75f + 0.25f, 0.0f, 1.0f);
//scattered *= scattered * scattered;
return scattered;}
float CalculateSkylight(in SurfaceStruct surface) {
if (surface.mask.clouds) {return 1.0f;
} else if (surface.mask.leaves) {
return 1.0f;
} else if (surface.mask.grass) {
return 1.0f;
} else {
float skylight = dot(surface.normal, surface.upVector); skylight = skylight * 0.5f + 0.5f;
return skylight;}
}
float CalculateScatteredUpLight(in SurfaceStruct surface) {float scattered = dot(surface.normal, surface.upVector);
scattered = scattered * 0.5f + 0.5f; scattered = 1.0f - scattered;
return scattered;
}
float CalculateSunglow(in SurfaceStruct surface) {
float curve = 4.0f;
vec3 npos = normalize(surface.screenSpacePosition.xyz);vec3 halfVector2 = normalize(-surface.lightVector + npos);float factor = 1.0f - dot(halfVector2, npos);
-
8/12/2019 copmpsite - Advance OpenGL Shader
17/34
return factor * factor * factor * factor;}
float CalculateAntiSunglow(in SurfaceStruct surface) {
float curve = 4.0f;
vec3 npos = normalize(surface.screenSpacePosition.xyz);vec3 halfVector2 = normalize(surface.lightVector + npos);float factor = 1.0f - dot(halfVector2, npos);
return factor * factor * factor * factor;}
bool CalculateSunspot(in SurfaceStruct surface) {
//circular sunfloat curve = 1.0f;
vec3 npos = normalize(surface.screenSpacePosition.xyz);vec3 halfVector2 = normalize(-surface.lightVector + npos);
float sunProximity = 1.0f - dot(halfVector2, npos);
if (sunProximity > 0.96f) {return true;} else {
return false;}
//Sun based on matID
// if (surface.mask.sunspot)// return true;// else// return false;
}
void GetLightVectors(inout MCLightmapStruct mcLightmap, in SurfaceStruct surface) {
vec2 torchDiff = vec2(0.0f);torchDiff.x = GetLightmapTorch(texcoord.st) - GetLightmapTorch(
texcoord.st + vec2(1.0f / viewWidth, 0.0f));torchDiff.y = GetLightmapTorch(texcoord.st) - GetLightmapTorch(
texcoord.st + vec2(0.0f, 1.0f / viewWidth));
//torchDiff /= GetDepthLinear(texcoord.st);
mcLightmap.torchVector.x = torchDiff.x * 200.0f;
//mcLightmap.torchVector.x *= 1.0f - surface.viewVector.x;
mcLightmap.torchVector.y = torchDiff.y * 200.0f;
mcLightmap.torchVector.x = 1.0f;mcLightmap.torchVector.y = 0.0f;mcLightmap.torchVector.z = sqrt(1.0f - mcLightmap.torchVector.x * mcLigh
tmap.torchVector.x + mcLightmap.torchVector.y + mcLightmap.torchVector.y);
-
8/12/2019 copmpsite - Advance OpenGL Shader
18/34
float torchNormal = dot(surface.normal.rgb, mcLightmap.torchVector.rgb);
mcLightmap.torchVector.x = torchNormal;
//mcLightmap.torchVector = mcLightmap.torchVector * 0.5f + 0.5f;}
void AddSkyGradient(inout SurfaceStruct surface) {float curve = 4.0f;vec3 npos = normalize(surface.screenSpacePosition.xyz);vec3 halfVector2 = normalize(-surface.upVector + npos);float skyGradientFactor = dot(halfVector2, npos);float skyDirectionGradient = skyGradientFactor;
skyGradientFactor = pow(skyGradientFactor, curve);surface.sky.albedo *= mix(skyGradientFactor, 1.0f, clamp((0.12f - (timeN
oon * 0.1f)) + rainStrength, 0.0f, 1.0f));
vec3 skyBlueColor = vec3(0.15f, 0.35f, 1.0f) * 1.75f;skyBlueColor.g *= skyGradientFactor * 2.0f + 0.75f;skyBlueColor = mix(skyBlueColor, vec3(1.0f, 1.2f, 1.0f), vec3(timeSkyDar
k)); skyBlueColor *= mix(vec3(1.0f), vec3(1.0f, 1.0f, 0.5f), vec3(timeSunrise+ timeSunset));
float fade1 = clamp(skyGradientFactor - 0.12f, 0.0f, 0.2f) / 0.2f;vec3 color1 = vec3(1.0f, 1.5, 1.0f) * 0.5f;
color1 = mix(color1, vec3(1.0f, 0.25f, 0.0f), vec3(timeSunrise+ timeSunset));
surface.sky.albedo *= mix(skyBlueColor, color1, vec3(fade1));
float fade2 = clamp(skyGradientFactor - 0.18f, 0.0f, 0.2f) / 0.2f;vec3 color2 = vec3(1.7f, 1.0f, 0.8f);
color2 = mix(color2, vec3(1.0f, 0.15f, 0.0f), vec3(timeSunrise+ timeSunset));
surface.sky.albedo *= mix(vec3(1.0f), color2, vec3(fade2 * 0.5f));
float horizonGradient = 1.0f - distance(skyDirectionGradient, 0.72f) / 0.72f;
horizonGradient = pow(horizonGradient, 10.0f); horizonGradient = max(0.0f, horizonGradient);
float sunglow = CalculateSunglow(surface);
horizonGradient *= sunglow * 2.0f + (0.65f - timeSunrise * 0.55f - timeSunset * 0.55f);
vec3 horizonColor1 = vec3(1.5f, 1.5f, 1.5f);horizonColor1 = mix(horizonColor1, vec3(1.5f, 1.95f, 0.5f) * 2.
0f, vec3(timeSunrise + timeSunset));vec3 horizonColor2 = vec3(1.5f, 1.2f, 0.8f) * 1.0f;
horizonColor2 = mix(horizonColor2, vec3(1.9f, 0.6f, 0.4f) * 2.0f, vec3(timeSunrise + timeSunset));
-
8/12/2019 copmpsite - Advance OpenGL Shader
19/34
surface.sky.albedo *= mix(vec3(1.0f), horizonColor1, vec3(horizonGradient) * (1.0f - timeMidnight));
surface.sky.albedo *= mix(vec3(1.0f), horizonColor2, vec3(pow(horizonGradient, 2.0f)) * (1.0f - timeMidnight));
float grayscale = surface.sky.albedo.r + surface.sky.albedo.g + surface.sky.albedo.b;
grayscale /= 3.0f;
surface.sky.albedo = mix(surface.sky.albedo, vec3(grayscale) * 1.4f, vec3(rainStrength));}
void AddSunglow(inout SurfaceStruct surface) {float sunglowFactor = CalculateSunglow(surface);float antiSunglowFactor = CalculateAntiSunglow(surface);
surface.sky.albedo *= 1.0f + sunglowFactor * (15.0f + timeNoon * 5.0f) *(1.0f - rainStrength);
surface.sky.albedo *= mix(vec3(1.0f), colorSunlight, pow(clamp(vec3(sunglowFactor) * (1.0f - timeMidnight) * (1.0f - rainStrength), vec3(0.0f), vec3(1.0f)), vec3(2.0f)));
surface.sky.albedo *= 1.0f + antiSunglowFactor * 2.0f * (1.0f - rainStre
ngth); //surface.sky.albedo *= mix(vec3(1.0f), colorSunlight, antiSunglowFactor);}
void AddCloudGlow(inout vec3 color, in SurfaceStruct surface) {float glow = CalculateSunglow(surface);
glow = pow(glow, 1.0f);
float mult = mix(50.0f, 800.0f, timeSkyDark);
color.rgb *= 1.0f + glow * mult * float(surface.mask.clouds);
}
void CalculateUnderwaterFog(in SurfaceStruct surface, inout vec3 finalComposite) {
vec3 fogColor = colorWaterMurk * vec3(colorSkylight.b);// float fogDensity = 0.045f;// float fogFactor = exp(GetDepthLinear(texcoord.st) * fogDensity) - 1.0
f;// fogFactor = min(fogFactor, 1.0f);float fogFactor = GetDepthLinear(texcoord.st) / 100.0f;
fogFactor = min(fogFactor, 0.7f); fogFactor = sin(fogFactor * 3.1415 / 2.0f);
fogFactor = pow(fogFactor, 0.5f);
finalComposite.rgb = mix(finalComposite.rgb, fogColor * 0.002f, vec3(fogFactor));
finalComposite.rgb *= mix(vec3(1.0f), colorWaterBlue * colorWaterBlue *colorWaterBlue * colorWaterBlue, vec3(fogFactor));
//finalComposite.rgb = vec3(0.1f);}
-
8/12/2019 copmpsite - Advance OpenGL Shader
20/34
void TestRaymarch(inout vec3 color, in SurfaceStruct surface){
//visualize march stepsfloat rayDepth = 0.0f;float rayIncrement = 0.05f;float fogFactor = 0.0f;
while (rayDepth < 1.0f){
vec4 rayPosition = GetScreenSpacePosition(texcoord.st, pow(rayDepth, 0.002f));
if (abs(rayPosition.z - surface.screenSpacePosition.z) < 0.025f){
color.rgb = vec3(0.01f, 0.0f, 0.0f);}
// if (SphereTestDistance(vec3(surface.screenSpacePosition.x, surface.screenSpacePosition.y, surface.screenSpacePosition.z))
-
8/12/2019 copmpsite - Advance OpenGL Shader
21/34
* 1.0f) / noiseTextureResolution)).g * 2.0f - 1.0f, texture2D(noisetex, vec2(0.0f + (i
* 1.0f) / noiseTextureResolution)).b * 2.0f - 1.0f);//kernel[i] += (stochastic * vec3(2.0f, 2.0f, 1.0f) - vec3(1.0f,
1.0f, 0.0f)) * 0.0f;kernel[i] = normalize(kernel[i]);
//scale randomly to distribute within hemisphere;kernel[i] *= pow(texture2D(noisetex, vec2(0.3f + (i * 1.0f) / no
iseTextureResolution)).r * CalculateNoisePattern1(vec2(43.0f), 64.0f).x * 1.0f,1.2f);
}
//Determine origin position and normalvec3 origin = surface.screenSpacePosition.xyz;vec3 normal = surface.normal.xyz;
//normal = lightVector;
//Create matrix to orient hemisphere according to surface normal//vec3 randomRotation = texture2D(noisetex, texcoord.st * vec2(viewWidth
/ noiseTextureResolution, viewHeight / noiseTextureResolution)).rgb * 2.0f - 1.0f;
//float dither1 = CalculateDitherPattern1() * 2.0f - 1.0f;//randomRotation = vec3(dither1, mod(dither1 + 0.5f, 2.0f), mod(
dither1 + 1.0f, 2.0f));vec3 randomRotation = CalculateNoisePattern1(vec2(0.0f), 64.0f).xyz *2.0f - 1.0f;
//vec3 randomRotation = vec3(1.0f, 0.0f, 0.0f);
vec3 tangent = normalize(randomRotation - upVector * dot(randomRotation,upVector));
vec3 bitangent = cross(upVector, tangent);mat3 tbn = mat3(tangent, bitangent, upVector);
float ao = 0.0f;float aoSkylight = 0.0f;
float aoUp = 0.0f;float aoBounced = 0.0f;float aoScattered = 0.0f;
float aoRadius = 0.35f * -surface.screenSpacePosition.z; //aoRadius = 3.0f;
float zThickness = 0.35f * -surface.screenSpacePosition.z; zThickness = 6.0f;
vec3 samplePosition = vec3(0.0f);float intersect = 0.0f;
vec4 sampleScreenSpace = vec4(0.0f);float sampleDepth = 0.0f;float distanceWeight = 0.0f;float finalRadius = 0.0f;
float skylightWeight = 0.0f;float bouncedWeight = 0.0f;float scatteredUpWeight = 0.0f;float scatteredSunWeight = 0.0f;vec3 bentNormal = vec3(0.0f);
-
8/12/2019 copmpsite - Advance OpenGL Shader
22/34
for (int i = 0; i < numSamples; i++){
samplePosition = tbn * kernel[i];samplePosition = samplePosition * aoRadius + origin;
intersect = dot(normalize(samplePosition - origin), surface.normal);
if (intersect > 0.2f) {//Convert camera space to screen spacesampleScreenSpace = gbufferProjection * vec4(samplePosit
ion, 1.0f);sampleScreenSpace.xyz /= sampleScreenSpace.w;sampleScreenSpace.xyz = sampleScreenSpace.xyz * 0.5f + 0
.5f;
//Check depth at sample pointsampleDepth = GetScreenSpacePosition(sampleScreenSpace.x
y).z;
//If point is behind geometry, buildup AOif (sampleDepth >= samplePosition.z && !surface.mask.sky
)
{ //Reduce halofloat sampleLength = length(samplePosition - ori
gin) * 4.0f;//distanceWeight = 1.0f - clamp(distance(sampleD
epth, origin.z) - (sampleLength * 0.5f), 0.0f, sampleLength * 0.5f) / (sampleLength * 0.5f);
distanceWeight = 1.0f - step(sampleLength, distance(sampleDepth, origin.z));
//Weigh samples based on light directionskylightWeight = clamp(dot(norm
alize(samplePosition - origin), upVector) * 1.0f - 0.0f , 0.0f, 0.
01f) / 0.01f; //skylightWeight += clamp(dot(normalize(samplePosition - origin), upVector), 0.0f, 1.0f);
bouncedWeight = clamp(dot(normalize(samplePosition - origin), -lightVector) * 1.0f - 0.0f , 0.0f, 0.51f) / 0.51f;
scatteredUpWeight = clamp(dot(normalize(samplePosition - origin), -upVector) * 1.0f - 0.0f , 0.0f, 0.51f) / 0.51f;
scatteredSunWeight = clamp(dot(normalize(samplePosition - origin), lightVector) * 1.0f - 0.25f, 0.0f, 0.51f) / 0.51f;
//buildup occlusion more for further facing surfaces
skylightWeight/= clamp(dot(normal, upVector) * 0.5f + 0.501f, 0.01f, 1.0f);
bouncedWeight/= clamp(dot(normal, -lightVector) * 0.5f + 0.501f, 0.01f, 1.0f);
scatteredUpWeight /= clamp(dot(normal, -upVector) * 0.5f + 0.501f, 0.01f, 1.0f);
scatteredSunWeight /= clamp(dot(normal, lightVector) * 0.75f + 0.25f, 0.01f, 1.0f);
-
8/12/2019 copmpsite - Advance OpenGL Shader
23/34
//Accumulate aoao += 2.0f * distanceWeight
;aoSkylight += 2.0f * distanceWeight * skylightW
eight ;aoUp += 2.0f * distanceWeight * scatt
eredUpWeight ;aoBounced += 2.0f * distanceWeight * bounc
edWeight ;aoScattered += 2.0f * distanceWeight * scattered
SunWeight ;} else {
bentNormal.rgb += normalize(samplePosition - origin);
}}
}
bentNormal.rgb /= numSamples;
ao /= numSamples;aoSkylight /= numSamples;
aoUp /= numSamples;aoBounced /= numSamples;aoScattered /= numSamples;
ao = 1.0f - ao;aoSkylight = 1.0f - aoSkylight;aoUp = 1.0f - aoUp;aoBounced = 1.0f - aoBounced;aoScattered = 1.0f - aoScattered;
ao = clamp(ao, 0.0f, 1.0f);aoSkylight = clamp(aoSkylight, 0.0f, 1.0f);aoUp = clamp(aoUp, 0.0f, 1.0f);
aoBounced = clamp(aoBounced, 0.0f, 1.0f);aoScattered = clamp(aoScattered, 0.0f, 1.0f);
surface.ao.constant = pow(ao,1.0f);
surface.ao.skylight = pow(aoSkylight,3.0f);
surface.ao.bouncedSunlight = pow(aoBounced,6.0f);
surface.ao.scatteredUpLight = pow(aoUp, 6.0f);surface.ao.scatteredSunlight = pow(aoScattered, 1.0f);
surface.debug = vec3(pow(aoSkylight, 2.0f) * clamp((dot(surface.normal,
upVector) * 0.75f + 0.25f), 0.0f, 1.0f));//surface.debug = vec3(dot(normalize(bentNormal), upVector));
}
void CalculateRainFog(inout vec3 color, in SurfaceStruct surface){
vec3 fogColor = colorSkylight * 0.055f;
float fogDensity = 0.0018f * rainStrength;
-
8/12/2019 copmpsite - Advance OpenGL Shader
24/34
fogDensity *= mix(0.0f, 1.0f, pow(eyeBrightnessSmooth.y / 240.0f, 6.0f));
float visibility = 1.0f / (pow(exp(distance(surface.screenSpacePosition.xyz, vec3(0.0f)) * fogDensity), 1.0f));
float fogFactor = 1.0f - visibility; fogFactor = clamp(fogFactor, 0.0f, 1.0f); fogFactor = mix(fogFactor, 1.0f, float(surface.mask.sky) * 0.8
f * rainStrength); fogFactor = mix(fogFactor, 1.0f, float(surface.mask.clouds) *
0.8f * rainStrength);
color = mix(color, fogColor, vec3(fogFactor));}
void CalculateAtmosphericScattering(inout vec3 color, in SurfaceStruct surface){
vec3 fogColor = mix(colorSkylight, colorSunlight, vec3(0.05f)) * 0.11f;
float sat = 0.5f;fogColor.r = fogColor.r * (1.0f + sat) - (fogColor.g + fogColor
.b) * 0.5f * sat;fogColor.g = fogColor.g * (1.0f + sat) - (fogColor.r + fogColor
.b) * 0.5f * sat;
fogColor.b = fogColor.b * (1.0f + sat) - (fogColor.r + fogColor.g) * 0.5f * sat;
float sunglow = pow(CalculateSunglow(surface), 2.0f);vec3 sunColor = colorSunlight;
fogColor += mix(vec3(0.0f), sunColor, sunglow * 0.8f);
float fogDensity = 0.004f; fogDensity *= mix(0.0f, 1.0f, pow(eyeBrightnessSmooth.y / 240.
0f, 6.0f));float visibility = 1.0f / (pow(exp(distance(surface.screenSpacePosition.
xyz, vec3(0.0f)) * fogDensity), 1.0f));
float fogFactor = 1.0f - visibility; fogFactor = clamp(fogFactor, 0.0f, 1.0f);
fogFactor = pow(fogFactor, 2.7f); //fogFactor = mix(fogFactor, 1.0f, float(surface.mask.sky) * 0
.8f * rainStrength); //fogFactor = mix(fogFactor, 1.0f, float(surface.mask.clouds)
* 0.8f * rainStrength);
fogFactor = mix(fogFactor, 0.0f, min(1.0f, surface.sky.sunSpot.r));fogFactor *= mix(1.0f, 0.25f, float(surface.mask.sky));fogFactor *= mix(1.0f, 0.75f, float(surface.mask.clouds));
float redshift = 1.5f * (1.0f - rainStrength); redshift *= float(surface.mask.land);
//scatter away high frequency lightcolor.b *= 1.0f - clamp(fogFactor * 1.65 * redshift, 0.0f, 0.75f);color.g *= 1.0f - fogFactor * 0.4* redshift;color.g *= 1.0f - clamp(fogFactor - 0.26f, 0.0f, 1.0f) * 0.5* redshift;
//add scattered low frequency lightcolor += fogColor * fogFactor * 1.0f;
-
8/12/2019 copmpsite - Advance OpenGL Shader
25/34
}
float Get3DNoise(in vec3 pos){
pos.z += 0.0f;vec3 p = floor(pos);vec3 f = fract(pos);
vec2 uv = (p.xy + p.z * vec2(17.0f)) + f.xy;vec2 uv2 = (p.xy + (p.z + 1.0f) * vec2(17.0f)) + f.xy;vec2 coord = (uv + 0.5f) / noiseTextureResolution;vec2 coord2 = (uv2 + 0.5f) / noiseTextureResolution;float xy1 = texture2D(noisetex, coord).x;float xy2 = texture2D(noisetex, coord2).x;return mix(xy1, xy2, f.z);
}
vec4 CloudColor(in vec3 worldPosition, in float sunglow){
float cloudHeight = 140.0f;
float cloudDepth = 25.0f;float cloudUpperHeight = cloudHeight + (cloudDepth / 2.0f);float cloudLowerHeight = cloudHeight - (cloudDepth / 2.0f);
if (worldPosition.y < cloudLowerHeight || worldPosition.y > cloudUpperHeight)
return vec4(0.0f);else{
vec3 p = worldPosition.xyz / 40.0f;float t = frameTimeCounter;
//t *= 0.0;
p.x += t * 0.005f;float noise = Get3DNoise(p)* 1.0f; p *= 4.0f; p.x += t * 0.07f;
noise += (1.0f - abs(Get3DNoise(p) * 3.0f - 1.0f)) * 0.30f; p *= 3.0f; p.xz += t * 0.15f;
noise += (1.0f - abs(Get3DNoise(p) * 3.0f - 1.0f)) * 0.085f; p *= 2.0f; p.xz += t * 0.15f;
noise += (1.0f - abs(Get3DNoise(p) * 3.0f - 1.0f)) * 0.06f;
noise /= 1.2f;
float cloudAltitudeWeight = 1.0f - clamp(distance(worldPosition.
y, cloudHeight) / (cloudDepth / 2.0f), 0.0f, 1.0f); cloudAltitudeWeight = pow(cloudAltitudeWeight, 0.15f);
noise *= cloudAltitudeWeight;
//cloud edgefloat coverage = 0.4f;
coverage = mix(coverage, 0.77f, rainStrength);float density = 0.05f;noise = clamp(noise - (1.0f - coverage), 0.0f, 1.0f - density) /
-
8/12/2019 copmpsite - Advance OpenGL Shader
26/34
(1.0f - density);
float sunProximity = pow(sunglow, 1.0f);float propigation = mix(3.0f, 18.0f, sunProximity);
float heightGradient = clamp(( - (cloudLowerHeight - worldPosition.y) / cloudDepth), 0.0f, 1.0f);
float directLightFalloff = pow(heightGradient, propigation);
directLightFalloff *= mix( clamp(pow(noise, 0.85f)* 2.5f, 0.0f, 1.0f), clamp(pow(1.0f - noise, 10.3f), 0.0f, 0.5f), pow(sunglow, 1.2f));
vec3 colorDirect = colorSunlight * 1.0f;colorDirect *= 1.0f + pow(sunglow, 10.0f) * 100.0f;colorDirect *= 1.0f + pow(sunglow, 2.0f) * 100.0f;
vec3 colorAmbient = mix(colorSkylight, colorSunlight, 0.15f) * 0.075f;
colorAmbient *= 1.0f + clamp(pow(noise, 1.0f) * 1.0f, 0.0f, 1.0f);
colorAmbient *= heightGradient + 0.75f;
vec3 colorBounced = colorBouncedSunlight * 0.05f;colorBounced *= pow((1.0f - heightGradient), 5.0f);
vec3 color = mix(colorAmbient, colorDirect, vec3(min(1.0f, directLightFalloff * 4.0)));
color += colorBounced;//vec3 color = colorAmbient;
color *= clamp(pow(noise, 0.1f), 0.0f, 1.0f);
color *= mix(1.0f, 0.1f, timeMidnight);
vec4 result = vec4(color.rgb, noise);
return result;}
}
void CalculateClouds (inout vec3 color, inout SurfaceStruct surface){
//if (texcoord.s < 0.5f && texcoord.t < 0.5f)//{
vec2 coord = texcoord.st * 2.0f;
vec4 worldPosition = gbufferModelViewInverse * surface.screenSpa
cePosition;worldPosition.xyz += cameraPosition.xyz;
float cloudHeight = 140.0f;float cloudDepth = 25.0f;float cloudDensity = 1.0f;
float rayDepth = 1.0f; rayDepth += CalculateDitherPattern1() * 0.11f; //rayDepth += texture2D(noisetex, texcoord.st * (viewW
-
8/12/2019 copmpsite - Advance OpenGL Shader
27/34
idth / noiseTextureResolution, viewHeight / noiseTextureResolution)).x * 0.1f; //rayDepth += CalculateDitherPattern2() * 0.1f;
float rayIncrement = 0.025f;
float previousRayDistance = 0.0f;int i = 0;
vec3 cloudColor = colorSunlight;vec4 cloudSum = vec4(0.0f);
cloudSum.rgb = colorSkylight * 0.2f;cloudSum.rgb = color.rgb;
float sunglow = CalculateSunglow(surface);
while (rayDepth > 0.0f){
//determine worldspace ray positionvec4 rayPosition = GetWorldSpacePosition(texcoord.st, po
w(rayDepth, 0.0005f * (1.0f - rayDepth)));
if (rayPosition.y > cloudHeight - (cloudHeight / 2.0f) && rayPosition.y < cloudHeight + (cloudHeight / 2.0f))
{
//determine screen space ray positionfloat rayDistance = length(rayPosition.xyz - cameraPosition.xyz);
float surfaceDistance = length(worldPosition.xyz- cameraPosition.xyz);
//if ray is past surface, don't accumulateif (rayDistance < surfaceDistance){
//add cloud density. Multiply by ray distance traveled
if (i > 0)
{ float stepDistance = abs(rayDistance - previousRayDistance);
vec4 proximity = CloudColor(rayPosition.xyz, sunglow);
proximity.a *= min(stepDistance * cloudDensity, cloudDensity);
cloudSum.rgb = mix(cloudSum.rgb,proximity.rgb, vec3(pow(min(1.0f, proximity.a * 1.2f), 0.1f)));
cloudSum.a += proximity.a * 1.0f;
}
else //add cloud density for first step{
float stepDistance = 0.0f;vec4 proximity = CloudColor(ray
Position.xyz, sunglow);proximity.a *= min(step
Distance * 0.9f, 1.0f);
cloudSum.rgb = mix(cloudSum.rgb,proximity.rgb, vec3(min(1.0f, proximity.a * 2.0f)));
-
8/12/2019 copmpsite - Advance OpenGL Shader
28/34
cloudSum.a += proximity.a;}
//record previous ray positionpreviousRayDistance = rayDistance;
}
}
//Increment rayrayDepth -= rayIncrement;i++;
}
color.rgb = mix(color.rgb, cloudSum.rgb, vec3(min(1.0f, cloudSum.a * 20.0f)));
//if (cloudSum.a > 0.0f)//color.rgb = mix(color.rgb, colorSunlight * 4.0f, 1.0f - pow(cl
oudSum.a, 0.02f));
if (cloudSum.a > 0.00f){
surface.mask.volumeCloud = true;
}
//}//color.rgb = vec3(noise) * 0.2f;
}
void SnowShader(inout SurfaceStruct surface){
float snowFactor = dot(surface.normal, upVector); snowFactor = clamp(snowFactor - 0.1f, 0.0f, 0.05f) / 0.05f;
surface.albedo = mix(surface.albedo.rgb, vec3(1.0f), vec3(snowFactor));}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////MAIN////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////void main() {
//Initialize surface properties required for lighting calculation for any surface that is not part of the sky
surface.albedo = GetAlbedoLinear(texcoord.st);//Gets the albedo texture
surface.albedo = pow(surface.albedo, vec3(1.4f));
surface.albedo = mix(surface.albedo, vec3(dot(surface.albedo, vec3(0.3333f))), vec3(0.075f));
-
8/12/2019 copmpsite - Advance OpenGL Shader
29/34
//surface.albedo = vec3(0.8f);surface.normal = GetNormals(texcoord.st);
//Gets the screen-space normalssurface.depth = GetDepth(texcoord.st);
//Gets the scene depthsurface.linearDepth = ExpToLinearDepth(surface.depth);
//Get linear scene depthsurface.screenSpacePosition = GetScreenSpacePosition(texcoord.st);
//Gets the screen-space positionsurface.viewVector = normalize(surface.screenSpaceP
osition.rgb); //Gets the view vectorsurface.lightVector = lightVector;
//Gets the sunlight vectorsurface.upVector = upVector;
//Store the up vector
surface.mask.matIDs = GetMaterialIDs(texcoord.st);//Gets material ids
CalculateMasks(surface.mask);
surface.albedo *= 1.0f - float(surface.mask.sky);//Remove the sky from surface albedo, because sky will be handled separately
//surface.water.albedo = surface.albedo * float(surface.mask.water);//Store underwater albedo
//surface.albedo *= 1.0f - float(surface.mask.water);//Remove water from surface albedo to handle water separately
//Initialize sky surface propertiessurface.sky.albedo = GetAlbedoLinear(texcoord.st) * (min(1.
0f, float(surface.mask.sky) + float(surface.mask.sunspot)));//Gets the albedo texture for the sky
surface.sky.tintColor = mix(colorSunlight, vec3(colorSunlight.r), vec3(0.8f));//Initializes the defualt tint color for the sky
surface.sky.tintColor *= mix(1.0f, 500.0f, timeSkyDark);//Boost sky color at night//Scale sunglow back to be less intense
surface.sky.sunSpot = vec3(float(CalculateSunspot(surface))) * vec3((min(1.0f, float(surface.mask.sky) + float(surface.mask.sunspot)))) * colorSunlight;
surface.sky.sunSpot *= 1.0f - timeMidnight;surface.sky.sunSpot *= 1500.0f;surface.sky.sunSpot *= 1.0f - rainStrength;//surface.sky.sunSpot *= 1.0f - timeMidnight;
AddSkyGradient(surface);AddSunglow(surface);
-
8/12/2019 copmpsite - Advance OpenGL Shader
30/34
//Initialize MCLightmap valuesmcLightmap.torch = GetLightmapTorch(texcoord.st);
//Gets the lightmap for light coming from emissive blocks
mcLightmap.sky = GetLightmapSky(texcoord.st);//Gets the lightmap for light coming from the sky
// mcLightmap.sky = 1.0f / pow((1.0f - mcLightmap.sky + 0.00001f), 2.0f);
// mcLightmap.sky -= 1.0f;// mcLightmap.sky = max(0.0f, mcLightmap.sky);
mcLightmap.lightning = 0.0f;//gets the lightmap for light coming from lightning
//Initialize default surface shading attributessurface.diffuse.roughness = 0.0f;
//Default surface roughnesssurface.diffuse.translucency = 0.0f;
//Default surface translucencysurface.diffuse.translucencyColor = vec3(1.0f);
//Default translucency color
surface.specular.specularity = GetSpecularity(texcoord.st);//Gets the reflectance/specularity of the surfacesurface.specular.extraSpecularity = 0.0f;
//Default value for extra specularitysurface.specular.glossiness = GetGlossiness(texcoord.st);surface.specular.metallic = 0.0f;
//Default value of how metallic the surface issurface.specular.gain = 1.0f;
//Default surface specular gainsurface.specular.base = 0.0f;
//Default reflectance when the surface normal and viewing normal are alignedsurface.specular.fresnelPower = 5.0f;
//Default surface fresnel power
//Calculate surface shadingCalculateNdotL(surface);shading.direct = CalculateDirectLighting(surfac
e); //Calculate direct sunlight without visibility check (shadows)
shading.direct = mix(shading.direct, 1.0f, float(surface.mask.water)); //Remove shading from water
shading.sunlightVisibility = CalculateSunlightVisibility(surface, shading); //Calculate shadows and apply th
em to direct lightingshading.direct *= shading.sunlightVisibility;shading.direct *= mix(1.0f, 0.0f, rainStrength)
;shading.waterDirect = shading.direct;shading.direct *= pow(mcLightmap.sky, 0.1f);shading.bounced = CalculateBouncedSunlight(surface);
//Calculate fake bounced sunlightshading.scattered = CalculateScatteredSunlight(surface);
//Calculate fake scattered sunlight
-
8/12/2019 copmpsite - Advance OpenGL Shader
31/34
shading.skylight = CalculateSkylight(surface);//Calculate scattered light from sky
shading.scatteredUp = CalculateScatteredUpLight(surface);
InitializeAO(surface);//if (texcoord.s < 0.5f && texcoord.t < 0.5f)//CalculateAO(surface);
//Colorize surface shading and store in lightmapslightmap.sunlight = vec3(shading.direct) * colorSu
nlight;AddCloudGlow(lightmap.sunlight, surface);//lightmap.sunlight *= 2.0f - AO;
lightmap.skylight = vec3(mcLightmap.sky);lightmap.skylight *= mix(pow(colorSkylight, vec3(1
.3f)), colorBouncedSunlight, max(vec3(0.0f), (vec3(1.0f - pow(mcLightmap.sky + 0
.23f, 0.45f)) * 1.0f) + (1.0f - shading.skylight) * 0.25f));lightmap.skylight *= shading.skylight;lightmap.skylight *= mix(1.0f, 5.0f, float(surface
.mask.clouds));lightmap.skylight *= mix(1.0f, 50.0f, float(surfac
e.mask.clouds) * timeSkyDark);lightmap.skylight *= surface.ao.skylight;//lightmap.skylight *= surface.ao.constant * 0.5f +
0.5f;lightmap.skylight += mix(colorSkylight, colorSunli
ght, vec3(0.2f)) * vec3(mcLightmap.sky) * surface.ao.constant * 0.15f;
lightmap.bouncedSunlight = vec3(shading.bounced) * colorBouncedSunlight;
lightmap.bouncedSunlight *= pow(vec3(mcLightmap.sky), vec3(1.75f));
lightmap.bouncedSunlight *= mix(1.0f, 0.25f, timeSunrise + timeSunset);
lightmap.bouncedSunlight *= mix(1.0f, 0.0f, rainStrength);lightmap.bouncedSunlight *= surface.ao.bouncedSunlight;//lightmap.bouncedSunlight *= surface.ao.constant * 0.5f + 0.5f;
lightmap.scatteredSunlight = vec3(shading.scattered) * colorScatteredSunlight;
lightmap.scatteredSunlight *= pow(vec3(mcLightmap.sky), vec3(1.0f));
lightmap.scatteredSunlight *= surface.ao.scatteredSunlight;//lightmap.scatteredSunlight *= surface.ao.constant * 0.5f + 0.5f;
lightmap.underwater = vec3(mcLightmap.sky) * colorSkylight;
lightmap.torchlight = mcLightmap.torch * colorTorchlight;lightmap.torchlight *= surface.ao.constant * surface.ao.cons
tant;
lightmap.nolight = vec3(0.05f);lightmap.nolight *= surface.ao.constant;
lightmap.scatteredUpLight = vec3(shading.scatteredUp) * mix(colorSunlight, colorSkylight, vec3(0.3f));
-
8/12/2019 copmpsite - Advance OpenGL Shader
32/34
lightmap.scatteredUpLight *= pow(mcLightmap.sky, 0.5f);lightmap.scatteredUpLight *= surface.ao.scatteredUpLight;lightmap.scatteredUpLight *= mix(1.0f, 0.1f, rainStrength);//lightmap.scatteredUpLight *= surface.ao.constant * 0.5f + 0.5f;
//If eye is in waterif (isEyeInWater > 0) {
vec3 halfColor = mix(colorWaterMurk, vec3(1.0f), vec3(0.5f));lightmap.sunlight *= mcLightmap.sky * halfColor;lightmap.skylight *= halfColor;lightmap.bouncedSunlight *= 0.0f;lightmap.scatteredSunlight *= halfColor;lightmap.nolight *= halfColor;lightmap.scatteredUpLight *= halfColor;
}
//Apply lightmaps to albedo and generate final shaded surfacefinal.nolight = surface.albedo * lightmap.nolight;final.sunlight = surface.albedo * lightmap.sunlight;final.skylight = surface.albedo * lightmap.skylight;
final.bouncedSunlight = surface.albedo * lightmap.bouncedSunlight;final.scatteredSunlight = surface.albedo * lightmap.scatteredSunlight;final.scatteredUpLight = surface.albedo * lightmap.scatteredUpLight;final.torchlight = surface.albedo * lightmap.torchlight;final.underwater = surface.water.albedo * colorWaterBlue;// final.underwater *= GetUnderwaterLightmapSky(texcoord.st)
;// final.underwater += vec3(0.9f, 1.00f, 0.35f) * float(surf
ace.mask.water) * 0.065f;final.underwater *= (lightmap.sunlight * 0.3f) + (lightma
p.skylight * 0.06f) + (lightmap.torchlight * 0.0165) + (lightmap.nolight * 0.002f);
//final.glow.torch = pow(surface.albedo, vec3(4.0f)) * float(surface.mask.torch);
final.glow.lava = Glowmap(surface.albedo, surface.mask.lava, 6.0f, vec3(1.0f, 0.05f, 0.001f));
final.glow.glowstone = Glowmap(surface.albedo, surface.mask.glowstone, 3.2f, colorTorchlight);
final.torchlight *= 1.0f - float(surface.mask.glowstone);
final.glow.fire = surface.albedo * float(surface.mask.fire);
final.glow.fire = pow(final.glow.fire, vec3(1.0f));
//Remove glow items from torchlight to keep controlfinal.torchlight *= 1.0f - float(surface.mask.lava);
//Do night eye effect on outdoor lighting and skyDoNightEye(final.sunlight);
-
8/12/2019 copmpsite - Advance OpenGL Shader
33/34
DoNightEye(final.skylight);DoNightEye(final.bouncedSunlight);DoNightEye(final.scatteredSunlight);DoNightEye(surface.sky.albedo);DoNightEye(final.underwater);
DoLowlightEye(final.nolight);
float sunlightMult = 0.8f;
//Apply lightmaps to albedo and generate final shaded surfacevec3 finalComposite = final.sunlight * 0.35f
//* sunlightMult //Add direct sunlight+ final.skylight
* 0.04f //Add ambient skylight+ final.nolight
* 0.0001f //Add base ambient light+ final.bouncedSunlight
* 0.015f * sunlightMult //Add fake bounced sunlight
+ final.scatteredSunlight* 0.00f * sunlightMult //Add fake scattered sunlight
+ final.scatteredUpLight
* 0.0025f * sunlightMult + final.torchlight* 2.0f //Add light coming from emissive blocks
+ final.glow.lava* 7.6f
+ final.glow.glowstone* 5.1f
+ final.glow.fire* 0.05f
;
//Apply sky to final compositesurface.sky.albedo *= 0.95f;
surface.sky.albedo = surface.sky.albedo * surface.sky.tintColor+ surface.sky.sunglow + surface.sky.sunSpot;surface.sky.albedo *= mix(1.0f, 0.125f, timeMidnight);//DoNightEye(surface.sky.albedo);finalComposite += surface.sky.albedo; //Add sk
y to final image
//if eye is in water, do underwater fogif (isEyeInWater > 0) {
CalculateUnderwaterFog(surface, finalComposite);}
CalculateRainFog(finalComposite.rgb, surface);CalculateAtmosphericScattering(finalComposite.rgb, surface);
CalculateClouds(finalComposite.rgb, surface);
//finalComposite.rgb = texture2D(shadowcolor, texcoord.st).rgb * 0.05f;//finalComposite.rgb = vec3(AO) * 0.2f;
-
8/12/2019 copmpsite - Advance OpenGL Shader
34/34
finalComposite *= 0.0005f;//Scale image down for HDR
finalComposite.b *= 1.0f;
//TestRaymarch(finalComposite.rgb, surface); //finalComposite.rgb = surface.debug * 0.00004f;
finalComposite = pow(finalComposite, vec3(1.0f / 2.2f));//Convert final image into gamma 0.45 space to compensate for gamma 2.2 on displays
finalComposite = pow(finalComposite, vec3(1.0f / BANDING_FIX_FACTOR));//Convert final image into banding fix space to help reduce color banding
// if (finalComposite.r > 1.0f) {// finalComposite.r = 0.0f;// }
// if (finalComposite.g > 1.0f) {// finalComposite.g = 0.0f;// }
// if (finalComposite.b > 1.0f) {// finalComposite.b = 0.0f;// }
gl_FragData[0] = vec4(finalComposite, 1.0f);gl_FragData[1] = vec4(surface.mask.matIDs, mcLightmap.torch, mcLightmap.
sky, 1.0f);
gl_FragData[2] = vec4(surface.normal.rgb * 0.5f + 0.5f, 1.0f);gl_FragData[3] = vec4(surface.specular.specularity, surface.shadow, surface.specular.glossiness, 1.0f);
// gl_FragData[4] = vec4(surface.albedo, 1.0f);// gl_FragData[5] = vec4(lightmap.)
}