Skip to content

Instantly share code, notes, and snippets.

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 gulafaran/cb51eae8ba9bdb79ed59ca8988266e39 to your computer and use it in GitHub Desktop.
Save gulafaran/cb51eae8ba9bdb79ed59ca8988266e39 to your computer and use it in GitHub Desktop.
#version 150
#define VertexShader
#ifdef FragmentShader
#define varying in
#else
#define varying out
#endif
void fake_sincos( float a, out float s, out float c)
{
float b = fract (a + 0.75);
c = min(a, 1.0-a)*4.0-1.0;
s = min(b, 1.0-b)*4.0-1.0;
}
vec3 UnpackNormal2f( vec2 normal )
{
vec2 norm_xy = normal*2.0-1.0;
float norm_z = sqrt(1.0 - norm_xy.x*norm_xy.x - norm_xy.y*norm_xy.y);
return vec3 ( norm_xy, norm_z );
}
vec4 ComputeFog( vec3 wpos, float depth, vec3 L, vec3 V, vec3 fog_color1, vec3 fog_color2, vec4 fog_coefs )
{
float fog_amount = max(0, depth - fog_coefs.y*10000) * fog_coefs.x;
float height_fog = max( fog_coefs.z*wpos.z + fog_coefs.w, 0.0 );
fog_amount += height_fog;
fog_amount = 1.0-exp2( -fog_amount );
vec3 fog_color = vec3 (1,1,1);
fog_color = fog_color1;
return vec4 ( fog_color, fog_amount );
}
vec2 RemapTexCoords( vec2 tc, vec4 rem)
{
return rem.xy + rem.zw * tc;
}
float Lum( vec3 color )
{
return dot( color.xyz, vec3 ( 0.299, 0.587, 0.114 ));
}
vec3 Desaturate( vec3 color, vec3 amount )
{
float lum = Lum( color );
return mix ( color, vec3 (lum, lum, lum), amount );
}
float SchlickFresnel(float Ro, float x)
{
return Ro + (1.0 - Ro) * pow(1.0 - x, 5.0);
}
float Beckman(float NdotH, float roughness)
{
float R2 = roughness * roughness;
float NdotH2 = clamp(NdotH * NdotH, 0.0, 1.0) ;
return exp(-(1.0 - NdotH2) / (R2 * NdotH2)) / (4.0 * R2 * NdotH2 * NdotH2);
}
struct StdVertex
{
vec4 pos;
vec3 normal;
vec2 tc1;
vec2 tc2;
vec2 tc3;
vec4 tangent;
vec3 binormal;
vec2 noise;
vec4 bw;
ivec4 bi;
float shadow_coef;
int instanceId;
};
float PackSpecPowerAndMT( float spec_power, float mt )
{
vec2 sm = vec2 ( spec_power, mt );
const vec2 ab = vec2 (255.0 / 4.0, 255.0 / 64.0);
const vec2 cd = vec2 (1.0 / 255.0, 64.0 / 255.0);
return dot(floor(sm * ab), cd);
}
void UnpackSpecPowerAndMT(float x, out float s, out int m)
{
const float a = 255.0 / 64.0;
const float b = 256.0 / 255.0;
float flt_m = floor(x * a);
m = int(flt_m);
s = clamp(4.0 * x - flt_m * b, 0.0, 1.0) ;
}
layout(std140) uniform StaticSceneParams {
vec4 Lift;
vec4 Gain;
vec4 Gamma;
vec4 DiffuseDesat;
vec4 GainNoOverbright;
vec4 ShallowWaterColor;
vec4 DeepWaterColor;
vec4 WaterReflectionColor;
vec4 WaterSpecularColor;
vec4 SunDirWorld;
vec4 SkyColor;
vec4 SunRealColor;
vec4 SunRealBackColor;
vec4 SunColor;
vec4 IndoorColor1;
vec4 IndoorColor2;
vec4 FogColor;
vec4 FogCoef;
vec4 FogColor2;
vec4 SkyFog;
vec4 Frustum;
vec4 SunBackColor;
vec4 PostProcBloom;
vec4 PostProcBlurDesat;
vec4 PostProcBloomScene;
vec4 PostProcBloomColor;
vec4 VignetteColor;
vec4 PostProcSSAOParams;
vec4 UnitModifier1;
vec4 UnitModifier2;
vec4 RecipWorldSize;
vec4 HfaoParams;
vec4 FogOfWarParams;
vec4 FogOfWarColor;
vec4 FogParams;
vec4 MiscParams;
vec4 CloudsParams;
vec4 SunBeamsParams;
vec4 FoamGeneration;
vec4 WaterDims;
};
layout(std140) uniform DynamicSceneParams {
mat4 View;
mat4 ViewInverse;
mat4 LightView;
mat4 ViewProjection;
mat4 Projection;
mat4 ViewProjectionInverse;
mat4 LightProjection;
mat4 LightViewProjection;
vec4 EyePosWorld;
vec4 EyeAt;
};
vec3 GradeColor( vec3 color)
{
return Gain.xyz * ( color + Lift.xyz * ( vec3 (1, 1, 1) - color));
}
vec4 LightExp( vec4 x)
{
return exp(-x);
}
vec4 LightLog( vec4 x)
{
return -log(max(x, 0.5/255.0));
}
vec4 EncodeLightmap( vec4 light)
{
return light;
}
vec4 DecodeLightmap( vec4 light)
{
return light;
}
float LinearizeDepth( float depth, vec4 DepthLinearization )
{
return 1.0 / ( depth * DepthLinearization.x + DepthLinearization.y );
}
vec3 DecodeCompressedNormal( vec2 inp)
{
vec3 N;
N.xy = inp*2.0-1.0;
N.z = sqrt( 1.0 - N.x*N.x - N.y*N.y );
return N;
}
vec3 GetMaterialColor( int material )
{
int idx = material;
if( idx == 0 )
return vec3 (0,0,0);
else if( idx == 1 )
return vec3 (0,1,0);
else if( idx == 2 )
return vec3 (1,0,0);
else if( idx == 3 )
return vec3 (1,1,0);
return vec3 (1,1,1);
}
float ComputeSpecular( vec3 L, vec3 V, vec3 N, float power )
{
vec3 H = normalize( L - V );
float dotNH = clamp(dot( N, H ), 0.0, 1.0) ;
float spec = pow( dotNH, power );
return spec;
}
vec3 EncodeNormalStereo( vec3 n )
{
float scale = 1.7777;
vec2 enc = n.xy / (n.z+1.0);
enc /= scale;
enc = enc*0.5+0.5;
return vec3 ( enc, 0.0 );
}
vec3 DecodeNormalStereo( vec3 enc )
{
float scale = 1.7777;
vec3 nn =
enc.xyz* vec3 (2.0*scale,2.0*scale,0.0) +
vec3 (-scale,-scale,1.0);
float g = 2.0 / dot(nn.xyz,nn.xyz);
vec3 n;
n.xy = g*nn.xy;
n.z = g-1.0;
return normalize(n);
}
vec3 EncodeNormal2( vec3 n )
{
n = vec3 (normalize( n ));
vec2 enc = vec2 ((n.xy * inversesqrt (n.z+1.0)));
enc = enc*0.3535534 + 0.5;
return vec3 ( enc, 0.0 );
}
vec3 DecodeNormal2( vec3 enc )
{
vec2 ab = vec2 ((enc.xy*2.828427 - 1.41421356));
float z = 1.0 - dot(ab, ab);
return vec3 (ab*sqrt(1.0 + z), z);
}
vec3 EncodeNormal( vec3 N )
{
return EncodeNormal2( vec3 (N) );
}
vec3 DecodeNormal( vec3 enc )
{
return DecodeNormal2( vec3 (enc) );
}
float ComputeSpecularStrengthFromGloss( float gloss )
{
return 0.06 *( pow( 2000.0 , gloss) * 0.125 + 0.25 )* clamp(gloss*16.0, 0.0, 1.0) ;
}
float ComputeClouds( sampler2D CloudsMapSampler , vec3 wpos)
{
vec2 tc_clouds = wpos.xy / 1000 ;
tc_clouds.y -= CloudsParams.x;
tc_clouds = ((tc_clouds) * ( mat2 (SkyFog.zw, -SkyFog.w, SkyFog.z))) ;
float tex_clouds = dot( texture(CloudsMapSampler, tc_clouds) .yw, vec2 ( MiscParams.w , 1- MiscParams.w ));
return 1 - CloudsParams.y + smoothstep(CloudsParams.z, CloudsParams.w, tex_clouds) * CloudsParams.y;
}
vec3 MixNormal( vec3 BaseTex, vec3 DetailTex)
{
vec3 t = BaseTex + vec3 (0, 0, 1);
vec3 u = DetailTex* vec3 (-1, -1, 1);
vec3 res = t*dot(t, u)/t.z - u;
return res;
}
void EncodeGBuffer
(
vec3 albedo,
vec3 normal,
vec4 gloss,
float si,
float alpha,
float material,
float env,
out vec4 gb0,
out vec4 gb1
)
{
gb0.xyz = EncodeNormal( normal );
gb0.w = PackSpecPowerAndMT( gloss.w, material );
gb0.z = gloss.x;
gb1.xyz = albedo;
float si1 = si;
gb1.w = ((si > 0.02f) ? si1*126.5 : env*126.5 + 128.0) * (1.0 / 255.0);
gb0.w = alpha;
gb1.w = alpha;
}
void DecodeGBuffer1( vec4 gb1, out vec3 albedo, out float si, out float env )
{
albedo = gb1.xyz;
float param = fract ( gb1.w*2.0 );
float select = floor( gb1.w*2.0 );
si = (1.0-select)*param;
env = select*param;
}
void DecodeGBuffer0( vec4 gb0, out vec3 N, out vec4 gloss, out int material )
{
N = DecodeNormal( gb0.xyz );
gloss = vec4 (0,0,0,0);
float mask = gb0.w;
gloss.xyz = gb0.zzz;
UnpackSpecPowerAndMT( mask, gloss.w, material );
}
#ifdef VertexShader
in vec4 ATTR0 ;
in vec2 ATTR9 ;
#endif
int ShaderPriority = 15;
layout(std140) uniform HG_GLOBALS_CB__ {
vec4 UnpackCoef;
vec4 RenderTargetParams;
vec4 DepthLinearization;
vec4 DiffuseRemap;
vec4 Dimensions;
uniform vec4 MiscInstancedParams[32 ];
uniform vec4 ColorModifier[32 ];
uniform mat4 World[32 ];
};
uniform sampler2D DiffuseMap ;
uniform sampler2D DepthMap ;
uniform sampler2D GBufferNormalCopy ;
varying vec4 interp_view ;
varying vec4 interp_tr1 ;
varying vec4 interp_tr2 ;
varying vec4 interp_tr3 ;
varying float interp_iscale ;
varying vec4 interp_color_modifier ;
#ifdef VertexShader
void main() { vec4 _vert_pos;
#line 153
StdVertex IN; IN.pos = vec4 ( ( ATTR0 .xyz*32767.0) , 1 ); IN.instanceId = int( ( ATTR9 .x*32767.0) ); IN.pos.xyz = IN.pos.xyz * UnpackCoef.www + UnpackCoef.xyz; ;
mat4 world;
world = World[IN.instanceId] ;
vec4 wpos = vec4 ( dot(IN.pos, vec4 ((world)[0][0], (world)[1][0], (world)[2][0], (world)[3][0])), dot(IN.pos, vec4 ((world)[0][1], (world)[1][1], (world)[2][1], (world)[3][1])), dot(IN.pos, vec4 ((world)[0][2], (world)[1][2], (world)[2][2], (world)[3][2])), dot(IN.pos, vec4 ((world)[0][3], (world)[1][3], (world)[2][3], (world)[3][3]))) ;
_vert_pos = vec4 ( dot(wpos, vec4 ((ViewProjection)[0][0], (ViewProjection)[1][0], (ViewProjection)[2][0], (ViewProjection)[3][0])), dot(wpos, vec4 ((ViewProjection)[0][1], (ViewProjection)[1][1], (ViewProjection)[2][1], (ViewProjection)[3][1])), dot(wpos, vec4 ((ViewProjection)[0][2], (ViewProjection)[1][2], (ViewProjection)[2][2], (ViewProjection)[3][2])), dot(wpos, vec4 ((ViewProjection)[0][3], (ViewProjection)[1][3], (ViewProjection)[2][3], (ViewProjection)[3][3]))) ;
float n = 1/length( ((world)[0].xyz) );
interp_tr1 .xyz = vec3((world)[0][0], (world)[1][0], (world)[2][0]) * n;
interp_tr2 .xyz = vec3((world)[0][1], (world)[1][1], (world)[2][1]) * n;
interp_tr3 .xyz = vec3((world)[0][2], (world)[1][2], (world)[2][2]) * n;
interp_view .xyz = wpos.xyz - EyePosWorld.xyz;
interp_view .w = dot( interp_view .xyz, EyeAt.xyz);
vec3 center = vec4 ( dot( vec4 (0,0,0,1), vec4 ((world)[0][0], (world)[1][0], (world)[2][0], (world)[3][0])), dot( vec4 (0,0,0,1), vec4 ((world)[0][1], (world)[1][1], (world)[2][1], (world)[3][1])), dot( vec4 (0,0,0,1), vec4 ((world)[0][2], (world)[1][2], (world)[2][2], (world)[3][2])), dot( vec4 (0,0,0,1), vec4 ((world)[0][3], (world)[1][3], (world)[2][3], (world)[3][3]))) .xyz;
interp_tr1 .w = center.x;
interp_tr2 .w = center.y;
interp_tr3 .w = center.z;
interp_iscale = MiscInstancedParams[IN.instanceId] .y;
interp_color_modifier = ColorModifier[IN.instanceId] ;
gl_Position = vec4(_vert_pos.x, -_vert_pos.y, _vert_pos.z*2.0 - _vert_pos.w, _vert_pos.w); return ; }
#endif
#line 187
out vec4 FO_COL0 ;
out vec4 FO_COL1 ;
#ifdef FragmentShader
void main() {
#line 199
vec4 tex_diffuse = vec4 (1,1,1,1);
vec4 tex_gloss = vec4 (0,0,0,0);
float iscale = interp_iscale ;
vec2 screen = gl_FragCoord.xy *RenderTargetParams.xy ;
float depth = textureLod(DepthMap, screen, 0) .x ;
float linear_depth = 1.0 / (depth * DepthLinearization.x + DepthLinearization.y);
vec3 wpos = linear_depth / interp_view .w * interp_view .xyz + EyePosWorld.xyz;
vec3 to_vector = wpos - vec3 ( interp_tr1 .w, interp_tr2 .w, interp_tr3 .w);
vec3 tc3;
tc3.y = dot(to_vector, interp_tr1 .xyz);
tc3.x = dot(to_vector, interp_tr2 .xyz);
tc3.z = dot(to_vector, interp_tr3 .xyz);
tc3 *= Dimensions.xyz*iscale;
vec3 clip_val = 0.25 - tc3*tc3 - vec3 (0.02, 0.02, 0);
if (clip_val.x < 0 || clip_val.y < 0 || clip_val.z < 0) discard ;
vec2 tc = 0.5 - tc3.xy;
vec2 diff_tc = RemapTexCoords(tc, DiffuseRemap) ;
vec2 ddx_tc = dFdx (diff_tc);
vec2 ddy_tc = dFdy (diff_tc);
ddx_tc = clamp(ddx_tc, -0.03, 0.03);
ddy_tc = clamp(ddy_tc, -0.03, 0.03);
tex_diffuse = textureGrad(DiffuseMap, diff_tc, ddx_tc, ddy_tc) ;
float tex_envmask = 0;
vec3 tex_nm = texture(GBufferNormalCopy, screen) .xyz;
tex_nm = DecodeNormal( tex_nm );
float dotSlope = clamp( clamp(dot( tex_nm, interp_tr3 .xyz ) - 0.5, 0.0, 1.0) * 1000, 0.0, 1.0) ;
if (dotSlope - 0.2 < 0.5 ) discard ;
vec4 cModifier = interp_color_modifier ;
tex_diffuse.xyz *= cModifier.xyz;
vec3 out_normal = vec3 (0,0,1);
float alpha = cModifier.w;
alpha *= tex_diffuse.w;
EncodeGBuffer( tex_diffuse.xyz, out_normal, tex_gloss, 0, alpha, 0, tex_envmask, FO_COL0, FO_COL1 );
}
#endif
#line 377
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment