First commit

This commit is contained in:
2025-11-17 17:18:43 +01:00
parent 2286a3b782
commit bca5ef911b
905 changed files with 950521 additions and 2 deletions

225
shaders/ambientOcclusion.shader Executable file
View File

@@ -0,0 +1,225 @@
/**
* Kepler - Core
* STUDIOS
* All rights reserved.
* Obfuscated by Shader closure (A product of kaj dijkstra)
*/
/**
* Author: Kaj Dijksta
*/
attribute vec2 uv;
attribute vec3 position;
uniform mat4 viewProjection;
varying vec2 v_uv;
void main ( void ) {
v_uv = uv;
gl_Position = viewProjection *vec4 ( position , 1.0 );
}
// #keplerEngine - Split
#ifndef AMBIANT_ONLY
#define AMBIANT_ONLY 0
#endif
precision highp float;
varying vec2 v_uv;
uniform sampler2D infoSampler;
uniform sampler2D randomSampler;
uniform float screenWidth;
uniform float screenHeight;
uniform vec3 cameraPosition;
uniform float far;
uniform vec3 scale[ 32 ];
uniform vec4 kernelRad[ 8 ];
struct fragmentPass { highp vec2 uv; mediump vec4 diffuse; mediump vec3 normal; mediump vec3 positionWorld; mediump float ambiantOcclusion; highp float far; highp float depth; highp float depthNorm; };
float function_0 ( vec3 var_0 , vec3 var_1 , vec3 var_2 , float var_3 )
{
mediump vec3 var_4 = reflect ( -var_2 , var_0 );
return pow ( clamp ( dot ( var_1 , var_4 ) , 0.0 , 1.0 ) , var_3 );
}
float function_1 ( vec2 var_0 ) {
mediump vec4 var_1 = vec4 (.6 , 0.075 , 1.0 , 1.0 );
const mediump float var_2 = 1.0 -1.0 /8.0;
const mediump float var_3 = 0.025;
mediump float var_4 = 0.0;
mediump vec3 var_5[8];
var_5[0] = normalize ( vec3 ( 1.0 , 1.0 , 1.0 ) ) *var_3 *( var_4 += var_2 );
var_5[1] = normalize ( vec3 ( -1.0 , -1.0 , -1.0 ) ) *var_3 *( var_4 += var_2 );
var_5[2] = normalize ( vec3 ( -1.0 , -1.0 , 1.0 ) ) *var_3 *( var_4 += var_2 );
var_5[3] = normalize ( vec3 ( -1.0 , 1.0 , -1.0 ) ) *var_3 *( var_4 += var_2 );
var_5[4] = normalize ( vec3 ( -1.0 , 1.0 , 1.0 ) ) *var_3 *( var_4 += var_2 );
var_5[5] = normalize ( vec3 ( 1.0 , -1.0 , -1.0 ) ) *var_3 *( var_4 += var_2 );
var_5[6] = normalize ( vec3 ( 1.0 , -1.0 , 1.0 ) ) *var_3 *( var_4 += var_2 );
var_5[7] = normalize ( vec3 ( 1.0 , 1.0 , -1.0 ) ) *var_3 *( var_4 += var_2 );
mediump vec3 var_6 = texture2D ( randomSampler , vec2 ( screenWidth , screenHeight ) *var_0 /4.0 ).xyz *2.0 -1.0;
mediump float var_7 = texture2D ( infoSampler , var_0 ).w;
mediump float var_8 = var_7 *far;
mediump vec3 var_9 = var_1.zzw *clamp ( var_8 /5.3 , 0.0 , 1.0 ) *( 1.0 +var_8 /8.0 );
mediump float var_10 = far /var_9.z *0.75;
var_9.xy *= 1.0 /var_8;
var_9.z *= 2.0 /far;
float var_11 = 64.0 /var_9.z;
mediump vec4 var_12[2];
mediump vec4 var_13 = vec4 ( 0.0 );
mediump vec3 var_14;
mediump vec4 var_15;
highp vec4 var_16 = vec4 ( 0.0 );
const float var_17 = 0.2;
vec4 var_18;
var_14 = reflect ( var_5[0] , var_6 ) *var_9;
var_12[0].x = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
var_14.xyz *= var_17;
var_12[1].x = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
var_14 = reflect ( var_5[1] , var_6 ) *var_9;
var_12[0].y = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
var_14.xyz *= var_17;
var_12[1].y = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
var_14 = reflect ( var_5[2] , var_6 ) *var_9;
var_12[0].z = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
var_14.xyz *= var_17;
var_12[1].z = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
var_14 = reflect ( var_5[3] , var_6 ) *var_9;
var_12[0].w = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
var_14.xyz *= var_17;
var_12[1].w = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
const float var_19 = 0.001;
var_15 = var_7 -var_12[0];
var_18 = var_15 *var_10;
var_16 = ( clamp ( abs ( var_18 ) , 0.0 , 1.0 ) +clamp ( var_18 , 0.0 , 1.0 ) ) /2.0;
var_13 += mix ( clamp ( ( -var_15 ) *var_11 , 0.0 , 1.0 ) , vec4 ( var_19 ) , var_16 );
var_15 = var_7 -var_12[1];
var_18 = var_15 *var_10;
var_16 = ( clamp ( abs ( var_18 ) , 0.0 , 1.0 ) +clamp ( var_18 , 0.0 , 1.0 ) ) /2.0;
var_13 += mix ( clamp ( ( -var_15 ) *var_11 , 0.0 , 1.0 ) , vec4 ( var_19 ) , var_16 );
float var_20 = dot ( var_13 , vec4 ( ( 1.0 /16.0 ) *2.0 ) ) -var_1.y;
var_20 = clamp ( mix ( 0.9 , var_20 , var_1.x ) , 0.0 , 1.0 );
return var_20;
}
float function_2 ( vec2 var_0 , float var_1 , vec3 var_2 )
{
const mediump float var_3 = 0.4;
const mediump int var_4 = 16;
const mediump float var_5 = 0.05;
const mediump float var_6 = var_3 /( float ( var_4 ) /2.0 );
const mediump float var_7 = 1000.0;
mediump float var_8 = 2.8 *( 1.0 +sqrt ( var_5 ) ) /var_7;
const mediump float var_9 = 1.0 -2.8;
mediump vec3 var_10 = texture2D ( randomSampler , vec2 ( screenWidth , screenHeight ) *var_0 /4.0 ).xyz *2.0 -1.0;
mediump vec4 var_11 = vec4 ( var_7 /var_1 );
mediump vec4 var_12 = vec4 ( 0.0 );
for ( int var_13 = 0; var_13 < var_4 /4; var_13 ++)
{
mediump vec4 var_14 = vec4 ( 0.0 );
mediump vec4 var_15 = vec4 ( 0.0 );
mediump vec4 var_16 = vec4 ( 0.0 );
for ( int var_17 = 0; var_17 < 4; ++var_17 )
{
mediump vec3 var_18 = reflect ( scale[4 *var_13 +var_17] , var_10 );
var_16[var_17] = dot ( var_18 , var_2 );
var_18 = ( var_16[var_17] >= 0.0 ) ? var_18 : -var_18;
var_14[var_17] = texture2D ( infoSampler , var_0.xy +var_18.xy ).w;
var_15[var_17] = var_18.z;
}
var_14 = var_14 *var_11;
mediump vec4 var_19 = clamp ( var_7 +var_15 *( 2.0 *var_7 ) -var_14 , 0.0 , 1.0 );
var_19 *= clamp ( var_8 *var_14 +var_9 , 0.0 , 1.0 );
var_19 *= ( abs ( var_16 ) *kernelRad[var_13] );
var_12 += var_19;
}
var_12 *= var_6;
lowp float var_20 = 1.0 -dot ( vec4 ( 1.0 ) , var_12 );
return var_20;
}
float function_3 ( vec2 var_0 , float var_1 , vec3 var_2 )
{
const mediump vec4 var_3 = vec4 ( 1. , 0.075 , 1.0 , 2.0 );
const int var_4 = 8;
const mediump float var_5 = 0.6;
const mediump float var_6 = 1.0 /var_5;
const mediump float var_7 = 2.5;
const mediump float var_8 = var_3.x;
vec3 var_9[16];
var_9[0] = vec3 ( -0.055664 , -0.00371090 , -0.0654297 );
var_9[1] = vec3 ( 0.0173828 , 0.0111328 , 0.0064453 );
var_9[2] = vec3 ( 0.0001953 , 0.008203100000000001 , -0.0060547 );
var_9[3] = vec3 ( 0.0220703 , -0.035937500000000004 , -0.00625 );
var_9[4] = vec3 ( 0.0242188 , 0.012695300000000001 , -0.025 );
var_9[5] = vec3 ( 0.0070313 , -0.0025391000000000003 , 0.014843799999999999 );
var_9[6] = vec3 ( -0.007812 , 0.0013672 , -0.0314453 );
var_9[7] = vec3 ( 0.0117188 , -0.0140625 , -0.019921900000000003 );
var_9[8] = vec3 ( -0.025195 , -0.055859400000000003 , 0.008203100000000001 );
var_9[9] = vec3 ( 0.0308594 , 0.019335900000000003 , 0.0324219 );
var_9[10] = vec3 ( 0.0173828 , -0.0140625 , 0.003125 );
var_9[11] = vec3 ( 0.0179688 , -0.0044922 , 0.004687500000000001 );
var_9[12] = vec3 ( -0.014648 , -0.020117200000000002 , -0.0029297000000000004 );
var_9[13] = vec3 ( -0.030078 , 0.0234375 , 0.0539063 );
var_9[14] = vec3 ( 0.0228516 , 0.0154297 , -0.0119141 );
var_9[15] = vec3 ( -0.011914 , -0.00039060000000000006 , -0.006640600000000001 );
mediump vec3 var_10 = texture2D ( randomSampler , vec2 ( screenWidth , screenHeight ) *var_0 /4.0 ).xyz *2.0 -1.0;
mediump vec4 var_11 = vec4 ( 1.0 /var_1 );
mediump vec4 var_12 = vec4 ( 0.0 );
for ( int var_13 = 0; var_13 < var_4; var_13 += 4 )
{
mediump vec4 var_14 = vec4 ( 0.0 );
mediump vec4 var_15 = vec4 ( 0.0 );
mediump vec4 var_16;
mediump vec4 var_17;
for ( int var_18 = 0; var_18 < 4; ++var_18 )
{
mediump vec3 var_19 = reflect ( var_9[var_13 +var_18] , var_10 );
float var_20 = dot ( var_19 , var_2 );
var_19 = ( var_20 >= 0.0 ) ? var_19 : -var_19;
var_14[var_18] = texture2D ( infoSampler , ( var_0 +var_19.xy ) ).w;
var_15[var_18] = var_19.z;
}
var_14 = var_14 *var_11;
mediump vec4 var_21 = ( 1.0 +var_15 *2. -var_14 ) *var_6;
var_16 = clamp ( var_7 *var_21 , 0.0 , 1.0 );
var_17 = clamp ( 1.0 /var_21 , 0.0 , 1.0 );
var_12 += var_16 *var_17;
}
var_12 *= var_8 /float ( var_4 );
mediump float var_22 = dot ( vec4 ( 1.0 ) , var_12 );
return 1.0 -var_22;
}
mediump vec3 function_4 ( mediump vec2 var_0 ) {
mediump vec2 var_1 = var_0 *4.0 -2.0;
mediump float var_2 = dot ( var_1 , var_1 );
mediump float var_3 = sqrt ( 1.0 -var_2 /4.0 );
mediump vec3 var_4;
var_4.xy = var_1 *var_3;
var_4.z = 1.0 -var_2 /2.0;
return var_4;
}
void main ( void ) {
vec4 var_0 = texture2D ( infoSampler , v_uv );
float var_1 = var_0.w ;
//vec3 var_3 = function_4 ( var_0.yz );
vec3 var_3 = var_0.xyz;
float var_4;
// var_4 = function_1 ( v_uv );
var_4 = function_3 ( v_uv , var_1 , var_3 );
gl_FragColor = vec4 ( var_4 , var_4 , var_4 , 1.0 );
//gl_FragColor = vec4(v_uv, 0.0, 1.0);
//gl_FragColor = vec4(vec3(texture2D ( infoSampler , v_uv).w/10.),1.0);//werkt
// gl_FragColor = vec4(vec3(texture2D ( infoSampler , v_uv).xyz),1.0);//werkt
//gl_FragColor = vec4(texture2D ( randomSampler, v_uv).xyz , 1.0);//werkt
}

647
shaders/color.shader Executable file
View File

@@ -0,0 +1,647 @@
#version 300 es
precision highp float;
#ifndef NORMAL_MAP
#define NORMAL_MAP 0
#endif
#ifndef PARALLAX
#define PARALLAX 0
#endif
in vec3 position;
in vec2 textcoord;
in vec3 normal;
in vec3 tangent;
in vec3 bitangent;
uniform mat4 world;
uniform mat4 view;
uniform mat4 viewProjection;
uniform mat4 worldInverseTranspose;
uniform float normals;
out vec2 v_textcoord;
out vec4 v_normal;
out vec4 v_tangent;
out vec4 v_binormal;
out vec2 v_offset;
out vec4 v_worldposition;
out vec4 v_position;
out float v_depth;
out vec3 v_view;
//mat3 transpose(mat3 matrix) {
//
// return mat3( matrix[0].x, matrix[1].x, matrix[2].x,
// matrix[0].y, matrix[1].y, matrix[2].y,
// matrix[0].z, matrix[1].z, matrix[2].z );
//
//}
void main(void) {
v_textcoord = textcoord;
v_normal = normalize(worldInverseTranspose * vec4(normal, 0.0));
#if NORMAL_MAP == 1
v_tangent = normalize(worldInverseTranspose * vec4(tangent, 0.0) );
v_binormal = normalize(worldInverseTranspose * vec4(bitangent, 0.0) );
//v_binormal = vec4(cross(v_normal.xyz, v_tangent.xyz), 0.0);
#endif
#if PARALLAX == 1
const float height_map_range = 0.32;
vec3 vs_normal = ( view * v_normal ).xyz;
vec3 vs_tangent = ( view * v_tangent ).xyz;
vec3 vs_bitangent = ( view * vec4(v_binormal, 0.0) ).xyz;
//vec3 vs_bitangent = bitangent;
vec4 a = view * vec4(position, 1.0);
v_view = a.xyz;
mat3 tbn = mat3( normalize(vs_tangent), normalize(vs_bitangent), normalize(vs_normal) );
mat3 vs_to_ts = transpose( tbn );
vec3 ts_view = vs_to_ts * v_view;
v_offset = (ts_view.xy / ts_view.z) * height_map_range;
#endif
v_worldposition = world * vec4(position, 1.0);
v_position = viewProjection * vec4(position, 1.0);
v_depth = v_position.z;
gl_Position = v_position;
}
// #keplerEngine - Split
#version 300 es
precision highp float;
#define PI 3.14159265358979323846
layout(location = 0) out vec4 deferred_diffuse_roughness;
layout(location = 1) out vec4 deferred_normal_depth;
layout(location = 2) out vec4 deferred_position_material_ID;
layout(location = 3) out vec4 tangent;
//layout(location = 3) out vec4 deferred_material_1;
in vec2 v_textcoord;
in vec4 v_normal;
in vec4 v_tangent;
in vec4 v_binormal;
in vec4 v_worldposition;
in vec4 v_position;
in float v_depth;
in vec2 v_offset;
in vec3 v_view;
const float gamma = 2.2;
//pbr
uniform samplerCube reflectionSampler;
uniform vec3 cameraPosition;
uniform vec3 lightGeometry;
uniform float clearCoat;
uniform vec3 clearCoatColor;
uniform float clearCoatThickness;
uniform float clearCoatRoughness;
uniform float clearCoatIOR;
uniform float anisotropy;
uniform vec3 lightDirection;
uniform vec3 lightColor;
uniform float lightIntensity;
uniform float environmentLuminance;
uniform float v_metallic;
uniform float v_reflectance;
uniform float lightType;
uniform vec3 lightPosition;
#define ANISOTROPY 0
#define USE_IES_PROFILE 0
#define TRANSPARENT_MATERIAL 0
#define TRANSLUCENT_MATERIAL 0
#define CUBEMAP_EDGE_FIXUP 0
#ifndef NORMAL_MAP
#define NORMAL_MAP 0
#endif
#ifndef TEXTURE
#define TEXTURE 0
#endif
#ifndef ROUGHNESS_MAP
#define ROUGHNESS_MAP 0
#endif
#ifndef PARALLAX
#define PARALLAX 0
#endif
precision highp float;
uniform sampler2D diffuseSampler;
uniform sampler2D normalSampler;
uniform sampler2D roughnessSampler;
uniform sampler2D heightSampler;
uniform sampler2D shadowNoiseSampler;
uniform sampler2D shadowDepthSampler;
uniform float normals;
uniform float far;
uniform float roughness;
uniform float metallic;
uniform float alpha;
uniform float shadingModelID;
uniform vec3 diffuseColor;
uniform float attenuation;
uniform float SourceRadius;
uniform float SourceLength;
uniform float specular;
uniform float mode;
uniform float uvMultiplier;
uniform float render_type;
uniform float shadowBias;
uniform mat4 lightViewProjection;
#include "physically_based_shading_full.shader"
vec3 sampleReflection( vec3 r ) {
return texture(reflectionSampler, r).xyz;
}
vec3 HDR_ACES(const vec3 x) {
// Narkowicz 2015, "ACES Filmic Tone Mapping Curve"
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return (x * (a * x + b)) / (x * (c * x + d) + e);
}
vec3 tonemap(const vec3 x) {
return HDR_ACES(x);
}
float linearToSRGB(float c) {
return (c <= 0.0031308) ? c * 12.92 : (pow(abs(c), 1.0 / 2.4) * 1.055) - 0.055;
}
vec3 linearToSRGB(vec3 c) {
return vec3(linearToSRGB(c.r), linearToSRGB(c.g), linearToSRGB(c.b));
}
float DecodeFloatRGBA( vec4 rgba ) {
return (rgba).x;
//return dot( rgba, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0) );
}
float shadow_sample(sampler2D depthMap, vec2 coord)
{
return texture(depthMap, coord).x;//DecodeFloatRGBA() ;
}
vec2 DoubleSampleRotated(sampler2D depthMap, vec4 p, vec4 rotMatr, vec4 kernel) {
vec4 rotatedOff;
rotatedOff = rotMatr.xyzw * kernel.xxww +
rotMatr.zwxy * kernel.yyzz;
vec4 fetchPos = p.xyxy + rotatedOff;// + rotatedOff
vec2 result;
result.x = shadow_sample(depthMap, fetchPos.xy);
result.y = shadow_sample(depthMap, fetchPos.zw);
return result;
}
float PCF(sampler2D depthMap, vec4 p, vec2 randDirTC, float depth)
{
vec2 kernelRadius = vec2(4.0);
vec4 irreg_kernel_2d[8];
irreg_kernel_2d[0] = vec4(-0.556641,-0.037109,-0.654297, 0.111328);
irreg_kernel_2d[1] = vec4(0.173828,0.111328,0.064453, -0.359375);
irreg_kernel_2d[2] = vec4(0.001953,0.082031,-0.060547, 0.078125);
irreg_kernel_2d[3] = vec4(0.220703,-0.359375,-0.062500, 0.001953);
irreg_kernel_2d[4] = vec4(0.242188,0.126953,-0.250000, -0.140625);
irreg_kernel_2d[5] = vec4(0.070313,-0.025391,0.148438, 0.082031);
irreg_kernel_2d[6] = vec4(-0.078125,0.013672,-0.314453, 0.013672);
irreg_kernel_2d[7] = vec4(0.117188,-0.140625,-0.199219, 0.117188);
vec2 vInvShadowMapWH = vec2(1.0 / 2048.0);
const int kernelSize = 8;
mediump float P_Z = depth; // p.z;
vec4 p0 = vec4(p.xyz, 1.0);
mediump vec2 rotScale = vec2(kernelRadius.y * 2.0);
float shadowTest = 0.0;
#define KERNEL_STEP_SIZE 2
vec2 rotSample = 2.0 * texture(shadowDepthSampler, randDirTC.xy).xy - 1.0;
rotSample.xy = normalize(rotSample.xy);
rotSample.xy *= (kernelRadius.xy * vInvShadowMapWH.xy);
vec4 rot = vec4(rotSample.x, -rotSample.y, rotSample.y, rotSample.x);
const int kernelOffset = 0;
for(int i=kernelOffset; i<kernelSize; i+=KERNEL_STEP_SIZE) // Loop over taps
{
mediump vec4 sampleDepth = vec4(0.0);
vec4 irr = irreg_kernel_2d[i+0];
sampleDepth.xy = DoubleSampleRotated(depthMap, p0, rot, irr);
sampleDepth.zw = DoubleSampleRotated(depthMap, p0, rot, irreg_kernel_2d[i+1]);
mediump vec4 InShadow;
InShadow.x = ( P_Z < sampleDepth.x + shadowBias ) ? 1. : 0.0;
InShadow.y = ( P_Z < sampleDepth.y + shadowBias ) ? 1. : 0.0;
InShadow.z = ( P_Z < sampleDepth.z + shadowBias ) ? 1. : 0.0;
InShadow.w = ( P_Z < sampleDepth.w + shadowBias ) ? 1. : 0.0;
const mediump float quality = 8.0; // 8 == high
const mediump float fInvSamplNum = (1.0 / quality);
shadowTest += dot(InShadow, vec4(fInvSamplNum));
}
return shadowTest;
}
float poissonPCFmultitap(vec4 projCoords, float shadowDepth, vec2 uv)
{
const mediump float step = 1.0 - 1.0 / 8.0;
const mediump float fScale = 0.025; // 0.025
mediump float n = 0.0;
mediump vec3 directions[8];
float vSampleScale = 1.0 / 2048.0;
directions[0] = normalize(vec3( 1.0, 1.0, 1.0))*fScale*(n+=step);
directions[1] = normalize(vec3(-1.0,-1.0,-1.0))*fScale*(n+=step);
directions[2] = normalize(vec3(-1.0,-1.0, 1.0))*fScale*(n+=step);
directions[3] = normalize(vec3(-1.0, 1.0,-1.0))*fScale*(n+=step);
directions[4] = normalize(vec3(-1.0, 1.0 ,1.0))*fScale*(n+=step);
directions[5] = normalize(vec3( 1.0,-1.0,-1.0))*fScale*(n+=step);
directions[6] = normalize(vec3( 1.0,-1.0, 1.0))*fScale*(n+=step);
directions[7] = normalize(vec3( 1.0, 1.0,-1.0))*fScale*(n+=step);
mediump vec3 randomSample = texture(shadowNoiseSampler, vec2(64.0, 64.0) * uv.xy / 4.0).xyz * 2.0 - 1.0;
float sum = 0.0;
// for( int i = 0; i < 4; i++ ) {
// vec3 sampler = reflect(directions[0], randomSample) * vSampleScale;
float pixelDepth = DecodeFloatRGBA( texture(shadowDepthSampler, projCoords.xy ) ) ; // + sampler.xy + sampler.z
if( pixelDepth + shadowBias > shadowDepth) {
sum += 1.0;
} else {
sum += 0.0;
}
// }
return sum;
}
float linestep(float min, float max, float value) {
return clamp((value - min) / (max - min), 0., 1.);
}
float reduceBleeding(float p_max, float amount) {
return linestep(amount, 1.0, p_max);
}
float ChebyshevUpperBound(vec2 moments, float distance) {
if (distance <= moments.x)
return 1.0;
float g_minVariance = .00007;
float variance = moments.y - (moments.x*moments.x);
variance = max(variance,g_minVariance);
float d = distance - moments.x;
float p_max = variance / (variance + d*d);
return reduceBleeding(p_max, .725);
}
float calculateShadowOcclusion( vec3 worldPosition, vec2 uv ) {
vec4 projCoords = lightViewProjection * vec4(worldPosition, 1.0) ;
float shadowDepth = length( lightPosition - worldPosition );
projCoords.xy /= projCoords.w;
projCoords = 0.49 * projCoords + 0.5;
vec4 moments = texture( shadowDepthSampler, projCoords.xy );
mediump vec2 randomSample = texture(shadowNoiseSampler, vec2(1024.) * uv / 64.0).xy * 2.0 - 1.0;
float outFrustum = 0.0;
if(projCoords.x < 0.0 || projCoords.x > 1.0) {
return 1.0;
}
if(projCoords.y < 0.0 || projCoords.y > 1.0) {
return 1.0;
}
if(projCoords.z < 1.0) {
return 1.0;
}
//return ChebyshevUpperBound(moments.xy, shadowDepth);
//if(uv.x < .5)
//return DecodeFloatRGBA( texture(shadowDepthSampler, projCoords.xy ) ) / 10.0;
//else
//return shadowDepth;
//return moments.x;
//return projCoords.x;
//return texture( shadowDepthSampler, uv ).x;//projCoords.xy
//return poissonPCFmultitap(projCoords, shadowDepth, uv);
//return PCF(sampler2D depthMap, vec4 p, vec2 randDirTC);
return PCF( shadowDepthSampler, projCoords, randomSample, shadowDepth );
//return 1.0 - ChebyshevUpperBound(moments.xy, shadowDepth);
// return smoothShadow(projCoords, shadowDepth,uv);
}
void main() {
vec2 textureCoordinate = v_textcoord;
vec3 normal;
#if NORMAL_MAP == 1
vec4 normalMap = texture(normalSampler, textureCoordinate * uvMultiplier) * 2.0 - 1.0;
normal = normalize((v_tangent.xyz * normalMap.x) + (v_binormal.xyz * normalMap.y) + (v_normal.xyz * normalMap.z));
mat3 tangentToWorld = transpose(mat3( v_tangent.xyz ,
v_binormal.xyz ,
v_normal.xyz));
normal = normalMap.xyz * tangentToWorld;
#else
normal = v_normal.xyz;
#endif
#if PARALLAX == 1
const float max_samples = 30.0;
const float min_samples = 8.0;
vec3 view = normalize(v_view);
float num_steps = mix(max_samples, min_samples, dot(view, normal));
float current_height = 0.0;
float step_size = 1.0 / float(num_steps);
float prev_height = 1.0;
float step_index = 0.0;
vec2 tex_offset_per_step = step_size * v_offset;
vec2 tex_current_offset = v_textcoord;
float current_bound = 1.0;
float parallax_amount = 0.0;
vec2 pt1 = vec2(0.0);
vec2 pt2 = vec2(0.0);
vec2 tex_offset = vec2(0.0);
for(int x = 0; x < 30; x++) {
if(step_index < num_steps)
{
tex_current_offset -= tex_offset_per_step;
current_height = texture(heightSampler, tex_current_offset).r; //, dx, dy
current_bound -= step_size;
if(current_height > current_bound)
{
pt1 = vec2(current_bound, current_height);
pt2 = vec2(current_bound + step_size, prev_height);
tex_offset = tex_current_offset - tex_offset_per_step;
step_index = num_steps + 1.0;
}
else
{
step_index++;
prev_height = current_height;
}
}
}
float delta1 = pt1.x - pt1.y;
float delta2 = pt2.x - pt2.y;
float denominator = delta2 - delta1;
if(denominator == 0.0)
{
parallax_amount = 0.0;
}
else
{
parallax_amount = (pt1.x * delta2 - pt2.x * delta1) / denominator;
}
vec2 parallax_offset = v_offset * (1.0 - parallax_amount);
textureCoordinate.xy -= parallax_offset;
#endif
float gamma = 1.4;
vec3 baseColor;
vec4 diffuseMap;
#if TEXTURE == 1
diffuseMap = ( texture(diffuseSampler, textureCoordinate * uvMultiplier));//toLinear
baseColor = diffuseMap.rgb;
#else
baseColor = diffuseColor;
#endif
float f_roughness = roughness;
#if ROUGHNESS_MAP == 1
f_roughness = texture(roughnessSampler, textureCoordinate).x;
#endif
vec3 diffuse = baseColor ;
//
float opacity = 1.0;
float metallic = v_metallic / 2.0;
float reflectance = v_reflectance / 2.0;
vec3 viewDir = (cameraPosition - v_worldposition.xyz);
vec3 reflectionVector = reflect(-viewDir.xyz, normalize(normal.xyz));
vec3 reflectionSample = sampleReflection( reflectionVector );
if(render_type == 0.0) {
// Material properties
deferredMaterialData materialData;
materialData.ambientOcclusion = 1.0;
materialData.normal = normalize( normal.xyz );
materialData.baseColor = sRGBtoLinear(diffuse.rgb);
materialData.position = v_worldposition.xyz;
materialData.index = shadingModelID;
materialData.roughness = f_roughness;
materialData.clearCoat = clearCoat;
materialData.clearCoatRoughness = clearCoatRoughness;
materialData.clearCoatThickness = clearCoatThickness;
materialData.clearCoatColor = clearCoatColor;
materialData.alpha = 1.0;
materialData.shadowOcclusion = calculateShadowOcclusion( v_worldposition.xyz, textureCoordinate );
materialData.metallic = metallic;
materialData.reflectance = reflectance;
materialData.tangent = v_tangent.xyz;
// Light properties
deferredLightData lightData;
lightData.position = lightPosition;
lightData.direction = lightDirection;
lightData.color = lightColor;
lightData.intensity = lightIntensity;
lightData.inverseFalloff = lightGeometry.x;
lightData.length = lightGeometry.y;
lightData.radius = lightGeometry.z;
vec4 pbr = physically_based_shading( materialData, lightData );
float exposure = 1.7;
pbr.rgb *= exposure;
deferred_diffuse_roughness = vec4(linearToSRGB(tonemap(pbr.rgb)), 1.0);
if(shadingModelID == 100.0) {
deferred_diffuse_roughness = vec4(reflectionSample, 1.0);
}
//if(textureCoordinate.x > .5)
//deferred_diffuse_roughness = vec4(vec3(shadow), 1.0);
} else {
deferred_diffuse_roughness = vec4( (diffuse), f_roughness); // //f_roughness kaj niet vergeten!!!
deferred_normal_depth = vec4(normalize(normal), v_depth);
deferred_position_material_ID = vec4(v_worldposition.xyz, float( shadingModelID ) );
}
}

83
shaders/convolution.shader Executable file
View File

@@ -0,0 +1,83 @@
/**
* Kepler - Core
* Copyright (c) 2010 kaj dijkstra STUDIOS
* All rights reserved.
*/
/**
* Author: Kaj Dijksta
*/
attribute vec3 position;
attribute vec2 uv;
uniform mat4 viewProjection;
varying vec2 v_textureCoord;
void main(void) {
v_textureCoord = uv;
gl_Position = viewProjection * vec4(position, 1.0);
}
// #keplerEngine - Split
precision highp float;
uniform sampler2D image;
uniform vec2 imageIncrement;
uniform sampler2D random;
uniform float merge;
uniform float far;
varying vec2 v_textureCoord;
void main() {
float MSAA1X[13];
//MSAA1X[0] = 0.004433048175243747;
//MSAA1X[1] = 0.054005582622414484;
//MSAA1X[2] = 0.2420362293761143;
//MSAA1X[3] = 0.3990502796524549;
//MSAA1X[4] = 0.2420362293761143;
//MSAA1X[5] = 0.054005582622414484;
//MSAA1X[6] = 0.004433048175243747;
MSAA1X[0] = 0.0022181958546457665;
MSAA1X[1] = 0.008773134791588384;
MSAA1X[2] = 0.027023157602879527;
MSAA1X[3] = 0.06482518513852684;
MSAA1X[4] = 0.12110939007484814;
MSAA1X[5] = 0.17621312278855084;
MSAA1X[6] = 0.19967562749792112;
MSAA1X[7] = 0.17621312278855084;
MSAA1X[8] = 0.12110939007484814;
MSAA1X[9] = 0.06482518513852684;
MSAA1X[10] = 0.027023157602879527;
MSAA1X[11] = 0.008773134791588384;
MSAA1X[12] = 0.0022181958546457665;
vec2 imageCoordTmp = v_textureCoord + (vec2(imageIncrement) * 6.0);
vec4 sum = vec4(0.0);
for (int i = 0; i < 12; ++i) {
sum += texture2D(image, imageCoordTmp) * MSAA1X[i];
imageCoordTmp -= imageIncrement;
}
gl_FragColor = vec4(sum);
}

477
shaders/deferred.shader Executable file
View File

@@ -0,0 +1,477 @@
#version 300 es
precision highp float;
in vec2 uv;
in vec3 position;
out vec2 v_uv;
uniform mat4 viewProjection;
void main(void) {
v_uv = uv;
gl_Position = viewProjection * vec4(position, 1.0);
}
// #keplerEngine - Split
#version 300 es
precision highp float;
#define PI 3.14159265358979323846
#define ANISOTROPY 0
#define USE_IES_PROFILE 0
#define TRANSPARENT_MATERIAL 0
#define TRANSLUCENT_MATERIAL 0
#define CUBEMAP_EDGE_FIXUP 0
uniform vec3 cameraPosition;
uniform samplerCube reflectionSampler;
uniform float lightType;
uniform float environmentLuminance;
#include "physically_based_shading.shader"
uniform float viewMode;
uniform vec3 lightGeometry;
uniform float clearCoat;
uniform vec3 clearCoatColor;
uniform float clearCoatThickness;
uniform float clearCoatRoughness;
uniform vec3 lightDirection;
uniform float attenuation;
uniform vec3 lightPosition;
uniform vec3 lightColor;
uniform float lightIntensity;
uniform float metallic;
#define TEXTURED_MATERIAL 1
out vec4 fragmentColor;
in vec2 v_uv;
uniform float reflectance;
uniform float anisotropy;
uniform float roughness;
uniform sampler2D diffuseSampler;
uniform sampler2D normalSampler;
uniform sampler2D tangentSampler;
uniform sampler2D infoSampler;
uniform sampler2D ambientOcclusionSampler;
uniform sampler2D materialSampler;
uniform sampler2D shadowNoiseSampler;
uniform sampler2D shadowDepthSampler;
uniform float luma_z;
uniform float far;
uniform mat4 InvProjection;
uniform mat4 lightViewProjection;
uniform vec2 screenSize;
uniform float shadowBias;
const float PackUpscale = 256. / 255.; // fraction -> 0..1 (including 1)
const float UnpackDownscale = 255. / 256.; // 0..1 -> fraction (excluding 1)
const vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );
const vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );
const float ShiftRight8 = 1. / 256.;
vec4 packDepthToRGBA( const in float v ) {
vec4 r = vec4( fract( v * PackFactors ), v );
r.yzw -= r.xyz * ShiftRight8; // tidy overflow
return r * PackUpscale;
}
float unpackRGBAToDepth( const in vec4 v ) {
return dot( v, UnpackFactors );
}
float shadow_sample(sampler2D depthMap, vec2 coord)
{
return ( texture(depthMap, coord.xy).x );//DecodeFloatRGBA() ;
}
vec2 DoubleSampleRotated(sampler2D depthMap, vec4 p, vec4 rotMatr, vec4 kernel) {
vec4 rotatedOff;
rotatedOff = rotMatr.xyzw * kernel.xxww +
rotMatr.zwxy * kernel.yyzz;
vec4 fetchPos = p.xyxy + rotatedOff;// + rotatedOff
vec2 result;
result.x = shadow_sample(depthMap, fetchPos.xy);
result.y = shadow_sample(depthMap, fetchPos.zw);
return result;
}
float PCF(sampler2D depthMap, vec4 p, vec2 randDirTC, float depth)
{
vec2 kernelRadius = vec2(4.0);
vec4 irreg_kernel_2d[8];
irreg_kernel_2d[0] = vec4(-0.556641,-0.037109,-0.654297, 0.111328);
irreg_kernel_2d[1] = vec4(0.173828,0.111328,0.064453, -0.359375);
irreg_kernel_2d[2] = vec4(0.001953,0.082031,-0.060547, 0.078125);
irreg_kernel_2d[3] = vec4(0.220703,-0.359375,-0.062500, 0.001953);
irreg_kernel_2d[4] = vec4(0.242188,0.126953,-0.250000, -0.140625);
irreg_kernel_2d[5] = vec4(0.070313,-0.025391,0.148438, 0.082031);
irreg_kernel_2d[6] = vec4(-0.078125,0.013672,-0.314453, 0.013672);
irreg_kernel_2d[7] = vec4(0.117188,-0.140625,-0.199219, 0.117188);
vec2 vInvShadowMapWH = vec2(1.0 / 2048.0);
const int kernelSize = 8;
mediump float P_Z = depth; // p.z;
vec4 p0 = vec4(p.xyz, 1.0);
mediump vec2 rotScale = vec2(kernelRadius.y * 2.0);
float shadowTest = 0.0;
#define KERNEL_STEP_SIZE 2
vec2 rotSample = 2.0 * texture(shadowDepthSampler, randDirTC.xy).xy - 1.0;
rotSample.xy = normalize(rotSample.xy);
rotSample.xy *= (kernelRadius.xy * vInvShadowMapWH.xy);
vec4 rot = vec4(rotSample.x, -rotSample.y, rotSample.y, rotSample.x);
const int kernelOffset = 0;
for(int i=kernelOffset; i<kernelSize; i+=KERNEL_STEP_SIZE) // Loop over taps
{
mediump vec4 sampleDepth = vec4(0.0);
vec4 irr = irreg_kernel_2d[i+0];
sampleDepth.xy = DoubleSampleRotated(depthMap, p0, rot, irr);
sampleDepth.zw = DoubleSampleRotated(depthMap, p0, rot, irreg_kernel_2d[i+1]);
mediump vec4 InShadow;
InShadow.x = ( P_Z < sampleDepth.x + shadowBias ) ? 1. : 0.0;
InShadow.y = ( P_Z < sampleDepth.y + shadowBias ) ? 1. : 0.0;
InShadow.z = ( P_Z < sampleDepth.z + shadowBias ) ? 1. : 0.0;
InShadow.w = ( P_Z < sampleDepth.w + shadowBias ) ? 1. : 0.0;
const mediump float quality = 8.0; // 8 == high
const mediump float fInvSamplNum = (1.0 / quality);
shadowTest += dot(InShadow, vec4(fInvSamplNum));
}
return shadowTest;
}
float DecodeFloatRGBA( vec4 rgba ) {
return (rgba).x;
//return dot( rgba, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0) );
}
float poissonPCFmultitap(vec4 projCoords, float shadowDepth, vec2 uv)
{
const mediump float step = 1.0 - 1.0 / 8.0;
const mediump float fScale = 0.025; // 0.025
mediump float n = 0.0;
mediump vec3 directions[8];
float vSampleScale = 1.0 / 2048.0;
directions[0] = normalize(vec3( 1.0, 1.0, 1.0))*fScale*(n+=step);
directions[1] = normalize(vec3(-1.0,-1.0,-1.0))*fScale*(n+=step);
directions[2] = normalize(vec3(-1.0,-1.0, 1.0))*fScale*(n+=step);
directions[3] = normalize(vec3(-1.0, 1.0,-1.0))*fScale*(n+=step);
directions[4] = normalize(vec3(-1.0, 1.0 ,1.0))*fScale*(n+=step);
directions[5] = normalize(vec3( 1.0,-1.0,-1.0))*fScale*(n+=step);
directions[6] = normalize(vec3( 1.0,-1.0, 1.0))*fScale*(n+=step);
directions[7] = normalize(vec3( 1.0, 1.0,-1.0))*fScale*(n+=step);
mediump vec3 randomSample = texture(shadowNoiseSampler, vec2(64.0, 64.0) * uv.xy / 4.0).xyz * 2.0 - 1.0;
float sum = 0.0;
for( int i = 0; i < 4; i++ ) {
vec3 sampler = reflect(directions[0], randomSample) * vSampleScale;
float pixelDepth = DecodeFloatRGBA( texture(shadowDepthSampler, projCoords.xy+ sampler.xy ) ) ; // + sampler.xy + sampler.z
if( pixelDepth + shadowBias > shadowDepth) {
sum += 1.0;
} else {
sum += 0.0;
}
}
return sum;
}
float linestep(float min, float max, float value) {
return clamp((value - min) / (max - min), 0., 1.);
}
float reduceBleeding(float p_max, float amount) {
return linestep(amount, 1.0, p_max);
}
float ChebyshevUpperBound(vec2 moments, float distance) {
if (distance <= moments.x)
return 1.0;
float g_minVariance = .0007;
float variance = moments.y - (moments.x*moments.x);
variance = max(variance,g_minVariance);
float d = distance - moments.x;
float p_max = variance / (variance + d*d);
return reduceBleeding(p_max, shadowBias);
}
float calculateShadowOcclusion( vec3 worldPosition, vec2 uv ) {
vec4 projCoords = lightViewProjection * vec4(worldPosition, 1.0) ;
float shadowDepth = length( lightPosition - worldPosition ) / 99.0;
projCoords.xy /= projCoords.w;
projCoords = 0.51 * projCoords + 0.5;
vec4 moments = texture( shadowDepthSampler, projCoords.xy );
mediump vec2 randomSample = texture(shadowNoiseSampler, vec2(1024.) * uv / 64.0).xy * 2.0 - 1.0;
float outFrustum = 0.0;
if(projCoords.x < 0.0 || projCoords.x > 1.0) {
return 0.0;
}
if(projCoords.y < 0.0 || projCoords.y > 1.0) {
return 0.0;
}
if(projCoords.z < 1.0) {
return 0.0;
}
return ChebyshevUpperBound(moments.xy, shadowDepth);
return poissonPCFmultitap(projCoords, shadowDepth, uv);
//return PCF( shadowDepthSampler, projCoords, randomSample, shadowDepth );
}
vec3 sampleReflection( vec3 r ) {
return texture(reflectionSampler, r).xyz;
}
vec3 cubemapReflection( deferredMaterialData materialData ) {
vec3 viewDir = ( cameraPosition - materialData.position );
vec3 reflectionVector = reflect(-viewDir, normalize( materialData.normal ));
vec3 reflectionSample = sampleReflection( reflectionVector );
return reflectionSample;
}
const float gamma = 2.2;
float toLinear(float v) {
return pow(v, gamma);
}
vec2 toLinear(vec2 v) {
return pow(v, vec2(gamma));
}
vec3 toLinear(vec3 v) {
return pow(v, vec3(gamma));
}
vec4 toLinear(vec4 v) {
return vec4(toLinear(v.rgb), v.a);
}
vec3 HDR_ACES(const vec3 x) {
// Narkowicz 2015, "ACES Filmic Tone Mapping Curve"
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return (x * (a * x + b)) / (x * (c * x + d) + e);
}
vec3 tonemap(const vec3 x) {
return HDR_ACES(x);
}
float linearToSRGB(float c) {
return (c <= 0.0031308) ? c * 12.92 : (pow(abs(c), 1.0 / 2.4) * 1.055) - 0.055;
}
vec3 linearToSRGB(vec3 c) {
return vec3(linearToSRGB(c.r), linearToSRGB(c.g), linearToSRGB(c.b));
}
#define VIEWMODE_LIT 0.0
#define VIEWMODE_LIGHTNING 1.0
#define VIEWMODE_UNLIT 2.0
#define VIEWMODE_REFLECTION 3.0
#define VIEWMODE_NORMAL 4.0
#define VIEWMODE_AMBIENTOCCLUSION 5.0
#define VIEWMODE_ROUGHNESS 6.0
#define VIEWMODE_SHADOW 7.0
void main() {
vec4 normalDepth = texture(normalSampler, v_uv);
vec4 diffuseRoughness = texture(diffuseSampler, v_uv);
vec4 positionMaterialIndex = texture(infoSampler, v_uv);
vec4 depthNormal = texture(normalSampler, v_uv);
vec4 shadowAmbientOcclusion = texture(ambientOcclusionSampler, v_uv);
// Material properties
deferredMaterialData materialData;
materialData.ambientOcclusion = shadowAmbientOcclusion.x;
materialData.normal = normalize( depthNormal.xyz );
materialData.baseColor = sRGBtoLinear(diffuseRoughness.rgb);
materialData.position = positionMaterialIndex.xyz;
materialData.index = positionMaterialIndex.w;
materialData.roughness = diffuseRoughness.w;
materialData.clearCoat = clearCoat;
materialData.clearCoatRoughness = clearCoatRoughness;
materialData.clearCoatThickness = clearCoatThickness;
materialData.clearCoatColor = clearCoatColor;
materialData.alpha = 1.0;
materialData.shadowOcclusion = shadowAmbientOcclusion.y; //calculateShadowOcclusion( materialData.position, v_uv );
materialData.metallic = metallic;
materialData.reflectance = reflectance;
// Light properties
deferredLightData lightData;
lightData.position = lightPosition;
lightData.direction = lightDirection;
lightData.color = lightColor;
lightData.intensity = lightIntensity;
lightData.inverseFalloff = lightGeometry.x;
lightData.length = lightGeometry.y;
lightData.radius = lightGeometry.z;
if( viewMode == VIEWMODE_LIGHTNING ){
materialData.baseColor = vec3(1.0);
}
vec4 color = physically_based_shading( materialData, lightData ) * materialData.ambientOcclusion;
float exposure = 1.7;
color.rgb *= exposure;
vec3 finalRender = color.rgb;// + diffuse * materialData.ambientOcclusion
// For fxaa
float luma = sqrt( dot(color.rgb, vec3(0.299, 0.587, 0.114)) );
fragmentColor = vec4(linearToSRGB(tonemap(color.rgb)), luma);
if( viewMode == VIEWMODE_UNLIT ){
fragmentColor = vec4(linearToSRGB(tonemap( materialData.baseColor )), luma);
}
if( viewMode == VIEWMODE_REFLECTION ){
vec3 cubeReflectionSample = cubemapReflection( materialData );
fragmentColor = vec4(linearToSRGB(tonemap( cubeReflectionSample )), luma);
}
if( viewMode == VIEWMODE_NORMAL ){
fragmentColor = vec4(materialData.normal, luma);
}
if( viewMode == VIEWMODE_AMBIENTOCCLUSION ){
fragmentColor = vec4(vec3(materialData.ambientOcclusion), luma);
}
if( viewMode == VIEWMODE_ROUGHNESS ){
fragmentColor = vec4(vec3(materialData.roughness), luma);
}
if( viewMode == VIEWMODE_SHADOW ){
fragmentColor = vec4(vec3(materialData.shadowOcclusion), luma);
}
if(materialData.index == 100.0) {
vec3 cubeReflectionSample = cubemapReflection( materialData );
fragmentColor = vec4( cubeReflectionSample, 1.0 );
}
}

142
shaders/depth.shader Executable file
View File

@@ -0,0 +1,142 @@
#version 300 es
in vec3 position;
out vec4 v_position;
out float v_depth;
uniform mat4 view;
uniform mat4 viewProjection;
uniform mat4 world;
void main(void) {
vec4 worldPosition = world * vec4(position.xyz, 1.0);
v_position = viewProjection * worldPosition;
v_depth = v_position.z;
gl_Position = viewProjection * vec4(position.xyz, 1.0);
}
// #keplerEngine - Split
#version 300 es
precision highp float;
in vec4 v_position;
in float v_depth;
out vec4 pixelColor;
uniform float far;
uniform float near;
uniform vec3 diffuseColor;
vec4 pack (float depth)
{
const vec4 bias = vec4(1.0 / 255.0,
1.0 / 255.0,
1.0 / 255.0,
0.0);
float r = depth;
float g = fract(r * 255.0);
float b = fract(g * 255.0);
float a = fract(b * 255.0);
vec4 color = vec4(r, g, b, a);
return color - (color.yzww * bias);
}
vec4 EncodeFloatRGBA( float v ) {
vec4 enc = vec4(1.0, 255.0, 65025.0, 160581375.0) * v;
enc = fract(enc);
enc -= enc.yzww * vec4(1.0/255.0,1.0/255.0,1.0/255.0,0.0);
return enc;
}
vec2 packHalf (float depth)
{
const vec2 bias = vec2(1.0 / 255.0,
0.0);
vec2 colour = vec2(depth, fract(depth * 255.0));
return colour - (colour.yy * bias);
}
vec2 ComputeMoments(float depth) {
vec2 moments;
moments.x = depth;
float dx = dFdx(depth);
float dy = dFdy(depth);
moments.y = depth*depth + 0.25 * (dx*dx + dy*dy);
return moments;
}
float normalizeDepth(float depth, float realFar) {
return (depth) / (realFar);
}
const float PackUpscale = 256. / 255.; // fraction -> 0..1 (including 1)
const float UnpackDownscale = 255. / 256.; // 0..1 -> fraction (excluding 1)
const vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );
const vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );
const float ShiftRight8 = 1. / 256.;
vec4 packDepthToRGBA( const in float v ) {
vec4 r = vec4( fract( v * PackFactors ), v );
r.yzw -= r.xyz * ShiftRight8; // tidy overflow
return r * PackUpscale;
}
float unpackRGBAToDepth( const in vec4 v ) {
return dot( v, UnpackFactors );
}
vec4 DistributePrecision(vec2 Moments)
{
float g_DistributeFactor = 256.0;
float FactorInv = 1.0 / g_DistributeFactor;
// Split precision
vec2 IntPart;
vec2 FracPart = mod(Moments * g_DistributeFactor, IntPart);
// Compose outputs to make reconstruction cheap.
return vec4(IntPart * FactorInv, FracPart);
}
float DecodeFloatRGBA( vec4 rgba ) {
return dot( rgba, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0) );
}
void main() {
float depth = v_position.z ;
depth = depth;
float moment1 = depth;
float moment2 = depth * depth;
float dx = dFdx(depth);
float dy = dFdy(depth);
moment2 += 0.25 * (dx * dx + dy * dy);
pixelColor = vec4(depth, moment2, 0.0, 1.0);
//pixelColor = vec4(diffuseColor, 1.0);
gl_FragDepth = clamp(depth / far, 0.0, 1.0);
}

118
shaders/edgeDetection.shader Executable file
View File

@@ -0,0 +1,118 @@
#version 300 es
in vec3 position;
in vec2 uv;
uniform mat4 viewProjection;
uniform vec2 res;
out vec2 textureCoord;
out vec4 vOffset[ 3 ];
void SMAAEdgeDetectionVS( vec2 texcoord ) {
vOffset[ 0 ] = texcoord.xyxy + res.xyxy * vec4( -1.0, 0.0, 0.0, 1.0 ); // WebGL port note: Changed sign in W component
vOffset[ 1 ] = texcoord.xyxy + res.xyxy * vec4( 1.0, 0.0, 0.0, -1.0 ); // WebGL port note: Changed sign in W component
vOffset[ 2 ] = texcoord.xyxy + res.xyxy * vec4( -2.0, 0.0, 0.0, 2.0 ); // WebGL port note: Changed sign in W component
}
void main(void) {
textureCoord = uv;
SMAAEdgeDetectionVS( textureCoord );
gl_Position = viewProjection * vec4(position, 1.0);
}
// #keplerEngine - Split
#version 300 es
precision highp float;
#define SMAA_THRESHOLD 0.2
uniform vec2 res;
uniform sampler2D material_sampler;
uniform sampler2D sceneSampler;
in vec2 textureCoord;
in vec4 vOffset[ 3 ];
out vec4 fragmentColor;
vec4 SMAAColorEdgeDetectionPS( vec2 texcoord, vec4 offset[3], sampler2D colorTex ) {
vec2 threshold = vec2( SMAA_THRESHOLD, SMAA_THRESHOLD );
// Calculate color deltas:
vec4 delta;
vec3 C = texture( colorTex, texcoord ).rgb;
vec3 Cleft = texture( colorTex, offset[0].xy ).rgb;
vec3 t = abs( C - Cleft );
delta.x = max( max( t.r, t.g ), t.b );
vec3 Ctop = texture( colorTex, offset[0].zw ).rgb;
t = abs( C - Ctop );
delta.y = max( max( t.r, t.g ), t.b );
// We do the usual threshold:
vec2 edges = step( threshold, delta.xy );
// Then discard if there is no edge:
if ( dot( edges, vec2( 1.0, 1.0 ) ) == 0.0 )
discard;
// Calculate right and bottom deltas:
vec3 Cright = texture( colorTex, offset[1].xy ).rgb;
t = abs( C - Cright );
delta.z = max( max( t.r, t.g ), t.b );
vec3 Cbottom = texture( colorTex, offset[1].zw ).rgb;
t = abs( C - Cbottom );
delta.w = max( max( t.r, t.g ), t.b );
// Calculate the maximum delta in the direct neighborhood:
float maxDelta = max( max( max( delta.x, delta.y ), delta.z ), delta.w );
// Calculate left-left and top-top deltas:
vec3 Cleftleft = texture( colorTex, offset[2].xy ).rgb;
t = abs( C - Cleftleft );
delta.z = max( max( t.r, t.g ), t.b );
vec3 Ctoptop = texture( colorTex, offset[2].zw ).rgb;
t = abs( C - Ctoptop );
delta.w = max( max( t.r, t.g ), t.b );
// Calculate the final maximum delta:
maxDelta = max( max( maxDelta, delta.z ), delta.w );
// Local contrast adaptation in action:
edges.xy *= step( 0.5 * maxDelta, delta.xy );
return vec4( edges, 0.0, 0.0 );
}
void main() {
//vec4 sceneColor = vec4( texture( sceneSampler, textureCoord).xyz, 1.0 );
vec4 abc = SMAAColorEdgeDetectionPS( textureCoord, vOffset, material_sampler );
if(length(abc) > .6) {
fragmentColor = vec4(221.0/256.0, 161.0/256.0, 87.0/256.0, 1.0);
} else {
fragmentColor = vec4( 0.0 );
}
}

137
shaders/fxaa.shader Executable file
View File

@@ -0,0 +1,137 @@
#version 300 es
in vec3 position;
in vec2 uv;
uniform vec2 res;
uniform mat4 viewProjection;
out vec2 textureCoord;
out vec2 v_rgbNW;
out vec2 v_rgbNE;
out vec2 v_rgbSW;
out vec2 v_rgbSE;
out vec2 v_rgbM;
void main(void) {
textureCoord = uv;
mediump vec2 fragCoord = textureCoord * res;
vec2 inverseVP = 1.0 / res.xy;
mediump vec2 v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP;
mediump vec2 v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP;
mediump vec2 v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP;
mediump vec2 v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP;
mediump vec2 v_rgbM = vec2(fragCoord * inverseVP);
gl_Position = viewProjection * vec4(position, 1.0);
}
// #keplerEngine - Split
#version 300 es
precision highp float;
uniform sampler2D deferredSampler;
uniform vec2 res;
uniform vec2 inverseScreenSize;
in vec2 textureCoord;
in vec2 v_rgbNW;
in vec2 v_rgbNE;
in vec2 v_rgbSW;
in vec2 v_rgbSE;
in vec2 v_rgbM;
out vec4 fragmentColor;
#ifndef FXAA_REDUCE_MIN
#define FXAA_REDUCE_MIN (1.0/ 128.0)
#endif
#ifndef FXAA_REDUCE_MUL
#define FXAA_REDUCE_MUL (1.0 / 8.0)
#endif
#ifndef FXAA_SPAN_MAX
#define FXAA_SPAN_MAX 8.0
#endif
//optimized version for mobile, where dependent
//texture reads can be a bottleneck
vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution,
vec2 v_rgbNW, vec2 v_rgbNE,
vec2 v_rgbSW, vec2 v_rgbSE,
vec2 v_rgbM) {
vec4 color;
mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y);
vec3 rgbNW = texture(tex, v_rgbNW).xyz;
vec3 rgbNE = texture(tex, v_rgbNE).xyz;
vec3 rgbSW = texture(tex, v_rgbSW).xyz;
vec3 rgbSE = texture(tex, v_rgbSE).xyz;
vec4 texColor = texture(tex, v_rgbM);
vec3 rgbM = texColor.xyz;
vec3 luma = vec3(0.299, 0.587, 0.114);
float lumaNW = dot(rgbNW, luma);
float lumaNE = dot(rgbNE, luma);
float lumaSW = dot(rgbSW, luma);
float lumaSE = dot(rgbSE, luma);
float lumaM = dot(rgbM, luma);
float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
mediump vec2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
(0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
dir * rcpDirMin)) * inverseVP;
vec3 rgbA = 0.5 * (
texture(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz +
texture(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz);
vec3 rgbB = rgbA * 0.5 + 0.25 * (
texture(tex, fragCoord * inverseVP + dir * -0.5).xyz +
texture(tex, fragCoord * inverseVP + dir * 0.5).xyz);
float lumaB = dot(rgbB, luma);
if ((lumaB < lumaMin) || (lumaB > lumaMax))
color = vec4(rgbA, texColor.a);
else
color = vec4(rgbB, texColor.a);
return color;
}
void main() {
//compute FXAA
//
mediump vec2 fragCoord = textureCoord * res;
fragmentColor = fxaa(deferredSampler, fragCoord, res, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM);
//fragmentColor = texture(deferredSampler, textureCoord);
fragmentColor.w = 1.0;
}

1173
shaders/fxaa4.shader Executable file
View File

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,225 @@
/**
* Kepler - Core
* STUDIOS
* All rights reserved.
* Obfuscated by Shader closure (A product of kaj dijkstra)
*/
/**
* Author: Kaj Dijksta
*/
attribute vec2 uv;
attribute vec3 position;
uniform mat4 viewProjection;
varying vec2 v_uv;
void main ( void ) {
v_uv = uv;
gl_Position = viewProjection *vec4 ( position , 1.0 );
}
// #keplerEngine - Split
#ifndef AMBIANT_ONLY
#define AMBIANT_ONLY 0
#endif
precision highp float;
varying vec2 v_uv;
uniform sampler2D infoSampler;
uniform sampler2D randomSampler;
uniform float screenWidth;
uniform float screenHeight;
uniform vec3 cameraPosition;
uniform float far;
uniform vec3 scale[ 32 ];
uniform vec4 kernelRad[ 8 ];
struct fragmentPass { highp vec2 uv; mediump vec4 diffuse; mediump vec3 normal; mediump vec3 positionWorld; mediump float ambiantOcclusion; highp float far; highp float depth; highp float depthNorm; };
float function_0 ( vec3 var_0 , vec3 var_1 , vec3 var_2 , float var_3 )
{
mediump vec3 var_4 = reflect ( -var_2 , var_0 );
return pow ( clamp ( dot ( var_1 , var_4 ) , 0.0 , 1.0 ) , var_3 );
}
float function_1 ( vec2 var_0 ) {
mediump vec4 var_1 = vec4 (.6 , 0.075 , 1.0 , 1.0 );
const mediump float var_2 = 1.0 -1.0 /8.0;
const mediump float var_3 = 0.025;
mediump float var_4 = 0.0;
mediump vec3 var_5[8];
var_5[0] = normalize ( vec3 ( 1.0 , 1.0 , 1.0 ) ) *var_3 *( var_4 += var_2 );
var_5[1] = normalize ( vec3 ( -1.0 , -1.0 , -1.0 ) ) *var_3 *( var_4 += var_2 );
var_5[2] = normalize ( vec3 ( -1.0 , -1.0 , 1.0 ) ) *var_3 *( var_4 += var_2 );
var_5[3] = normalize ( vec3 ( -1.0 , 1.0 , -1.0 ) ) *var_3 *( var_4 += var_2 );
var_5[4] = normalize ( vec3 ( -1.0 , 1.0 , 1.0 ) ) *var_3 *( var_4 += var_2 );
var_5[5] = normalize ( vec3 ( 1.0 , -1.0 , -1.0 ) ) *var_3 *( var_4 += var_2 );
var_5[6] = normalize ( vec3 ( 1.0 , -1.0 , 1.0 ) ) *var_3 *( var_4 += var_2 );
var_5[7] = normalize ( vec3 ( 1.0 , 1.0 , -1.0 ) ) *var_3 *( var_4 += var_2 );
mediump vec3 var_6 = texture2D ( randomSampler , vec2 ( screenWidth , screenHeight ) *var_0 /4.0 ).xyz *2.0 -1.0;
mediump float var_7 = texture2D ( infoSampler , var_0 ).w;
mediump float var_8 = var_7 *far;
mediump vec3 var_9 = var_1.zzw *clamp ( var_8 /5.3 , 0.0 , 1.0 ) *( 1.0 +var_8 /8.0 );
mediump float var_10 = far /var_9.z *0.75;
var_9.xy *= 1.0 /var_8;
var_9.z *= 2.0 /far;
float var_11 = 64.0 /var_9.z;
mediump vec4 var_12[2];
mediump vec4 var_13 = vec4 ( 0.0 );
mediump vec3 var_14;
mediump vec4 var_15;
highp vec4 var_16 = vec4 ( 0.0 );
const float var_17 = 0.2;
vec4 var_18;
var_14 = reflect ( var_5[0] , var_6 ) *var_9;
var_12[0].x = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
var_14.xyz *= var_17;
var_12[1].x = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
var_14 = reflect ( var_5[1] , var_6 ) *var_9;
var_12[0].y = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
var_14.xyz *= var_17;
var_12[1].y = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
var_14 = reflect ( var_5[2] , var_6 ) *var_9;
var_12[0].z = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
var_14.xyz *= var_17;
var_12[1].z = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
var_14 = reflect ( var_5[3] , var_6 ) *var_9;
var_12[0].w = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
var_14.xyz *= var_17;
var_12[1].w = texture2D ( infoSampler , var_0.xy +var_14.xy ).w +var_14.z;
const float var_19 = 0.001;
var_15 = var_7 -var_12[0];
var_18 = var_15 *var_10;
var_16 = ( clamp ( abs ( var_18 ) , 0.0 , 1.0 ) +clamp ( var_18 , 0.0 , 1.0 ) ) /2.0;
var_13 += mix ( clamp ( ( -var_15 ) *var_11 , 0.0 , 1.0 ) , vec4 ( var_19 ) , var_16 );
var_15 = var_7 -var_12[1];
var_18 = var_15 *var_10;
var_16 = ( clamp ( abs ( var_18 ) , 0.0 , 1.0 ) +clamp ( var_18 , 0.0 , 1.0 ) ) /2.0;
var_13 += mix ( clamp ( ( -var_15 ) *var_11 , 0.0 , 1.0 ) , vec4 ( var_19 ) , var_16 );
float var_20 = dot ( var_13 , vec4 ( ( 1.0 /16.0 ) *2.0 ) ) -var_1.y;
var_20 = clamp ( mix ( 0.9 , var_20 , var_1.x ) , 0.0 , 1.0 );
return var_20;
}
float function_2 ( vec2 var_0 , float var_1 , vec3 var_2 )
{
const mediump float var_3 = 0.4;
const mediump int var_4 = 16;
const mediump float var_5 = 0.05;
const mediump float var_6 = var_3 /( float ( var_4 ) /2.0 );
const mediump float var_7 = 1000.0;
mediump float var_8 = 2.8 *( 1.0 +sqrt ( var_5 ) ) /var_7;
const mediump float var_9 = 1.0 -2.8;
mediump vec3 var_10 = texture2D ( randomSampler , vec2 ( screenWidth , screenHeight ) *var_0 /4.0 ).xyz *2.0 -1.0;
mediump vec4 var_11 = vec4 ( var_7 /var_1 );
mediump vec4 var_12 = vec4 ( 0.0 );
for ( int var_13 = 0; var_13 < var_4 /4; var_13 ++)
{
mediump vec4 var_14 = vec4 ( 0.0 );
mediump vec4 var_15 = vec4 ( 0.0 );
mediump vec4 var_16 = vec4 ( 0.0 );
for ( int var_17 = 0; var_17 < 4; ++var_17 )
{
mediump vec3 var_18 = reflect ( scale[4 *var_13 +var_17] , var_10 );
var_16[var_17] = dot ( var_18 , var_2 );
var_18 = ( var_16[var_17] >= 0.0 ) ? var_18 : -var_18;
var_14[var_17] = texture2D ( infoSampler , var_0.xy +var_18.xy ).w;
var_15[var_17] = var_18.z;
}
var_14 = var_14 *var_11;
mediump vec4 var_19 = clamp ( var_7 +var_15 *( 2.0 *var_7 ) -var_14 , 0.0 , 1.0 );
var_19 *= clamp ( var_8 *var_14 +var_9 , 0.0 , 1.0 );
var_19 *= ( abs ( var_16 ) *kernelRad[var_13] );
var_12 += var_19;
}
var_12 *= var_6;
lowp float var_20 = 1.0 -dot ( vec4 ( 1.0 ) , var_12 );
return var_20;
}
float function_3 ( vec2 var_0 , float var_1 , vec3 var_2 )
{
const mediump vec4 var_3 = vec4 ( 1. , 0.075 , 1.0 , 2.0 );
const int var_4 = 8;
const mediump float var_5 = 0.6;
const mediump float var_6 = 1.0 /var_5;
const mediump float var_7 = 2.5;
const mediump float var_8 = var_3.x;
vec3 var_9[16];
var_9[0] = vec3 ( -0.055664 , -0.00371090 , -0.0654297 );
var_9[1] = vec3 ( 0.0173828 , 0.0111328 , 0.0064453 );
var_9[2] = vec3 ( 0.0001953 , 0.008203100000000001 , -0.0060547 );
var_9[3] = vec3 ( 0.0220703 , -0.035937500000000004 , -0.00625 );
var_9[4] = vec3 ( 0.0242188 , 0.012695300000000001 , -0.025 );
var_9[5] = vec3 ( 0.0070313 , -0.0025391000000000003 , 0.014843799999999999 );
var_9[6] = vec3 ( -0.007812 , 0.0013672 , -0.0314453 );
var_9[7] = vec3 ( 0.0117188 , -0.0140625 , -0.019921900000000003 );
var_9[8] = vec3 ( -0.025195 , -0.055859400000000003 , 0.008203100000000001 );
var_9[9] = vec3 ( 0.0308594 , 0.019335900000000003 , 0.0324219 );
var_9[10] = vec3 ( 0.0173828 , -0.0140625 , 0.003125 );
var_9[11] = vec3 ( 0.0179688 , -0.0044922 , 0.004687500000000001 );
var_9[12] = vec3 ( -0.014648 , -0.020117200000000002 , -0.0029297000000000004 );
var_9[13] = vec3 ( -0.030078 , 0.0234375 , 0.0539063 );
var_9[14] = vec3 ( 0.0228516 , 0.0154297 , -0.0119141 );
var_9[15] = vec3 ( -0.011914 , -0.00039060000000000006 , -0.006640600000000001 );
mediump vec3 var_10 = texture2D ( randomSampler , vec2 ( screenWidth , screenHeight ) *var_0 /4.0 ).xyz *2.0 -1.0;
mediump vec4 var_11 = vec4 ( 1.0 /var_1 );
mediump vec4 var_12 = vec4 ( 0.0 );
for ( int var_13 = 0; var_13 < var_4; var_13 += 4 )
{
mediump vec4 var_14 = vec4 ( 0.0 );
mediump vec4 var_15 = vec4 ( 0.0 );
mediump vec4 var_16;
mediump vec4 var_17;
for ( int var_18 = 0; var_18 < 4; ++var_18 )
{
mediump vec3 var_19 = reflect ( var_9[var_13 +var_18] , var_10 );
float var_20 = dot ( var_19 , var_2 );
var_19 = ( var_20 >= 0.0 ) ? var_19 : -var_19;
var_14[var_18] = texture2D ( infoSampler , ( var_0 +var_19.xy ) ).w;
var_15[var_18] = var_19.z;
}
var_14 = var_14 *var_11;
mediump vec4 var_21 = ( 1.0 +var_15 *2. -var_14 ) *var_6;
var_16 = clamp ( var_7 *var_21 , 0.0 , 1.0 );
var_17 = clamp ( 1.0 /var_21 , 0.0 , 1.0 );
var_12 += var_16 *var_17;
}
var_12 *= var_8 /float ( var_4 );
mediump float var_22 = dot ( vec4 ( 1.0 ) , var_12 );
return 1.0 -var_22;
}
mediump vec3 function_4 ( mediump vec2 var_0 ) {
mediump vec2 var_1 = var_0 *4.0 -2.0;
mediump float var_2 = dot ( var_1 , var_1 );
mediump float var_3 = sqrt ( 1.0 -var_2 /4.0 );
mediump vec3 var_4;
var_4.xy = var_1 *var_3;
var_4.z = 1.0 -var_2 /2.0;
return var_4;
}
void main ( void ) {
vec4 var_0 = texture2D ( infoSampler , v_uv );
float var_1 = var_0.w ;
//vec3 var_3 = function_4 ( var_0.yz );
vec3 var_3 = var_0.xyz;
float var_4;
// var_4 = function_1 ( v_uv );
var_4 = function_3 ( v_uv , var_1 , var_3 );
gl_FragColor = vec4 ( var_4 , var_4 , var_4 , 1.0 );
//gl_FragColor = vec4(v_uv, 0.0, 1.0);
//gl_FragColor = vec4(vec3(texture2D ( infoSampler , v_uv).w/10.),1.0);//werkt
// gl_FragColor = vec4(vec3(texture2D ( infoSampler , v_uv).xyz),1.0);//werkt
//gl_FragColor = vec4(texture2D ( randomSampler, v_uv).xyz , 1.0);//werkt
}

1058
shaders/old/color.shader Executable file
View File

File diff suppressed because it is too large Load Diff

83
shaders/old/convolution.shader Executable file
View File

@@ -0,0 +1,83 @@
/**
* Kepler - Core
* Copyright (c) 2010 kaj dijkstra STUDIOS
* All rights reserved.
*/
/**
* Author: Kaj Dijksta
*/
attribute vec3 position;
attribute vec2 uv;
uniform mat4 viewProjection;
varying vec2 v_textureCoord;
void main(void) {
v_textureCoord = uv;
gl_Position = viewProjection * vec4(position, 1.0);
}
// #keplerEngine - Split
precision highp float;
uniform sampler2D image;
uniform vec2 imageIncrement;
uniform sampler2D random;
uniform float merge;
uniform float far;
varying vec2 v_textureCoord;
void main() {
float MSAA1X[13];
//MSAA1X[0] = 0.004433048175243747;
//MSAA1X[1] = 0.054005582622414484;
//MSAA1X[2] = 0.2420362293761143;
//MSAA1X[3] = 0.3990502796524549;
//MSAA1X[4] = 0.2420362293761143;
//MSAA1X[5] = 0.054005582622414484;
//MSAA1X[6] = 0.004433048175243747;
MSAA1X[0] = 0.0022181958546457665;
MSAA1X[1] = 0.008773134791588384;
MSAA1X[2] = 0.027023157602879527;
MSAA1X[3] = 0.06482518513852684;
MSAA1X[4] = 0.12110939007484814;
MSAA1X[5] = 0.17621312278855084;
MSAA1X[6] = 0.19967562749792112;
MSAA1X[7] = 0.17621312278855084;
MSAA1X[8] = 0.12110939007484814;
MSAA1X[9] = 0.06482518513852684;
MSAA1X[10] = 0.027023157602879527;
MSAA1X[11] = 0.008773134791588384;
MSAA1X[12] = 0.0022181958546457665;
vec2 imageCoordTmp = v_textureCoord + (vec2(imageIncrement) * 6.0);
vec4 sum = vec4(0.0);
for (int i = 0; i < 12; ++i) {
sum += texture2D(image, imageCoordTmp) * MSAA1X[i];
imageCoordTmp -= imageIncrement;
}
gl_FragColor = vec4(sum);
}

432
shaders/old/deferred.shader Executable file
View File

@@ -0,0 +1,432 @@
#version 300 es
precision highp float;
in vec2 uv;
in vec3 position;
out vec2 v_uv;
uniform mat4 viewProjection;
void main(void) {
v_uv = uv;
gl_Position = viewProjection * vec4(position, 1.0);
}
// #keplerEngine - Split
#version 300 es
precision highp float;
#define PI 3.14159265358979323846
#define ANISOTROPY 0
#define USE_IES_PROFILE 0
#define TRANSPARENT_MATERIAL 0
#define TRANSLUCENT_MATERIAL 0
#define CUBEMAP_EDGE_FIXUP 0
uniform float lightType;
uniform vec3 lightGeometry;
uniform samplerCube reflectionSampler;
uniform float clearCoat;
uniform vec3 clearCoatColor;
uniform float clearCoatThickness;
uniform float clearCoatRoughness;
uniform vec3 lightDirection;
uniform float attenuation;
uniform float SourceRadius;
uniform float SourceLength;
uniform float environmentLuminance;
uniform vec3 lightPosition;
uniform vec3 lightColor;
uniform float lightIntensity;
uniform float clearCoatIOR;
uniform vec3 cameraPosition;
uniform float metallic;
#include "physically_based_shading.shader"
#define TEXTURED_MATERIAL 1
out vec4 fragmentColor;
in vec2 v_uv;
uniform float reflectance;
uniform float anisotropy;
uniform float roughness;
uniform sampler2D diffuseSampler;
uniform sampler2D normalSampler;
uniform sampler2D tangentSampler;
uniform sampler2D infoSampler;
uniform sampler2D ambientOcclusionSampler;
uniform sampler2D materialSampler;
uniform sampler2D shadowNoiseSampler;
uniform sampler2D shadowDepthSampler;
uniform float luma_z;
uniform float far;
uniform mat4 InvProjection;
uniform mat4 lightViewProjection;
uniform vec2 screenSize;
uniform float shadowBias;
const float PackUpscale = 256. / 255.; // fraction -> 0..1 (including 1)
const float UnpackDownscale = 255. / 256.; // 0..1 -> fraction (excluding 1)
const vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );
const vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );
const float ShiftRight8 = 1. / 256.;
vec4 packDepthToRGBA( const in float v ) {
vec4 r = vec4( fract( v * PackFactors ), v );
r.yzw -= r.xyz * ShiftRight8; // tidy overflow
return r * PackUpscale;
}
float unpackRGBAToDepth( const in vec4 v ) {
return dot( v, UnpackFactors );
}
float shadow_sample(sampler2D depthMap, vec2 coord)
{
return ( texture(depthMap, coord.xy).x );//DecodeFloatRGBA() ;
}
vec2 DoubleSampleRotated(sampler2D depthMap, vec4 p, vec4 rotMatr, vec4 kernel) {
vec4 rotatedOff;
rotatedOff = rotMatr.xyzw * kernel.xxww +
rotMatr.zwxy * kernel.yyzz;
vec4 fetchPos = p.xyxy + rotatedOff;// + rotatedOff
vec2 result;
result.x = shadow_sample(depthMap, fetchPos.xy);
result.y = shadow_sample(depthMap, fetchPos.zw);
return result;
}
float PCF(sampler2D depthMap, vec4 p, vec2 randDirTC, float depth)
{
vec2 kernelRadius = vec2(4.0);
vec4 irreg_kernel_2d[8];
irreg_kernel_2d[0] = vec4(-0.556641,-0.037109,-0.654297, 0.111328);
irreg_kernel_2d[1] = vec4(0.173828,0.111328,0.064453, -0.359375);
irreg_kernel_2d[2] = vec4(0.001953,0.082031,-0.060547, 0.078125);
irreg_kernel_2d[3] = vec4(0.220703,-0.359375,-0.062500, 0.001953);
irreg_kernel_2d[4] = vec4(0.242188,0.126953,-0.250000, -0.140625);
irreg_kernel_2d[5] = vec4(0.070313,-0.025391,0.148438, 0.082031);
irreg_kernel_2d[6] = vec4(-0.078125,0.013672,-0.314453, 0.013672);
irreg_kernel_2d[7] = vec4(0.117188,-0.140625,-0.199219, 0.117188);
vec2 vInvShadowMapWH = vec2(1.0 / 2048.0);
const int kernelSize = 8;
mediump float P_Z = depth; // p.z;
vec4 p0 = vec4(p.xyz, 1.0);
mediump vec2 rotScale = vec2(kernelRadius.y * 2.0);
float shadowTest = 0.0;
#define KERNEL_STEP_SIZE 2
vec2 rotSample = 2.0 * texture(shadowDepthSampler, randDirTC.xy).xy - 1.0;
rotSample.xy = normalize(rotSample.xy);
rotSample.xy *= (kernelRadius.xy * vInvShadowMapWH.xy);
vec4 rot = vec4(rotSample.x, -rotSample.y, rotSample.y, rotSample.x);
const int kernelOffset = 0;
for(int i=kernelOffset; i<kernelSize; i+=KERNEL_STEP_SIZE) // Loop over taps
{
mediump vec4 sampleDepth = vec4(0.0);
vec4 irr = irreg_kernel_2d[i+0];
sampleDepth.xy = DoubleSampleRotated(depthMap, p0, rot, irr);
sampleDepth.zw = DoubleSampleRotated(depthMap, p0, rot, irreg_kernel_2d[i+1]);
mediump vec4 InShadow;
InShadow.x = ( P_Z < sampleDepth.x + shadowBias ) ? 1. : 0.0;
InShadow.y = ( P_Z < sampleDepth.y + shadowBias ) ? 1. : 0.0;
InShadow.z = ( P_Z < sampleDepth.z + shadowBias ) ? 1. : 0.0;
InShadow.w = ( P_Z < sampleDepth.w + shadowBias ) ? 1. : 0.0;
const mediump float quality = 8.0; // 8 == high
const mediump float fInvSamplNum = (1.0 / quality);
shadowTest += dot(InShadow, vec4(fInvSamplNum));
}
return shadowTest;
}
float DecodeFloatRGBA( vec4 rgba ) {
return (rgba).x;
//return dot( rgba, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0) );
}
float poissonPCFmultitap(vec4 projCoords, float shadowDepth, vec2 uv)
{
const mediump float step = 1.0 - 1.0 / 8.0;
const mediump float fScale = 0.025; // 0.025
mediump float n = 0.0;
mediump vec3 directions[8];
float vSampleScale = 1.0 / 2048.0;
directions[0] = normalize(vec3( 1.0, 1.0, 1.0))*fScale*(n+=step);
directions[1] = normalize(vec3(-1.0,-1.0,-1.0))*fScale*(n+=step);
directions[2] = normalize(vec3(-1.0,-1.0, 1.0))*fScale*(n+=step);
directions[3] = normalize(vec3(-1.0, 1.0,-1.0))*fScale*(n+=step);
directions[4] = normalize(vec3(-1.0, 1.0 ,1.0))*fScale*(n+=step);
directions[5] = normalize(vec3( 1.0,-1.0,-1.0))*fScale*(n+=step);
directions[6] = normalize(vec3( 1.0,-1.0, 1.0))*fScale*(n+=step);
directions[7] = normalize(vec3( 1.0, 1.0,-1.0))*fScale*(n+=step);
mediump vec3 randomSample = texture(shadowNoiseSampler, vec2(64.0, 64.0) * uv.xy / 4.0).xyz * 2.0 - 1.0;
float sum = 0.0;
for( int i = 0; i < 4; i++ ) {
vec3 sampler = reflect(directions[0], randomSample) * vSampleScale;
float pixelDepth = DecodeFloatRGBA( texture(shadowDepthSampler, projCoords.xy+ sampler.xy ) ) ; // + sampler.xy + sampler.z
if( pixelDepth + shadowBias > shadowDepth) {
sum += 1.0;
} else {
sum += 0.0;
}
}
return sum;
}
float linestep(float min, float max, float value) {
return clamp((value - min) / (max - min), 0., 1.);
}
float reduceBleeding(float p_max, float amount) {
return linestep(amount, 1.0, p_max);
}
float ChebyshevUpperBound(vec2 moments, float distance) {
if (distance <= moments.x)
return 1.0;
float g_minVariance = .0007;
float variance = moments.y - (moments.x*moments.x);
variance = max(variance,g_minVariance);
float d = distance - moments.x;
float p_max = variance / (variance + d*d);
return reduceBleeding(p_max, shadowBias);
}
float calculateShadowOcclusion( vec3 worldPosition, vec2 uv ) {
vec4 projCoords = lightViewProjection * vec4(worldPosition, 1.0) ;
float shadowDepth = length( lightPosition - worldPosition ) / 99.0;
projCoords.xy /= projCoords.w;
projCoords = 0.51 * projCoords + 0.5;
vec4 moments = texture( shadowDepthSampler, projCoords.xy );
mediump vec2 randomSample = texture(shadowNoiseSampler, vec2(1024.) * uv / 64.0).xy * 2.0 - 1.0;
float outFrustum = 0.0;
if(projCoords.x < 0.0 || projCoords.x > 1.0) {
return 0.0;
}
if(projCoords.y < 0.0 || projCoords.y > 1.0) {
return 0.0;
}
if(projCoords.z < 1.0) {
return 0.0;
}
return ChebyshevUpperBound(moments.xy, shadowDepth);
return poissonPCFmultitap(projCoords, shadowDepth, uv);
//return PCF( shadowDepthSampler, projCoords, randomSample, shadowDepth );
}
vec3 sampleReflection( vec3 r ) {
return texture(reflectionSampler, r).xyz;
}
vec3 cubemapReflection( deferredMaterialData materialData ) {
vec3 viewDir = ( cameraPosition - materialData.position );
vec3 reflectionVector = reflect(-viewDir, normalize( materialData.normal ));
vec3 reflectionSample = sampleReflection( reflectionVector );
return reflectionSample;
}
const float gamma = 2.2;
float toLinear(float v) {
return pow(v, gamma);
}
vec2 toLinear(vec2 v) {
return pow(v, vec2(gamma));
}
vec3 toLinear(vec3 v) {
return pow(v, vec3(gamma));
}
vec4 toLinear(vec4 v) {
return vec4(toLinear(v.rgb), v.a);
}
vec3 HDR_ACES(const vec3 x) {
// Narkowicz 2015, "ACES Filmic Tone Mapping Curve"
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return (x * (a * x + b)) / (x * (c * x + d) + e);
}
vec3 tonemap(const vec3 x) {
return HDR_ACES(x);
}
float linearToSRGB(float c) {
return (c <= 0.0031308) ? c * 12.92 : (pow(abs(c), 1.0 / 2.4) * 1.055) - 0.055;
}
vec3 linearToSRGB(vec3 c) {
return vec3(linearToSRGB(c.r), linearToSRGB(c.g), linearToSRGB(c.b));
}
void main() {
deferredMaterialData materialData;
float gamma = 2.2;
vec4 normalDepth = texture(normalSampler, v_uv);
vec4 diffuseRoughness = texture(diffuseSampler, v_uv);
vec4 positionMaterialIndex = texture(infoSampler, v_uv);
vec4 depthNormal = texture(normalSampler, v_uv);
vec4 shadowAmbientOcclusion = texture(ambientOcclusionSampler, v_uv);
materialData.ambientOcclusion = shadowAmbientOcclusion.x;
materialData.normal = normalize( depthNormal.xyz );
materialData.baseColor = sRGBtoLinear(diffuseRoughness.rgb);
materialData.position = positionMaterialIndex.xyz;
materialData.index = positionMaterialIndex.w;
materialData.roughness = diffuseRoughness.w;
materialData.alpha = 1.0;
materialData.shadowOcclusion = shadowAmbientOcclusion.y; //calculateShadowOcclusion( materialData.position, v_uv );
materialData.metallic = metallic;
materialData.reflectance = reflectance;
//materialData.tangent = tangent;
//materialData.shadowOcclusion = 1.0;
//materialData.baseColor = vec3(1.0);
vec4 color = physically_based_shading( materialData );
float exposure = 1.7;
color.rgb *= exposure;
vec3 finalRender = color.rgb;// + diffuse * .2
// For fxaa
float luma = sqrt( dot(color.rgb, vec3(0.299, 0.587, 0.114)) );
if(luma_z == 1.0)
//if(v_uv.x < 0.5)
fragmentColor = vec4(linearToSRGB(tonemap(color.rgb)), luma);
//else
// fragmentColor = vec4(vec3(materialData.ambientOcclusion), 1.0);
//fragmentColor = vec4(linearToSRGB(tonemap(color.rgb)), 1.0);
if(materialData.index == 100.0) {
vec3 cubeReflectionSample = cubemapReflection( materialData );
fragmentColor = vec4( cubeReflectionSample, 1.0 );
}
}

142
shaders/old/depth.shader Executable file
View File

@@ -0,0 +1,142 @@
#version 300 es
in vec3 position;
out vec4 v_position;
out float v_depth;
uniform mat4 view;
uniform mat4 viewProjection;
uniform mat4 world;
void main(void) {
vec4 worldPosition = world * vec4(position.xyz, 1.0);
v_position = viewProjection * worldPosition;
v_depth = v_position.z;
gl_Position = viewProjection * vec4(position.xyz, 1.0);
}
// #keplerEngine - Split
#version 300 es
precision highp float;
in vec4 v_position;
in float v_depth;
out vec4 pixelColor;
uniform float far;
uniform float near;
uniform vec3 diffuseColor;
vec4 pack (float depth)
{
const vec4 bias = vec4(1.0 / 255.0,
1.0 / 255.0,
1.0 / 255.0,
0.0);
float r = depth;
float g = fract(r * 255.0);
float b = fract(g * 255.0);
float a = fract(b * 255.0);
vec4 color = vec4(r, g, b, a);
return color - (color.yzww * bias);
}
vec4 EncodeFloatRGBA( float v ) {
vec4 enc = vec4(1.0, 255.0, 65025.0, 160581375.0) * v;
enc = fract(enc);
enc -= enc.yzww * vec4(1.0/255.0,1.0/255.0,1.0/255.0,0.0);
return enc;
}
vec2 packHalf (float depth)
{
const vec2 bias = vec2(1.0 / 255.0,
0.0);
vec2 colour = vec2(depth, fract(depth * 255.0));
return colour - (colour.yy * bias);
}
vec2 ComputeMoments(float depth) {
vec2 moments;
moments.x = depth;
float dx = dFdx(depth);
float dy = dFdy(depth);
moments.y = depth*depth + 0.25 * (dx*dx + dy*dy);
return moments;
}
float normalizeDepth(float depth, float realFar) {
return (depth) / (realFar);
}
const float PackUpscale = 256. / 255.; // fraction -> 0..1 (including 1)
const float UnpackDownscale = 255. / 256.; // 0..1 -> fraction (excluding 1)
const vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );
const vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );
const float ShiftRight8 = 1. / 256.;
vec4 packDepthToRGBA( const in float v ) {
vec4 r = vec4( fract( v * PackFactors ), v );
r.yzw -= r.xyz * ShiftRight8; // tidy overflow
return r * PackUpscale;
}
float unpackRGBAToDepth( const in vec4 v ) {
return dot( v, UnpackFactors );
}
vec4 DistributePrecision(vec2 Moments)
{
float g_DistributeFactor = 256.0;
float FactorInv = 1.0 / g_DistributeFactor;
// Split precision
vec2 IntPart;
vec2 FracPart = mod(Moments * g_DistributeFactor, IntPart);
// Compose outputs to make reconstruction cheap.
return vec4(IntPart * FactorInv, FracPart);
}
float DecodeFloatRGBA( vec4 rgba ) {
return dot( rgba, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0) );
}
void main() {
float depth = v_position.z ;
depth = depth;
float moment1 = depth;
float moment2 = depth * depth;
float dx = dFdx(depth);
float dy = dFdy(depth);
moment2 += 0.25 * (dx * dx + dy * dy);
pixelColor = vec4(depth, moment2, 0.0, 1.0);
//pixelColor = vec4(diffuseColor, 1.0);
gl_FragDepth = clamp(depth / far, 0.0, 1.0);
}

137
shaders/old/fxaa.shader Executable file
View File

@@ -0,0 +1,137 @@
#version 300 es
in vec3 position;
in vec2 uv;
uniform vec2 res;
uniform mat4 viewProjection;
out vec2 textureCoord;
out vec2 v_rgbNW;
out vec2 v_rgbNE;
out vec2 v_rgbSW;
out vec2 v_rgbSE;
out vec2 v_rgbM;
void main(void) {
textureCoord = uv;
mediump vec2 fragCoord = textureCoord * res;
vec2 inverseVP = 1.0 / res.xy;
mediump vec2 v_rgbNW = (fragCoord + vec2(-1.0, -1.0)) * inverseVP;
mediump vec2 v_rgbNE = (fragCoord + vec2(1.0, -1.0)) * inverseVP;
mediump vec2 v_rgbSW = (fragCoord + vec2(-1.0, 1.0)) * inverseVP;
mediump vec2 v_rgbSE = (fragCoord + vec2(1.0, 1.0)) * inverseVP;
mediump vec2 v_rgbM = vec2(fragCoord * inverseVP);
gl_Position = viewProjection * vec4(position, 1.0);
}
// #keplerEngine - Split
#version 300 es
precision highp float;
uniform sampler2D deferredSampler;
uniform vec2 res;
uniform vec2 inverseScreenSize;
in vec2 textureCoord;
in vec2 v_rgbNW;
in vec2 v_rgbNE;
in vec2 v_rgbSW;
in vec2 v_rgbSE;
in vec2 v_rgbM;
out vec4 fragmentColor;
#ifndef FXAA_REDUCE_MIN
#define FXAA_REDUCE_MIN (1.0/ 128.0)
#endif
#ifndef FXAA_REDUCE_MUL
#define FXAA_REDUCE_MUL (1.0 / 8.0)
#endif
#ifndef FXAA_SPAN_MAX
#define FXAA_SPAN_MAX 8.0
#endif
//optimized version for mobile, where dependent
//texture reads can be a bottleneck
vec4 fxaa(sampler2D tex, vec2 fragCoord, vec2 resolution,
vec2 v_rgbNW, vec2 v_rgbNE,
vec2 v_rgbSW, vec2 v_rgbSE,
vec2 v_rgbM) {
vec4 color;
mediump vec2 inverseVP = vec2(1.0 / resolution.x, 1.0 / resolution.y);
vec3 rgbNW = texture(tex, v_rgbNW).xyz;
vec3 rgbNE = texture(tex, v_rgbNE).xyz;
vec3 rgbSW = texture(tex, v_rgbSW).xyz;
vec3 rgbSE = texture(tex, v_rgbSE).xyz;
vec4 texColor = texture(tex, v_rgbM);
vec3 rgbM = texColor.xyz;
vec3 luma = vec3(0.299, 0.587, 0.114);
float lumaNW = dot(rgbNW, luma);
float lumaNE = dot(rgbNE, luma);
float lumaSW = dot(rgbSW, luma);
float lumaSE = dot(rgbSE, luma);
float lumaM = dot(rgbM, luma);
float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));
mediump vec2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));
float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) *
(0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);
float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
dir * rcpDirMin)) * inverseVP;
vec3 rgbA = 0.5 * (
texture(tex, fragCoord * inverseVP + dir * (1.0 / 3.0 - 0.5)).xyz +
texture(tex, fragCoord * inverseVP + dir * (2.0 / 3.0 - 0.5)).xyz);
vec3 rgbB = rgbA * 0.5 + 0.25 * (
texture(tex, fragCoord * inverseVP + dir * -0.5).xyz +
texture(tex, fragCoord * inverseVP + dir * 0.5).xyz);
float lumaB = dot(rgbB, luma);
if ((lumaB < lumaMin) || (lumaB > lumaMax))
color = vec4(rgbA, texColor.a);
else
color = vec4(rgbB, texColor.a);
return color;
}
void main() {
//compute FXAA
//
mediump vec2 fragCoord = textureCoord * res;
fragmentColor = fxaa(deferredSampler, fragCoord, res, v_rgbNW, v_rgbNE, v_rgbSW, v_rgbSE, v_rgbM);
//fragmentColor = texture(deferredSampler, textureCoord);
fragmentColor.w = 1.0;
}

1173
shaders/old/fxaa4.shader Executable file
View File

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,387 @@
struct deferredMaterialData {
vec3 normal;
vec3 baseColor;
vec3 diffuse;
vec3 position;
vec3 vertex;
vec3 specularReflectance;
float reflectance;
float metallic;
float roughness;
float clearCoatRoughness;
float geometricRoughness;
float ambientOcclusion;
float shadowOcclusion;
float eta;
float refracted_NoV;
float index;
float alpha;
};
float D_GGX(float NoH, float a) {
float a2 = a * a;
float f = (NoH * a2 - NoH) * NoH + 1.0;
return a2 / (PI * f * f);
}
float D_GGX_Anisotropy(float NoH, vec3 h, vec3 x, vec3 y, float ax, float ay) {
float XoH = dot(x, h);
float YoH = dot(y, h);
float d = XoH * XoH * (ax * ax) + YoH * YoH * (ay * ay) + NoH * NoH;
return (ax * ay) / (PI * d * d);
}
vec3 F_Schlick(float VoH, vec3 specularReflectance, float f90) {
return specularReflectance + (vec3(f90) - specularReflectance) * pow(1.0 - VoH, 5.0);
}
float V_SmithGGXCorrelated(float NoV, float NoL, float a) {
float a2 = a * a;
float GGXL = NoV * sqrt((-NoL * a2 + NoL) * NoL + a2);
float GGXV = NoL * sqrt((-NoV * a2 + NoV) * NoV + a2);
return 0.5 / (GGXV + GGXL);
}
float Fd_Lambert() {
return 1.0 / PI;
}
float F_Schlick_Scalar(float VoH, float specularReflectance, float f90) {
return specularReflectance + (f90 - specularReflectance) * pow(1.0 - VoH, 5.0);
}
float square(float v) {
return v * v;
}
vec3 irradianceSH(vec3 n) {
return
vec3(0.754553530212464, 0.748541695286661, 0.790922541330174)
+ vec3(-0.083855089181764, 0.092536341322488, 0.322767327275582) * (n.y)
+ vec3(0.308154551673257, 0.366799355358085, 0.466705760819624) * (n.z)
+ vec3(-0.188887618191928, -0.277403749518126, -0.377844811540716) * (n.x)
+ vec3(-0.252782448589491, -0.316051613736677, -0.396141020484574) * (n.y * n.x)
+ vec3(0.071362454444021, 0.159789075773366, 0.29059362717571) * (n.y * n.z)
+ vec3(-0.031040420617065, -0.031141089772695, -0.031044001883204) * (3.0 * n.z * n.z - 1.0)
+ vec3(-0.161001896026477, -0.203649521035777, -0.246641086569566) * (n.z * n.x)
+ vec3(0.045710934605387, 0.048121779682969, 0.046326375668417) * (n.x * n.x - n.y * n.y);
}
vec2 prefilteredDFGKaris(float NoV, float roughness) {
// see https://www.unrealengine.com/blog/physically-based-shading-on-mobile
const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
const vec4 c1 = vec4( 1.0, 0.0425, 1.040, -0.040);
vec4 r = roughness * c0 + c1;
float a004 = min(r.x * r.x, exp2(-9.28 * NoV)) * r.x + r.y;
return vec2(-1.04, 1.04) * a004 + r.zw;
}
float sRGBtoLinear(float c) {
return (c <= 0.04045) ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4);
}
vec3 sRGBtoLinear(vec3 c) {
return vec3(sRGBtoLinear(c.r), sRGBtoLinear(c.g), sRGBtoLinear(c.b));
}
vec3 decodeEnvironmentMap(vec4 c) {
return c.rgb;//;sRGBtoLinear(c.rgb) ;
}
float getSquareFalloffAttenuation(float distanceSquare) {
float factor = distanceSquare * lightGeometry.x;
float smoothFactor = max(1.0 - factor * factor, 0.0);
return smoothFactor * smoothFactor;
}
float getPhotometricAttenuation(vec3 lightToPos, vec3 lightDir) {
return 1.0;
}
float getAngleAttenuation(vec3 l, vec3 lightDir) {
float cd = dot(lightDir, l);
float attenuation = clamp(cd * lightGeometry.y + lightGeometry.z, 0.0, 1.0);
return attenuation * attenuation;
}
vec3 beerLambert(float NoV, float NoL, vec3 alpha, float d) {
return exp(alpha * -(d * ((NoL + NoV) / max(NoL * NoV, 1e-3))));
}
vec3 fixCubemapLookup(vec3 v, float lod) {
vec3 r = abs(v);
float M = max(max(v.x, v.y), v.z);
float scale = 1.0 - exp2(lod) * (1.0 / 256.0);
if (v.x != M) v.x *= scale;
if (v.y != M) v.y *= scale;
if (v.z != M) v.z *= scale;
return v;
}
vec3 evaluateSpecularIBL(vec3 r, float roughness) {
float lod = 5.0 * roughness;
r = fixCubemapLookup(r, lod);
return decodeEnvironmentMap( texture(reflectionSampler, r, lod) ); //, lod * 3.
}
float computeSpecularambientOcclusion(float NoV, float ambientOcclusion, float roughness) {
return clamp(pow(NoV + ambientOcclusion, exp2(-16.0 * roughness - 1.0)) - 1.0 + ambientOcclusion, 0.0, 1.0);
}
vec3 getSpecularDominantDirection(vec3 n, vec3 r, float roughness) {
float s = 1.0 - roughness;
return mix(n, r, s * (sqrt(s) + roughness));
}
vec3 evaluateIBL( deferredMaterialData materialData ) { //, deferredLightData params
vec3 normal = materialData.normal;
vec3 vertex = materialData.vertex;
float roughness = materialData.roughness;
float NoV = max( dot(normal, vertex), 0.0 );
#if ANISOTROPY == 1
vec3 t = normalize( materialData.tangent );
vec3 b = normalize( cross(t, normal));
vec3 anisotropicTangent = cross(-vertex, b);
vec3 anisotropicNormal = cross(anisotropicTangent, b);
vec3 bentNormal = normalize(mix(normal, anisotropicNormal, anisotropy));
vec3 r = reflect( vertex, bentNormal );
#else
vec3 r = reflect( -vertex, normal );
r = getSpecularDominantDirection(normal, r, roughness * roughness);
#endif
float NoR = max( dot(r, normal), 0.0);
// specular indirect
vec3 indirectSpecular = evaluateSpecularIBL( reflect(vertex, normal), roughness);
// horizon occlusion, can be removed for performance
//float horizon = min(1.0 + NoR, 1.0);
//indirectSpecular *= horizon * horizon;
vec2 env = prefilteredDFGKaris( NoV, materialData.roughness );
// we should multiply env.y by f90 for more accurate results
vec3 specularColor = materialData.specularReflectance * env.x + env.y * (1.0 - clearCoat) *
clamp(dot(materialData.specularReflectance, vec3(50.0 * 0.33)), 0.0, 1.0);
// diffuse indirect
vec3 indirectDiffuse = max(irradianceSH(normal), 0.0) * Fd_Lambert();
// ambient occlusion
float ambientOcclusionFade = clamp(dot(normalize(normal), vertex), 0.0, 1.0);
float ambientOcclusion = mix(1.0, materialData.ambientOcclusion, ambientOcclusionFade);
indirectDiffuse *= ambientOcclusion;
//indirectDiffuse *= 4.0;
// TODO: Not really useful without SSambientOcclusion/HBambientOcclusion/etc.
indirectSpecular *= computeSpecularambientOcclusion(NoV, ambientOcclusion, materialData.roughness);
// clear coat
float Fcc = F_Schlick_Scalar(NoV, 0.04, 1.0) * 0.2;
#if ANISOTROPY == 1
// We used the bent normal for the base layer
r = reflect(-vertex, normal);
#endif
vec3 indirectClearCoatSpecular = evaluateSpecularIBL(reflect(vertex, normal), materialData.clearCoatRoughness);
vec3 clearCoatAbsorption = mix(vec3(1.0),
beerLambert(materialData.refracted_NoV, materialData.refracted_NoV, clearCoatColor, clearCoatThickness),
clearCoat);
// indirect contribution
vec3 color =
(materialData.diffuse * indirectDiffuse + indirectSpecular * specularColor)//kaj materialData.diffuse * indirectDiffuse
* (1.0 - Fcc) * clearCoatAbsorption +
indirectClearCoatSpecular * Fcc;
#if TRANSLUCENT_MATERIAL == 1
indirectDiffuse = max(irradianceSH(-vertex), 0.0) * Fd_Lambert();
vec3 tL = -vertex + normal * translucencyDistortion;
float tD = pow(clamp(dot(vertex, -tL), 0.0, 1.0), translucencyPower) * translucencyScale;
vec3 tT = (tD + translucencyAmbient) * texture(translucencyThicknessMap, outUV).r;
color.rgb += materialData.diffuse * indirectDiffuse * tT;
#endif
return color;
}
vec3 getNormal(vec3 n) {
return normalize(n);
}
vec3 evaluateLight( deferredMaterialData params ) {
vec3 n = params.normal;
vec3 v = params.vertex;
vec3 l;
float NoL;
float energy;
float attenuation;
vec3 lightDir = normalize(lightDirection);
float linearRoughness = params.roughness * params.roughness;
vec3 r = reflect(-v, n);
if (lightType == 1.0) {
l = -lightDir;
// Disc area light
vec3 sunDir = -lightDir;
float e = sin(radians(0.53));
float d = cos(radians(0.53));
float DoR = dot(sunDir, r);
vec3 s = r - DoR * sunDir;
l = DoR < d ? normalize(d * sunDir + normalize(s) * e) : r;
NoL = dot(n, l);
energy = 1.0;
attenuation = 1.0;
} else if (lightType == 0.0) {
vec3 posToLight = lightPosition - params.position;
float distanceSquare = dot(posToLight, posToLight);
l = normalize(posToLight);
NoL = dot(n, l);
energy = 50.0;
attenuation = getSquareFalloffAttenuation(distanceSquare);
attenuation *= 1.0 / max(distanceSquare, 1e-4);
attenuation *= getPhotometricAttenuation(-l, lightDir);
// if (lightGeometry.w >= 1.0) {
// attenuation *= getAngleAttenuation(l, -lightDir);
// }
}
vec3 h = normalize(v + l);
NoL = clamp(NoL, 0.0, 1.0);
float NoV = abs(dot(n, v)) + 1e-5;
float NoH = clamp(dot(n, h), 0.0, 1.0);
float LoH = clamp(dot(l, h), 0.0, 1.0);
// specular BRDF
#if ANISOTROPY == 1
vec3 t = normalize(v_tangent.xyz);
vec3 b = normalize(cross(t, n));
float aspect = inversesqrt(1.0 - anisotropy * 0.9);
float ax = 1.0 / (linearRoughness * aspect);
float ay = aspect / linearRoughness;
float D = D_GGX_Anisotropy(NoH, h, t, b, ax, ay);
#else
float D = D_GGX(NoH, linearRoughness);
#endif
vec3 F = F_Schlick(LoH, params.specularReflectance, clamp(dot(params.specularReflectance, vec3(50.0 * 0.33)), 0.0, 1.0));
float V = V_SmithGGXCorrelated(NoV, NoL, linearRoughness);
vec3 Fr = (D * V) * F;
// diffuse BRDF
vec3 Fd = params.diffuse * Fd_Lambert();// * 3.6
// clear coat
float linearClearCoatRoughness = params.clearCoatRoughness * params.clearCoatRoughness;
float Dcc = D_GGX(NoH, linearClearCoatRoughness);
float Fcc = F_Schlick_Scalar(LoH, 0.04, 1.0) * clearCoat;
float Vcc = V_SmithGGXCorrelated(NoV, NoL, linearClearCoatRoughness);
float FrCC = Dcc * Vcc * Fcc;
vec3 refracted_l = -refract(l, n, params.eta);
float refracted_NoL = clamp(dot(n, refracted_l), 0.0, 1.0);
vec3 clearCoatAbsorption = mix(vec3(1.0),
beerLambert(params.refracted_NoV, refracted_NoL, clearCoatColor, clearCoatThickness),
clearCoat);
// direct contribution
vec3 color = (attenuation * NoL) * lightColor * lightIntensity * energy *
((Fd + Fr) * (1.0 - Fcc) * clearCoatAbsorption + FrCC);
#if TRANSLUCENT_MATERIAL == 1
vec3 tL = l + n * translucencyDistortion;
float tD = exp2(clamp(dot(v, -tL), 0.0, 1.0) * translucencyPower - translucencyPower) * translucencyScale;
vec3 tT = attenuation * lightIntensity * (tD + translucencyAmbient) * texture(translucencyThicknessMap, outUV).r;
color.rgb += Fd * lightColor * tT;
#endif
// micro-shadowing
float aperture = 2.0 * params.ambientOcclusion * params.ambientOcclusion * params.shadowOcclusion;
float microShadow = clamp(abs(dot(l, n)) + aperture - 1.0, 0.0, 1.0);
color.rgb *= microShadow;
return color;
}
vec4 physically_based_shading( deferredMaterialData materialData ) {
//deferredLightData lightData;
materialData.diffuse = (1.0 - metallic) * materialData.baseColor;
// Geometric AA
vec3 ndFdx = dFdx( materialData.normal );
vec3 ndFdy = dFdy( materialData.normal );
materialData.geometricRoughness = pow( max( dot(ndFdx, ndFdx), dot(ndFdy, ndFdy) ), 0.333);
materialData.roughness = max(materialData.roughness, materialData.geometricRoughness);
materialData.specularReflectance = 0.16 *
materialData.reflectance * materialData.reflectance * (1.0 - metallic) +
materialData.baseColor * materialData.metallic;
materialData.clearCoatRoughness = max( mix(0.089, 0.6, clearCoatRoughness), materialData.geometricRoughness);
materialData.eta = 1.0 / clearCoatIOR;
materialData.vertex = normalize( cameraPosition - materialData.position ); // v
vec3 refracted_vertex = -refract(materialData.vertex, materialData.normal, materialData.eta);
materialData.refracted_NoV = clamp(dot(materialData.normal, refracted_vertex), 0.0, 1.0);
float alpha = 1.0;
// indirect lighting
vec3 color = evaluateIBL( materialData );
color *= environmentLuminance;
color += evaluateLight(materialData);
return vec4(color, alpha);
}

234
shaders/old/shadow.shader Executable file
View File

@@ -0,0 +1,234 @@
/**
* Author: Kaj Dijksta
*/
attribute vec3 position;
attribute vec2 uv;
uniform mat4 viewProjection;
varying vec2 v_textureCoord;
void main(void) {
v_textureCoord = uv;
gl_Position = viewProjection * vec4(position, 1.0);
}
// #keplerEngine - Split
precision highp float;
uniform sampler2D positionSampler;
uniform sampler2D shadowDepthSampler;
uniform sampler2D shadowNoiseSampler;
uniform sampler2D ambientOcclusionSampler;
uniform float bias;
uniform float minVariance;
uniform vec3 lightPosition;
uniform mat4 lightViewProjection;
varying vec2 v_textureCoord;
float DecodeFloatRGBA( vec4 rgba ) {
return (rgba).x;
//return dot( rgba, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0) );
}
float poissonPCFmultitap(vec4 projCoords, float shadowDepth, vec2 uv)
{
const mediump float step = 1.0 - 1.0 / 8.0;
const mediump float fScale = 0.025; // 0.025
mediump float n = 0.0;
mediump vec3 directions[8];
float vSampleScale = 1.0 / 2048.0;
directions[0] = normalize(vec3( 1.0, 1.0, 1.0))*fScale*(n+=step);
directions[1] = normalize(vec3(-1.0,-1.0,-1.0))*fScale*(n+=step);
directions[2] = normalize(vec3(-1.0,-1.0, 1.0))*fScale*(n+=step);
directions[3] = normalize(vec3(-1.0, 1.0,-1.0))*fScale*(n+=step);
directions[4] = normalize(vec3(-1.0, 1.0 ,1.0))*fScale*(n+=step);
directions[5] = normalize(vec3( 1.0,-1.0,-1.0))*fScale*(n+=step);
directions[6] = normalize(vec3( 1.0,-1.0, 1.0))*fScale*(n+=step);
directions[7] = normalize(vec3( 1.0, 1.0,-1.0))*fScale*(n+=step);
mediump vec3 randomSample = texture2D(shadowNoiseSampler, vec2(64.0, 64.0) * uv.xy / 4.0).xyz * 2.0 - 1.0;
float sum = 0.0;
// for( int i = 0; i < 4; i++ ) {
// vec3 sampler = reflect(directions[0], randomSample) * vSampleScale;
float pixelDepth = DecodeFloatRGBA( texture2D(shadowDepthSampler, projCoords.xy ) ) ; // + sampler.xy + sampler.z
if( pixelDepth + bias > shadowDepth) {
sum += 1.0;
} else {
sum += 0.5;
}
// }
return sum;
}
float shadow_sample(sampler2D depthMap, vec2 coord)
{
return ( texture2D(depthMap, coord.xy).x );
}
vec2 DoubleSampleRotated(sampler2D depthMap, vec4 p, vec4 rotMatr, vec4 kernel) {
vec4 rotatedOff;
rotatedOff = rotMatr.xyzw * kernel.xxww +
rotMatr.zwxy * kernel.yyzz;
vec4 fetchPos = p.xyxy + rotatedOff;// + rotatedOff
vec2 result;
result.x = shadow_sample(depthMap, fetchPos.xy);
result.y = shadow_sample(depthMap, fetchPos.zw);
return result;
}
float PCF(sampler2D depthMap, vec4 p, vec2 randDirTC, float depth)
{
vec2 kernelRadius = vec2(4.0);
vec4 irreg_kernel_2d[8];
irreg_kernel_2d[0] = vec4(-0.556641,-0.037109,-0.654297, 0.111328);
irreg_kernel_2d[1] = vec4(0.173828,0.111328,0.064453, -0.359375);
irreg_kernel_2d[2] = vec4(0.001953,0.082031,-0.060547, 0.078125);
irreg_kernel_2d[3] = vec4(0.220703,-0.359375,-0.062500, 0.001953);
irreg_kernel_2d[4] = vec4(0.242188,0.126953,-0.250000, -0.140625);
irreg_kernel_2d[5] = vec4(0.070313,-0.025391,0.148438, 0.082031);
irreg_kernel_2d[6] = vec4(-0.078125,0.013672,-0.314453, 0.013672);
irreg_kernel_2d[7] = vec4(0.117188,-0.140625,-0.199219, 0.117188);
vec2 vInvShadowMapWH = vec2(1.0 / 2048.0);
const int kernelSize = 8;
mediump float P_Z = depth; // p.z;
vec4 p0 = vec4(p.xyz, 1.0);
mediump vec2 rotScale = vec2(kernelRadius.y * 2.0);
float shadowTest = 0.0;
#define KERNEL_STEP_SIZE 2
vec2 rotSample = 2.0 * texture2D(shadowDepthSampler, randDirTC.xy).xy - 1.0;
rotSample.xy = normalize(rotSample.xy);
rotSample.xy *= (kernelRadius.xy * vInvShadowMapWH.xy);
vec4 rot = vec4(rotSample.x, -rotSample.y, rotSample.y, rotSample.x);
const int kernelOffset = 0;
for(int i=kernelOffset; i<kernelSize; i+=KERNEL_STEP_SIZE) // Loop over taps
{
mediump vec4 sampleDepth = vec4(0.0);
vec4 irr = irreg_kernel_2d[i+0];
sampleDepth.xy = DoubleSampleRotated(depthMap, p0, rot, irr);
sampleDepth.zw = DoubleSampleRotated(depthMap, p0, rot, irreg_kernel_2d[i+1]);
mediump vec4 InShadow;
InShadow.x = ( P_Z < sampleDepth.x + bias ) ? 1. : 0.0;
InShadow.y = ( P_Z < sampleDepth.y + bias ) ? 1. : 0.0;
InShadow.z = ( P_Z < sampleDepth.z + bias ) ? 1. : 0.0;
InShadow.w = ( P_Z < sampleDepth.w + bias ) ? 1. : 0.0;
const mediump float quality = 8.0; // 8 == high
const mediump float fInvSamplNum = (1.0 / quality);
shadowTest += dot(InShadow, vec4(fInvSamplNum));
}
return shadowTest;
}
float linestep(float min, float max, float value) {
return clamp((value - min) / (max - min), 0., 1.);
}
float reduceBleeding(float p_max, float amount) {
return linestep(amount, 1.0, p_max);
}
float ChebyshevUpperBound(vec2 moments, float distance) {
if (distance <= moments.x)
return 1.0;
float variance = moments.y - (moments.x*moments.x);
variance = max(variance, minVariance);
float d = distance - moments.x;
float p_max = variance / (variance + d*d);
return reduceBleeding(p_max, bias);
}
float calculateShadowOcclusion( vec3 worldPosition, vec2 uv ) {
vec4 projCoords = lightViewProjection * vec4(worldPosition, 1.0) ;
float shadowDepth = length( lightPosition - worldPosition ) ;
projCoords.xy /= projCoords.w;
projCoords = 0.5 * projCoords + 0.5;
vec4 moments = texture2D( shadowDepthSampler, projCoords.xy );
mediump vec2 randomSample = texture2D(shadowNoiseSampler, vec2(1024.) * uv / 64.0).xy * 2.0 - 1.0;
float outFrustum = 0.0;
if(projCoords.x < 0.0 || projCoords.x > 1.0) {
return 0.0;
}
if(projCoords.y < 0.0 || projCoords.y > 1.0) {
return 0.0;
}
//if(projCoords.z < 1.0) {
// return 0.0;
//}
//return ChebyshevUpperBound(moments.xy, shadowDepth);
//return poissonPCFmultitap(projCoords, shadowDepth, uv);
return PCF( shadowDepthSampler, projCoords, randomSample, shadowDepth );
}
void main() {
vec3 position = texture2D( positionSampler, v_textureCoord ).xyz;
float shadow = calculateShadowOcclusion( position, v_textureCoord );
float ambientOcclusion = texture2D( ambientOcclusionSampler, v_textureCoord ).x;
gl_FragColor = vec4(ambientOcclusion, shadow, 0.0, 1.0);
}

70
shaders/old/tonemap.shader Executable file
View File

@@ -0,0 +1,70 @@
/**
* Kepler - Core
* Copyright (c) 2010 kaj dijkstra STUDIOS
* All rights reserved.
*/
/**
* Author: Kaj Dijksta
*/
attribute vec3 position;
attribute vec2 uv;
uniform mat4 viewProjection;
varying vec2 v_textureCoord;
void main(void) {
v_textureCoord = uv;
gl_Position = viewProjection * vec4(position, 1.0);
}
// #keplerEngine - Split
precision highp float;
uniform sampler2D colorSampler;
uniform float exposure;
varying vec2 v_textureCoord;
vec3 HDR_ACES(const vec3 x) {
// Narkowicz 2015, "ACES Filmic Tone Mapping Curve"
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return (x * (a * x + b)) / (x * (c * x + d) + e);
}
vec3 tonemap(const vec3 x) {
return HDR_ACES(x);
}
float linearToSRGB(float c) {
return (c <= 0.0031308) ? c * 12.92 : (pow(abs(c), 1.0 / 2.4) * 1.055) - 0.055;
}
vec3 linearToSRGB(vec3 c) {
return vec3(linearToSRGB(c.r), linearToSRGB(c.g), linearToSRGB(c.b));
}
void main() {
vec3 pbr = texture2D( colorSampler, v_textureCoord ).rgb;
gl_FragColor = vec4( pbr , 1.0 );
//pbr.rgb *= exposure;
//if(v_textureCoord.x < 0.5) {
// gl_FragColor = vec4( linearToSRGB( tonemap(pbr) ), 1.0 );
//} else {
// gl_FragColor = vec4(
//}
}

View File

@@ -0,0 +1,351 @@
struct deferredLightData {
vec3 position;
vec3 direction;
vec3 color;
float intensity;
float inverseFalloff;
float length;
float radius;
};
struct deferredMaterialData {
vec3 normal;
vec3 baseColor;
vec3 diffuse;
vec3 position;
vec3 vertex;
vec3 tangent;
vec3 specularReflectance;
float reflectance;
float metallic;
float roughness;
float clearCoat;
float clearCoatRoughness;
float clearCoatThickness;
vec3 clearCoatColor;
float geometricRoughness;
float ambientOcclusion;
float shadowOcclusion;
float refracted_NoV;
float index;
float alpha;
};
float D_GGX(float NoH, float a) {
float a2 = a * a;
float f = (NoH * a2 - NoH) * NoH + 1.0;
return a2 / (PI * f * f);
}
float D_GGX_Anisotropy(float NoH, vec3 h, vec3 x, vec3 y, float ax, float ay) {
float XoH = dot(x, h);
float YoH = dot(y, h);
float d = XoH * XoH * (ax * ax) + YoH * YoH * (ay * ay) + NoH * NoH;
return (ax * ay) / (PI * d * d);
}
vec3 F_Schlick(float VoH, vec3 specularReflectance, float f90) {
return specularReflectance + (vec3(f90) - specularReflectance) * pow(1.0 - VoH, 5.0);
}
float V_SmithGGXCorrelated(float NoV, float NoL, float a) {
float a2 = a * a;
float GGXL = NoV * sqrt((-NoL * a2 + NoL) * NoL + a2);
float GGXV = NoL * sqrt((-NoV * a2 + NoV) * NoV + a2);
return 0.5 / (GGXV + GGXL);
}
float Fd_Lambert() {
return 1.0 / PI;
}
float F_Schlick_Scalar(float VoH, float specularReflectance, float f90) {
return specularReflectance + (f90 - specularReflectance) * pow(1.0 - VoH, 5.0);
}
float square(float v) {
return v * v;
}
vec3 irradianceSH(vec3 n) {
return
vec3(0.754553530212464, 0.748541695286661, 0.790922541330174)
+ vec3(-0.083855089181764, 0.092536341322488, 0.322767327275582) * (n.y)
+ vec3(0.308154551673257, 0.366799355358085, 0.466705760819624) * (n.z)
+ vec3(-0.188887618191928, -0.277403749518126, -0.377844811540716) * (n.x)
+ vec3(-0.252782448589491, -0.316051613736677, -0.396141020484574) * (n.y * n.x)
+ vec3(0.071362454444021, 0.159789075773366, 0.29059362717571) * (n.y * n.z)
+ vec3(-0.031040420617065, -0.031141089772695, -0.031044001883204) * (3.0 * n.z * n.z - 1.0)
+ vec3(-0.161001896026477, -0.203649521035777, -0.246641086569566) * (n.z * n.x)
+ vec3(0.045710934605387, 0.048121779682969, 0.046326375668417) * (n.x * n.x - n.y * n.y);
}
vec2 prefilteredDFGKaris(float NoV, float roughness) {
const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
const vec4 c1 = vec4( 1.0, 0.0425, 1.040, -0.040);
vec4 r = roughness * c0 + c1;
float a004 = min(r.x * r.x, exp2(-9.28 * NoV)) * r.x + r.y;
return vec2(-1.04, 1.04) * a004 + r.zw;
}
float sRGBtoLinear(float c) {
return (c <= 0.04045) ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4);
}
vec3 sRGBtoLinear(vec3 c) {
return vec3(sRGBtoLinear(c.r), sRGBtoLinear(c.g), sRGBtoLinear(c.b));
}
vec3 decodeEnvironmentMap(vec4 c) {
return c.rgb; // or use sRGBtoLinear(c.rgb) if you want linear correction
}
float getSquareFalloffAttenuation(float distanceSquare, float inverseFalloff) {
float factor = distanceSquare * inverseFalloff;
float smoothFactor = max(1.0 - factor * factor, 0.0);
return smoothFactor * smoothFactor;
}
float getPhotometricAttenuation(vec3 lightToPos, vec3 lightDir) {
return 1.0;
}
float getAngleAttenuation(vec3 l, vec3 lightDir, float lightLength, float lightRadius) {
float cd = dot(lightDir, l);
float attenuation = clamp(cd * lightLength + lightRadius, 0.0, 1.0);
return attenuation * attenuation;
}
vec3 beerLambert(float NoV, float NoL, vec3 alpha, float d) {
return exp(alpha * -(d * ((NoL + NoV) / max(NoL * NoV, 1e-3))));
}
vec3 fixCubemapLookup(vec3 v, float lod) {
vec3 r = abs(v);
float M = max(max(v.x, v.y), v.z);
float scale = 1.0 - exp2(lod) * (1.0 / 256.0);
if (v.x != M) v.x *= scale;
if (v.y != M) v.y *= scale;
if (v.z != M) v.z *= scale;
return v;
}
vec3 evaluateSpecularIBL(vec3 r, float roughness) {
float lod = 5.0 * roughness;
r = fixCubemapLookup(r, lod);
return decodeEnvironmentMap(texture(reflectionSampler, r, lod)) * 1.8;
}
float computeSpecularambientOcclusion(float NoV, float ambientOcclusion, float roughness) {
return clamp(pow(NoV + ambientOcclusion, exp2(-16.0 * roughness - 1.0)) - 1.0 + ambientOcclusion, 0.0, 1.0);
}
vec3 getSpecularDominantDirection(vec3 n, vec3 r, float roughness) {
float s = 1.0 - roughness;
return mix(n, r, s * (sqrt(s) + roughness));
}
vec3 indirectLight(deferredMaterialData materialData) {
vec3 normal = materialData.normal;
vec3 vertex = materialData.vertex;
float roughness = materialData.roughness;
float NoV = max(dot(normal, vertex), 0.0);
#if ANISOTROPY == 1
vec3 t = normalize(materialData.tangent);
vec3 b = normalize(cross(t, normal));
vec3 anisotropicTangent = cross(-vertex, b);
vec3 anisotropicNormal = cross(anisotropicTangent, b);
vec3 bentNormal = normalize(mix(normal, anisotropicNormal, anisotropy));
vec3 r = reflect(vertex, bentNormal);
#else
vec3 r = reflect(-vertex, normal);
r = getSpecularDominantDirection(normal, r, roughness * roughness);
#endif
float NoR = max(dot(r, normal), 0.0);
vec3 indirectSpecular = evaluateSpecularIBL(reflect(vertex, normal), roughness);
vec2 env = prefilteredDFGKaris(NoV, materialData.roughness);
vec3 specularColor = materialData.specularReflectance * env.x + env.y * (1.0 - materialData.clearCoat) *
clamp(dot(materialData.specularReflectance, vec3(50.0 * 0.33)), 0.0, 1.0);
vec3 indirectDiffuse = max(irradianceSH(normal), 0.0) * Fd_Lambert();
float ambientOcclusionFade = clamp(dot(normalize(normal), vertex), 0.0, 1.0);
float ambientOcclusion = mix(1.0, materialData.ambientOcclusion, ambientOcclusionFade);
indirectDiffuse *= ambientOcclusion;
indirectSpecular *= computeSpecularambientOcclusion(NoV, ambientOcclusion, materialData.roughness);
float Fcc = F_Schlick_Scalar(NoV, 0.04, 1.0);
indirectSpecular = mix(indirectSpecular, vec3(Fcc), materialData.clearCoat);
return indirectDiffuse * materialData.baseColor + indirectSpecular;
}
vec3 directLight( deferredMaterialData materialData, deferredLightData lightData ) {
vec3 n = materialData.normal;
vec3 v = materialData.vertex;
vec3 l;
float NoL;
float energy;
float attenuation;
vec3 lightDir = normalize(lightData.direction);
float linearRoughness = materialData.roughness * materialData.roughness;
vec3 r = reflect(-v, n);
if (lightType == 1.0) {
l = -lightDir;
// Disc area light
vec3 sunDir = -lightDir;
float e = sin(radians(0.53));
float d = cos(radians(0.53));
float DoR = dot(sunDir, r);
vec3 s = r - DoR * sunDir;
l = DoR < d ? normalize(d * sunDir + normalize(s) * e) : r;
NoL = dot(n, l);
energy = 1.0;
attenuation = 1.0;
} else if (lightType == 0.0) {
vec3 posToLight = lightData.position - materialData.position;
float distanceSquare = dot(posToLight, posToLight);
l = normalize(posToLight);
NoL = dot(n, l);
energy = 50.0;
attenuation = getSquareFalloffAttenuation( distanceSquare, lightData.inverseFalloff );
attenuation *= 1.0 / max(distanceSquare, 1e-4);
attenuation *= getPhotometricAttenuation(-l, lightDir);
// attenuation *= getAngleAttenuation( l, -lightDir, lightData.length, lightData.radius );
}
vec3 h = normalize(v + l);
NoL = clamp(NoL, 0.0, 1.0);
float NoV = abs(dot(n, v)) + 1e-5;
float NoH = clamp(dot(n, h), 0.0, 1.0);
float LoH = clamp(dot(l, h), 0.0, 1.0);
// specular BRDF
#if ANISOTROPY == 1
vec3 t = normalize(v_tangent.xyz);
vec3 b = normalize(cross(t, n));
float aspect = inversesqrt(1.0 - anisotropy * 0.9);
float ax = 1.0 / (linearRoughness * aspect);
float ay = aspect / linearRoughness;
float D = D_GGX_Anisotropy(NoH, h, t, b, ax, ay);
#else
float D = D_GGX(NoH, linearRoughness);
#endif
vec3 F = F_Schlick(LoH, materialData.specularReflectance, clamp(dot(materialData.specularReflectance, vec3(50.0 * 0.33)), 0.0, 1.0));
float V = V_SmithGGXCorrelated(NoV, NoL, linearRoughness);
vec3 Fr = (D * V) * F;
// diffuse BRDF
vec3 Fd = materialData.diffuse * Fd_Lambert();// * 3.6
// clear coat
float linearClearCoatRoughness = materialData.clearCoatRoughness * materialData.clearCoatRoughness;
float Dcc = D_GGX(NoH, linearClearCoatRoughness);
float Fcc = F_Schlick_Scalar(LoH, 0.04, 1.0) * materialData.clearCoat;
float Vcc = V_SmithGGXCorrelated(NoV, NoL, linearClearCoatRoughness);
float FrCC = Dcc * Vcc * Fcc;
vec3 refracted_l = -refract(l, n, 1.0);
float refracted_NoL = clamp(dot(n, refracted_l), 0.0, 1.0);
vec3 clearCoatAbsorption = mix(vec3(1.0),
beerLambert(materialData.refracted_NoV, refracted_NoL, materialData.clearCoatColor, materialData.clearCoatThickness),
materialData.clearCoat);
// direct contribution
vec3 color = (attenuation * NoL) * lightData.color * lightData.intensity * energy *
((Fd + Fr) * (1.0 - Fcc) * clearCoatAbsorption + FrCC);
#if TRANSLUCENT_MATERIAL == 1
vec3 tL = l + n * translucencyDistortion;
float tD = exp2(clamp(dot(v, -tL), 0.0, 1.0) * translucencyPower - translucencyPower) * translucencyScale;
vec3 tT = attenuation * lightData.intensity * (tD + translucencyAmbient) * texture(translucencyThicknessMap, outUV).r;
color.rgb += Fd * lightData.color * tT;
#endif
// micro-shadowing
float aperture = 2.0 * materialData.ambientOcclusion * materialData.ambientOcclusion * materialData.shadowOcclusion;
float microShadow = clamp(abs(dot(l, n)) + aperture - 1.0, 0.0, 1.0);
color.rgb *= microShadow;
return color;
}
vec4 physically_based_shading( deferredMaterialData materialData, deferredLightData lightData ) {
//deferredLightData lightData;
materialData.diffuse = (1.0 - materialData.metallic) * materialData.baseColor;
// Geometric AA
vec3 ndFdx = dFdx( materialData.normal );
vec3 ndFdy = dFdy( materialData.normal );
materialData.geometricRoughness = pow( max( dot(ndFdx, ndFdx), dot(ndFdy, ndFdy) ), 0.333);
materialData.roughness = max(materialData.roughness, materialData.geometricRoughness);
materialData.specularReflectance = 0.16 *
materialData.reflectance * materialData.reflectance * (1.0 - materialData.metallic) +
materialData.baseColor * materialData.metallic;
materialData.clearCoatRoughness = max( mix(0.089, 0.6, materialData.clearCoatRoughness), materialData.geometricRoughness);
materialData.vertex = normalize( cameraPosition - materialData.position ); // v
vec3 refracted_vertex = -refract(materialData.vertex, materialData.normal, 1.0);
materialData.refracted_NoV = clamp(dot(materialData.normal, refracted_vertex), 0.0, 1.0);
float alpha = 1.0;
// indirect lighting
vec3 color = indirectLight( materialData );
color *= environmentLuminance;
color += directLight( materialData, lightData );
return vec4(color, alpha);
}

View File

@@ -0,0 +1,406 @@
struct deferredLightData {
vec3 position;
vec3 direction;
vec3 color;
float intensity;
float inverseFalloff;
float length;
float radius;
};
struct deferredMaterialData {
vec3 normal;
vec3 baseColor;
vec3 diffuse;
vec3 position;
vec3 vertex;
vec3 tangent;
vec3 specularReflectance;
float reflectance;
float metallic;
float roughness;
float clearCoat;
float clearCoatRoughness;
float clearCoatThickness;
vec3 clearCoatColor;
float geometricRoughness;
float ambientOcclusion;
float shadowOcclusion;
float refracted_NoV;
float index;
float alpha;
};
float D_GGX(float NoH, float a) {
float a2 = a * a;
float f = (NoH * a2 - NoH) * NoH + 1.0;
return a2 / (PI * f * f);
}
float D_GGX_Anisotropy(float NoH, vec3 h, vec3 x, vec3 y, float ax, float ay) {
float XoH = dot(x, h);
float YoH = dot(y, h);
float d = XoH * XoH * (ax * ax) + YoH * YoH * (ay * ay) + NoH * NoH;
return (ax * ay) / (PI * d * d);
}
vec3 F_Schlick(float VoH, vec3 specularReflectance, float f90) {
return specularReflectance + (vec3(f90) - specularReflectance) * pow(1.0 - VoH, 5.0);
}
float V_SmithGGXCorrelated(float NoV, float NoL, float a) {
float a2 = a * a;
float GGXL = NoV * sqrt((-NoL * a2 + NoL) * NoL + a2);
float GGXV = NoL * sqrt((-NoV * a2 + NoV) * NoV + a2);
return 0.5 / (GGXV + GGXL);
}
float Fd_Lambert() {
return 1.0 / PI;
}
float F_Schlick_Scalar(float VoH, float specularReflectance, float f90) {
return specularReflectance + (f90 - specularReflectance) * pow(1.0 - VoH, 5.0);
}
float square(float v) {
return v * v;
}
vec3 irradianceSH(vec3 n) {
return
vec3(0.754553530212464, 0.748541695286661, 0.790922541330174)
+ vec3(-0.083855089181764, 0.092536341322488, 0.322767327275582) * (n.y)
+ vec3(0.308154551673257, 0.366799355358085, 0.466705760819624) * (n.z)
+ vec3(-0.188887618191928, -0.277403749518126, -0.377844811540716) * (n.x)
+ vec3(-0.252782448589491, -0.316051613736677, -0.396141020484574) * (n.y * n.x)
+ vec3(0.071362454444021, 0.159789075773366, 0.29059362717571) * (n.y * n.z)
+ vec3(-0.031040420617065, -0.031141089772695, -0.031044001883204) * (3.0 * n.z * n.z - 1.0)
+ vec3(-0.161001896026477, -0.203649521035777, -0.246641086569566) * (n.z * n.x)
+ vec3(0.045710934605387, 0.048121779682969, 0.046326375668417) * (n.x * n.x - n.y * n.y);
}
vec2 prefilteredDFGKaris(float NoV, float roughness) {
// see https://www.unrealengine.com/blog/physically-based-shading-on-mobile
const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
const vec4 c1 = vec4( 1.0, 0.0425, 1.040, -0.040);
vec4 r = roughness * c0 + c1;
float a004 = min(r.x * r.x, exp2(-9.28 * NoV)) * r.x + r.y;
return vec2(-1.04, 1.04) * a004 + r.zw;
}
float sRGBtoLinear(float c) {
return (c <= 0.04045) ? c / 12.92 : pow((c + 0.055) / 1.055, 2.4);
}
vec3 sRGBtoLinear(vec3 c) {
return vec3(sRGBtoLinear(c.r), sRGBtoLinear(c.g), sRGBtoLinear(c.b));
}
vec3 decodeEnvironmentMap(vec4 c) {
return c.rgb;//;sRGBtoLinear(c.rgb) ;
}
float getSquareFalloffAttenuation(float distanceSquare, float inverseFalloff) {
float factor = distanceSquare * inverseFalloff;
float smoothFactor = max(1.0 - factor * factor, 0.0);
return smoothFactor * smoothFactor;
}
float getPhotometricAttenuation(vec3 lightToPos, vec3 lightDir) {
return 1.0;
}
float getAngleAttenuation(vec3 l, vec3 lightDir, float lightLength, float lightRadius) {
float cd = dot(lightDir, l);
float attenuation = clamp(cd * lightLength + lightRadius, 0.0, 1.0);
return attenuation * attenuation;
}
vec3 beerLambert(float NoV, float NoL, vec3 alpha, float d) {
return exp(alpha * -(d * ((NoL + NoV) / max(NoL * NoV, 1e-3))));
}
vec3 fixCubemapLookup(vec3 v, float lod) {
vec3 r = abs(v);
float M = max(max(v.x, v.y), v.z);
float scale = 1.0 - exp2(lod) * (1.0 / 256.0);
if (v.x != M) v.x *= scale;
if (v.y != M) v.y *= scale;
if (v.z != M) v.z *= scale;
return v;
}
vec3 evaluateSpecularIBL(vec3 r, float roughness) {
float lod = 5.0 * roughness;
r = fixCubemapLookup(r, lod);
return decodeEnvironmentMap( texture(reflectionSampler, r, lod) ) * 1.8; //, lod * 3.
}
float computeSpecularambientOcclusion(float NoV, float ambientOcclusion, float roughness) {
return clamp(pow(NoV + ambientOcclusion, exp2(-16.0 * roughness - 1.0)) - 1.0 + ambientOcclusion, 0.0, 1.0);
}
vec3 getSpecularDominantDirection(vec3 n, vec3 r, float roughness) {
float s = 1.0 - roughness;
return mix(n, r, s * (sqrt(s) + roughness));
}
vec3 indirectLight( deferredMaterialData materialData ) { //, deferredLightData materialData
vec3 normal = materialData.normal;
vec3 vertex = materialData.vertex;
float roughness = materialData.roughness;
float NoV = max( dot(normal, vertex), 0.0 );
#if ANISOTROPY == 1
vec3 t = normalize( materialData.tangent );
vec3 b = normalize( cross(t, normal));
vec3 anisotropicTangent = cross(-vertex, b);
vec3 anisotropicNormal = cross(anisotropicTangent, b);
vec3 bentNormal = normalize(mix(normal, anisotropicNormal, anisotropy));
vec3 r = reflect( vertex, bentNormal );
#else
vec3 r = reflect( -vertex, normal );
r = getSpecularDominantDirection(normal, r, roughness * roughness);
#endif
float NoR = max( dot(r, normal), 0.0);
// specular indirect
vec3 indirectSpecular = evaluateSpecularIBL( reflect(vertex, normal), roughness);
// horizon occlusion, can be removed for performance
//float horizon = min(1.0 + NoR, 1.0);
//indirectSpecular *= horizon * horizon;
vec2 env = prefilteredDFGKaris( NoV, materialData.roughness );
// we should multiply env.y by f90 for more accurate results
vec3 specularColor = materialData.specularReflectance * env.x + env.y * (1.0 - materialData.clearCoat) *
clamp(dot(materialData.specularReflectance, vec3(50.0 * 0.33)), 0.0, 1.0);
// diffuse indirect
vec3 indirectDiffuse = max(irradianceSH(normal), 0.0) * Fd_Lambert();
// ambient occlusion
float ambientOcclusionFade = clamp(dot(normalize(normal), vertex), 0.0, 1.0);
float ambientOcclusion = mix(1.0, materialData.ambientOcclusion, ambientOcclusionFade);
indirectDiffuse *= ambientOcclusion;
//indirectDiffuse *= 4.0;
// TODO: Not really useful without SSambientOcclusion/HBambientOcclusion/etc.
indirectSpecular *= computeSpecularambientOcclusion(NoV, ambientOcclusion, materialData.roughness);
// clear coat
float Fcc = F_Schlick_Scalar(NoV, 0.04, 1.0) * 0.2;
#if ANISOTROPY == 1
// We used the bent normal for the base layer
r = reflect(-vertex, normal);
#endif
vec3 indirectClearCoatSpecular = evaluateSpecularIBL(reflect(vertex, normal), materialData.clearCoatRoughness);
vec3 clearCoatAbsorption = mix(vec3(1.0),
beerLambert(materialData.refracted_NoV, materialData.refracted_NoV, materialData.clearCoatColor, materialData.clearCoatThickness),
materialData.clearCoat);
// indirect contribution (clear coat)
vec3 color =
(materialData.diffuse * indirectDiffuse + indirectSpecular * specularColor)//kaj materialData.diffuse * indirectDiffuse
* (1.0 - Fcc) * clearCoatAbsorption +
indirectClearCoatSpecular * Fcc;
#if TRANSLUCENT_MATERIAL == 1
indirectDiffuse = max(irradianceSH(-vertex), 0.0) * Fd_Lambert();
vec3 tL = -vertex + normal * translucencyDistortion;
float tD = pow(clamp(dot(vertex, -tL), 0.0, 1.0), translucencyPower) * translucencyScale;
vec3 tT = (tD + translucencyAmbient) * texture(translucencyThicknessMap, outUV).r;
color.rgb += materialData.diffuse * indirectDiffuse * tT;
#endif
return color;
}
vec3 getNormal(vec3 n) {
return normalize(n);
}
vec3 directLight( deferredMaterialData materialData, deferredLightData lightData ) {
vec3 n = materialData.normal;
vec3 v = materialData.vertex;
vec3 l;
float NoL;
float energy;
float attenuation;
vec3 lightDir = normalize(lightData.direction);
float linearRoughness = materialData.roughness * materialData.roughness;
vec3 r = reflect(-v, n);
if (lightType == 1.0) {
l = -lightDir;
// Disc area light
vec3 sunDir = -lightDir;
float e = sin(radians(0.53));
float d = cos(radians(0.53));
float DoR = dot(sunDir, r);
vec3 s = r - DoR * sunDir;
l = DoR < d ? normalize(d * sunDir + normalize(s) * e) : r;
NoL = dot(n, l);
energy = 1.0;
attenuation = 1.0;
} else if (lightType == 0.0) {
vec3 posToLight = lightData.position - materialData.position;
float distanceSquare = dot(posToLight, posToLight);
l = normalize(posToLight);
NoL = dot(n, l);
energy = 50.0;
attenuation = getSquareFalloffAttenuation( distanceSquare, lightData.inverseFalloff );
attenuation *= 1.0 / max(distanceSquare, 1e-4);
attenuation *= getPhotometricAttenuation(-l, lightDir);
// attenuation *= getAngleAttenuation( l, -lightDir, lightData.length, lightData.radius );
}
vec3 h = normalize(v + l);
NoL = clamp(NoL, 0.0, 1.0);
float NoV = abs(dot(n, v)) + 1e-5;
float NoH = clamp(dot(n, h), 0.0, 1.0);
float LoH = clamp(dot(l, h), 0.0, 1.0);
// specular BRDF
#if ANISOTROPY == 1
vec3 t = normalize( materialData.tangent );
vec3 b = normalize(cross(t, n));
float aspect = inversesqrt(1.0 - anisotropy * 0.9);
float ax = 1.0 / (linearRoughness * aspect);
float ay = aspect / linearRoughness;
float D = D_GGX_Anisotropy(NoH, h, t, b, ax, ay);
#else
float D = D_GGX(NoH, linearRoughness);
#endif
vec3 F = F_Schlick(LoH, materialData.specularReflectance, clamp(dot(materialData.specularReflectance, vec3(50.0 * 0.33)), 0.0, 1.0));
float V = V_SmithGGXCorrelated(NoV, NoL, linearRoughness);
vec3 Fr = (D * V) * F;
// diffuse BRDF
vec3 Fd = materialData.diffuse * Fd_Lambert();// * 3.6
// clear coat
float linearClearCoatRoughness = materialData.clearCoatRoughness * materialData.clearCoatRoughness;
float Dcc = D_GGX(NoH, linearClearCoatRoughness);
float Fcc = F_Schlick_Scalar(LoH, 0.04, 1.0) * materialData.clearCoat;
float Vcc = V_SmithGGXCorrelated(NoV, NoL, linearClearCoatRoughness);
float FrCC = Dcc * Vcc * Fcc;
vec3 refracted_l = -refract(l, n, 1.0);
float refracted_NoL = clamp(dot(n, refracted_l), 0.0, 1.0);
vec3 clearCoatAbsorption = mix(vec3(1.0),
beerLambert(materialData.refracted_NoV, refracted_NoL, materialData.clearCoatColor, materialData.clearCoatThickness),
materialData.clearCoat);
// direct contribution
vec3 color = (attenuation * NoL) * lightData.color * lightData.intensity * energy *
((Fd + Fr) * (1.0 - Fcc) * clearCoatAbsorption + FrCC);
#if TRANSLUCENT_MATERIAL == 1
vec3 tL = l + n * translucencyDistortion;
float tD = exp2(clamp(dot(v, -tL), 0.0, 1.0) * translucencyPower - translucencyPower) * translucencyScale;
vec3 tT = attenuation * lightData.intensity * (tD + translucencyAmbient) * texture(translucencyThicknessMap, outUV).r;
color.rgb += Fd * lightData.color * tT;
#endif
// micro-shadowing
float aperture = 2.0 * materialData.ambientOcclusion * materialData.ambientOcclusion * materialData.shadowOcclusion;
float microShadow = clamp(abs(dot(l, n)) + aperture - 1.0, 0.0, 1.0);
color.rgb *= microShadow;
return color;
}
vec4 physically_based_shading( deferredMaterialData materialData, deferredLightData lightData ) {
//deferredLightData lightData;
materialData.diffuse = (1.0 - materialData.metallic) * materialData.baseColor;
// Geometric AA
vec3 ndFdx = dFdx( materialData.normal );
vec3 ndFdy = dFdy( materialData.normal );
materialData.geometricRoughness = pow( max( dot(ndFdx, ndFdx), dot(ndFdy, ndFdy) ), 0.333);
materialData.roughness = max(materialData.roughness, materialData.geometricRoughness);
materialData.specularReflectance = 0.16 *
materialData.reflectance * materialData.reflectance * (1.0 - materialData.metallic) +
materialData.baseColor * materialData.metallic;
materialData.clearCoatRoughness = max( mix(0.089, 0.6, materialData.clearCoatRoughness), materialData.geometricRoughness);
materialData.vertex = normalize( cameraPosition - materialData.position ); // v
vec3 refracted_vertex = -refract(materialData.vertex, materialData.normal, 1.0);
materialData.refracted_NoV = clamp(dot(materialData.normal, refracted_vertex), 0.0, 1.0);
float alpha = 1.0;
// indirect lighting
vec3 color = indirectLight( materialData );
color *= environmentLuminance;
color += directLight( materialData, lightData );
return vec4(color, alpha);
}

View File

@@ -0,0 +1,205 @@
// Struct definitions (unchanged)
struct deferredMaterialData {
vec3 baseColor;
vec3 diffuse;
vec3 specularReflectance;
vec3 normal;
vec3 vertex;
float roughness;
float metallic;
float ambientOcclusion;
float alpha;
float clearcoat;
float clearcoatRoughness;
#if ANISOTROPY == 1
vec3 tangent;
#endif
#if TRANSLUCENT_MATERIAL == 1
float translucency;
float translucencyDistortion;
float translucencyPower;
float translucencyScale;
float translucencyAmbient;
#endif
float geometricRoughness;
};
struct deferredLightData {
vec3 direction;
float length;
float radius;
float inverseFalloff;
int lightType;
vec3 color;
};
// ————————— Optimized BRDF & utilities —————————
float D_GGX(float NoH, float a) {
float a2 = a * a;
float f = (NoH * a2 - NoH) * NoH + 1.0;
return a2 / max(PI * f * f, 1e-5);
}
float D_GGX_Anisotropy(float NoH, vec3 h, vec3 x, vec3 y, float ax, float ay) {
float XoH = dot(x, h), YoH = dot(y, h);
float d = XoH * XoH * (ax*ax) + YoH * YoH * (ay*ay) + NoH * NoH;
return (ax * ay) / max(PI * d * d, 1e-5);
}
vec3 F_Schlick(float VoH, vec3 F0, float f90) {
float pow5 = pow(1.0 - VoH, 5.0);
return F0 + (vec3(f90) - F0) * pow5;
}
float V_SmithGGXCorrelated(float NoV, float NoL, float a) {
float a2 = a * a;
float gv = sqrt(max((-NoV*a2 + NoV) * NoV + a2, 0.0));
float gl = sqrt(max((-NoL*a2 + NoL) * NoL + a2, 0.0));
return 0.5 / max(NoL * gv + NoV * gl, 1e-5);
}
float Fd_Lambert() {
return 1.0 / PI;
}
float F_Schlick_Scalar(float VoH, float F0, float f90) {
float pow5 = pow(1.0 - VoH, 5.0);
return F0 + (f90 - F0) * pow5;
}
vec3 irradianceSH(vec3 n) {
float nx = n.x, ny = n.y, nz = n.z;
float nymnx = ny * nx, nynz = ny * nz, nz2 = nz * nz, nxx_nyy = nx*nx - ny*ny;
return
vec3(0.7545535, 0.7485417, 0.7909225)
+ vec3(-0.08385509, 0.09253634, 0.3227673) * ny
+ vec3(0.3081546, 0.3667994, 0.4667058) * nz
+ vec3(-0.1888876, -0.2774037, -0.3778448) * nx
+ vec3(-0.2527824, -0.3160516, -0.396141) * nymnx
+ vec3(0.07136245, 0.1597891, 0.2905936) * nynz
+ vec3(-0.03104042) * (3.0 * nz2 - 1.0) // all three components identical
+ vec3(-0.1610019, -0.2036495, -0.246641) * (nz * nx)
+ vec3(0.04571093, 0.04812178, 0.04632638) * nxx_nyy;
}
vec2 prefilteredDFGKaris(float NoV, float roughness) {
const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);
const vec4 c1 = vec4( 1.0, 0.0425, 1.040, -0.040);
vec4 r = roughness * c0 + c1;
float expTerm = exp2(-9.28 * NoV);
float a004 = min(r.x * r.x, expTerm) * r.x + r.y;
return vec2(-1.04, 1.04) * a004 + r.zw;
}
vec3 decodeEnvironmentMap(vec4 c) {
return c.rgb; // assume linear
}
float getSquareFalloffAttenuation(float dist2, float invFalloff) {
float sf = max(1.0 - (dist2 * invFalloff) * (dist2 * invFalloff), 0.0);
return sf * sf;
}
vec3 fixCubemapLookup(vec3 v, float lod) {
vec3 a = abs(v);
float M = max(max(a.x, a.y), a.z);
float sc = 1.0 - exp2(lod) * (1.0/256.0);
if (a.x != M) v.x *= sc;
if (a.y != M) v.y *= sc;
if (a.z != M) v.z *= sc;
return v;
}
vec3 evaluateSpecularIBL(vec3 r, float roughness) {
float lod = 5.0 * roughness;
return decodeEnvironmentMap(textureLod(reflectionSampler, fixCubemapLookup(r, lod), lod)) * 1.8;
}
float computeSpecularao(float NoV, float ao, float roughness) {
float powe = exp2(-16.0 * roughness - 1.0);
return clamp(pow(NoV + ao, powe) - 1.0 + ao, 0.0, 1.0);
}
vec3 getSpecularDominantDirection(vec3 n, vec3 r, float roughness2) {
float s = 1.0 - roughness2;
return mix(n, r, s * (sqrt(s) + sqrt(roughness2)));
}
// ————————— Lighting functions —————————
vec3 indirectLight(in deferredMaterialData m) {
float rough2 = m.roughness * m.roughness;
float NoV = max(dot(m.normal, m.vertex), 0.0);
vec3 r = reflect(-m.vertex, m.normal);
r = getSpecularDominantDirection(m.normal, r, rough2);
vec3 diff = m.diffuse * irradianceSH(m.normal) * (1.0 - m.metallic) * m.ambientOcclusion;
vec3 spec = evaluateSpecularIBL(r, m.roughness)
* computeSpecularao(NoV, m.ambientOcclusion, m.roughness);
return diff + spec;
}
vec3 directLight(in deferredLightData light, in deferredMaterialData m) {
vec3 n = m.normal;
vec3 v = m.vertex;
vec3 L = normalize(-light.direction);
float att = 1.0;
if (light.lightType == 1) {
float Nl = max(dot(n, L), 0.0);
att = smoothstep(0.9999, 1.0, Nl);
}
float NoL = max(dot(n, L), 0.0), NoV = max(dot(n, v), 0.0);
vec3 diff = m.diffuse * NoL;
vec3 H = normalize(L + v);
float NoH = max(dot(n, H), 0.0), VoH = max(dot(v, H), 0.0);
#if ANISOTROPY == 1
vec3 t = normalize(m.tangent);
vec3 b = normalize(cross(t, n));
float ax = sqrt(m.roughness);
float ay = ax;
float D = D_GGX_Anisotropy(NoH, H, t, b, ax, ay);
#else
float D = D_GGX(NoH, m.roughness * m.roughness);
#endif
float V = V_SmithGGXCorrelated(NoV, NoL, m.roughness * m.roughness);
vec3 F = F_Schlick(VoH, m.specularReflectance, 1.0);
vec3 spec = (D * V) * F / max(4.0 * NoV * NoL, 1e-5);
if (m.clearcoat > 0.0) {
float Cr2 = m.clearcoatRoughness * m.clearcoatRoughness;
float Dc = D_GGX(NoH, Cr2);
float Vc = V_SmithGGXCorrelated(NoV, NoL, Cr2);
float Fc = F_Schlick_Scalar(VoH, 0.04, 1.0);
spec += m.clearcoat * (Dc * Vc * Fc / max(4.0 * NoV * NoL, 1e-5));
}
#if TRANSLUCENT_MATERIAL == 1
vec3 dL = normalize(L + m.translucencyDistortion * vec3(0.1));
float Nt = max(dot(n, dL), 0.0);
float trs = pow(Nt, m.translucencyPower) * m.translucencyScale;
trs = clamp(trs + m.translucencyAmbient, 0.0, 1.0);
diff += m.translucency * trs * light.color;
#endif
return att * light.color * (diff * (1.0 - m.metallic) + spec);
}
// ————————— Main shading entry —————————
vec4 physically_based_shading(deferredLightData light, deferredMaterialData m) {
m.diffuse = m.baseColor * (1.0 - m.metallic);
m.specularReflectance = mix(vec3(0.04), m.baseColor, m.metallic);
m.geometricRoughness = m.roughness * m.roughness;
m.vertex = normalize(cameraPosition - m.vertex);
vec3 color = indirectLight(m) + directLight(light, m);
return vec4(color, m.alpha);
}

56
shaders/selectEntity.shader Executable file
View File

@@ -0,0 +1,56 @@
/**
* Kepler - Core
* Copyright (c) 2010 kaj dijkstra STUDIOS
* All rights reserved.
*/
/**
* Author: Kaj Dijksta
*/
#version 300 es
precision highp float;
in vec3 position;
in vec2 uv;
out vec2 v_textureCoord;
uniform mat4 viewProjection;
void main(void) {
v_textureCoord = uv;
gl_Position = viewProjection * vec4(position, 1.0);
}
// #keplerEngine - Split
#version 300 es
precision highp float;
uniform sampler2D image;
uniform sampler2D entityIDSampler;
uniform float selectedEntityID;
in vec2 v_textureCoord;
out vec4 fragmentColor;
void main() {
float entityID = texture(entityIDSampler, v_textureCoord).w;
if(entityID == selectedEntityID)
fragmentColor = vec4(1.0);
else
fragmentColor = vec4(0.0);
//fragmentColor = texture(entityIDSampler, v_textureCoord);
}

234
shaders/shadow.shader Executable file
View File

@@ -0,0 +1,234 @@
/**
* Author: Kaj Dijksta
*/
attribute vec3 position;
attribute vec2 uv;
uniform mat4 viewProjection;
varying vec2 v_textureCoord;
void main(void) {
v_textureCoord = uv;
gl_Position = viewProjection * vec4(position, 1.0);
}
// #keplerEngine - Split
precision highp float;
uniform sampler2D positionSampler;
uniform sampler2D shadowDepthSampler;
uniform sampler2D shadowNoiseSampler;
uniform sampler2D ambientOcclusionSampler;
uniform float bias;
uniform float minVariance;
uniform vec3 lightPosition;
uniform mat4 lightViewProjection;
varying vec2 v_textureCoord;
float DecodeFloatRGBA( vec4 rgba ) {
return (rgba).x;
//return dot( rgba, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 160581375.0) );
}
float poissonPCFmultitap(vec4 projCoords, float shadowDepth, vec2 uv)
{
const mediump float step = 1.0 - 1.0 / 8.0;
const mediump float fScale = 0.025; // 0.025
mediump float n = 0.0;
mediump vec3 directions[8];
float vSampleScale = 1.0 / 2048.0;
directions[0] = normalize(vec3( 1.0, 1.0, 1.0))*fScale*(n+=step);
directions[1] = normalize(vec3(-1.0,-1.0,-1.0))*fScale*(n+=step);
directions[2] = normalize(vec3(-1.0,-1.0, 1.0))*fScale*(n+=step);
directions[3] = normalize(vec3(-1.0, 1.0,-1.0))*fScale*(n+=step);
directions[4] = normalize(vec3(-1.0, 1.0 ,1.0))*fScale*(n+=step);
directions[5] = normalize(vec3( 1.0,-1.0,-1.0))*fScale*(n+=step);
directions[6] = normalize(vec3( 1.0,-1.0, 1.0))*fScale*(n+=step);
directions[7] = normalize(vec3( 1.0, 1.0,-1.0))*fScale*(n+=step);
mediump vec3 randomSample = texture2D(shadowNoiseSampler, vec2(64.0, 64.0) * uv.xy / 4.0).xyz * 2.0 - 1.0;
float sum = 0.0;
// for( int i = 0; i < 4; i++ ) {
// vec3 sampler = reflect(directions[0], randomSample) * vSampleScale;
float pixelDepth = DecodeFloatRGBA( texture2D(shadowDepthSampler, projCoords.xy ) ) ; // + sampler.xy + sampler.z
if( pixelDepth + bias > shadowDepth) {
sum += 1.0;
} else {
sum += 0.5;
}
// }
return sum;
}
float shadow_sample(sampler2D depthMap, vec2 coord)
{
return ( texture2D(depthMap, coord.xy).x );
}
vec2 DoubleSampleRotated(sampler2D depthMap, vec4 p, vec4 rotMatr, vec4 kernel) {
vec4 rotatedOff;
rotatedOff = rotMatr.xyzw * kernel.xxww +
rotMatr.zwxy * kernel.yyzz;
vec4 fetchPos = p.xyxy + rotatedOff;// + rotatedOff
vec2 result;
result.x = shadow_sample(depthMap, fetchPos.xy);
result.y = shadow_sample(depthMap, fetchPos.zw);
return result;
}
float PCF(sampler2D depthMap, vec4 p, vec2 randDirTC, float depth)
{
vec2 kernelRadius = vec2(4.0);
vec4 irreg_kernel_2d[8];
irreg_kernel_2d[0] = vec4(-0.556641,-0.037109,-0.654297, 0.111328);
irreg_kernel_2d[1] = vec4(0.173828,0.111328,0.064453, -0.359375);
irreg_kernel_2d[2] = vec4(0.001953,0.082031,-0.060547, 0.078125);
irreg_kernel_2d[3] = vec4(0.220703,-0.359375,-0.062500, 0.001953);
irreg_kernel_2d[4] = vec4(0.242188,0.126953,-0.250000, -0.140625);
irreg_kernel_2d[5] = vec4(0.070313,-0.025391,0.148438, 0.082031);
irreg_kernel_2d[6] = vec4(-0.078125,0.013672,-0.314453, 0.013672);
irreg_kernel_2d[7] = vec4(0.117188,-0.140625,-0.199219, 0.117188);
vec2 vInvShadowMapWH = vec2(1.0 / 2048.0);
const int kernelSize = 8;
mediump float P_Z = depth; // p.z;
vec4 p0 = vec4(p.xyz, 1.0);
mediump vec2 rotScale = vec2(kernelRadius.y * 2.0);
float shadowTest = 0.0;
#define KERNEL_STEP_SIZE 2
vec2 rotSample = 2.0 * texture2D(shadowDepthSampler, randDirTC.xy).xy - 1.0;
rotSample.xy = normalize(rotSample.xy);
rotSample.xy *= (kernelRadius.xy * vInvShadowMapWH.xy);
vec4 rot = vec4(rotSample.x, -rotSample.y, rotSample.y, rotSample.x);
const int kernelOffset = 0;
for(int i=kernelOffset; i<kernelSize; i+=KERNEL_STEP_SIZE) // Loop over taps
{
mediump vec4 sampleDepth = vec4(0.0);
vec4 irr = irreg_kernel_2d[i+0];
sampleDepth.xy = DoubleSampleRotated(depthMap, p0, rot, irr);
sampleDepth.zw = DoubleSampleRotated(depthMap, p0, rot, irreg_kernel_2d[i+1]);
mediump vec4 InShadow;
InShadow.x = ( P_Z < sampleDepth.x + bias ) ? 1. : 0.0;
InShadow.y = ( P_Z < sampleDepth.y + bias ) ? 1. : 0.0;
InShadow.z = ( P_Z < sampleDepth.z + bias ) ? 1. : 0.0;
InShadow.w = ( P_Z < sampleDepth.w + bias ) ? 1. : 0.0;
const mediump float quality = 8.0; // 8 == high
const mediump float fInvSamplNum = (1.0 / quality);
shadowTest += dot(InShadow, vec4(fInvSamplNum));
}
return shadowTest;
}
float linestep(float min, float max, float value) {
return clamp((value - min) / (max - min), 0., 1.);
}
float reduceBleeding(float p_max, float amount) {
return linestep(amount, 1.0, p_max);
}
float ChebyshevUpperBound(vec2 moments, float distance) {
if (distance <= moments.x)
return 1.0;
float variance = moments.y - (moments.x*moments.x);
variance = max(variance, minVariance);
float d = distance - moments.x;
float p_max = variance / (variance + d*d);
return reduceBleeding(p_max, bias);
}
float calculateShadowOcclusion( vec3 worldPosition, vec2 uv ) {
vec4 projCoords = lightViewProjection * vec4(worldPosition, 1.0) ;
float shadowDepth = length( lightPosition - worldPosition ) ;
projCoords.xy /= projCoords.w;
projCoords = 0.5 * projCoords + 0.5;
vec4 moments = texture2D( shadowDepthSampler, projCoords.xy );
mediump vec2 randomSample = texture2D(shadowNoiseSampler, vec2(1024.) * uv / 64.0).xy * 2.0 - 1.0;
float outFrustum = 0.0;
if(projCoords.x < 0.0 || projCoords.x > 1.0) {
return 0.0;
}
if(projCoords.y < 0.0 || projCoords.y > 1.0) {
return 0.0;
}
//if(projCoords.z < 1.0) {
// return 0.0;
//}
//return ChebyshevUpperBound(moments.xy, shadowDepth);
//return poissonPCFmultitap(projCoords, shadowDepth, uv);
return PCF( shadowDepthSampler, projCoords, randomSample, shadowDepth );
}
void main() {
vec3 position = texture2D( positionSampler, v_textureCoord ).xyz;
float shadow = calculateShadowOcclusion( position, v_textureCoord );
float ambientOcclusion = texture2D( ambientOcclusionSampler, v_textureCoord ).x;
gl_FragColor = vec4(ambientOcclusion, shadow, 0.0, 1.0);
}

70
shaders/tonemap.shader Executable file
View File

@@ -0,0 +1,70 @@
/**
* Kepler - Core
* Copyright (c) 2010 kaj dijkstra STUDIOS
* All rights reserved.
*/
/**
* Author: Kaj Dijksta
*/
attribute vec3 position;
attribute vec2 uv;
uniform mat4 viewProjection;
varying vec2 v_textureCoord;
void main(void) {
v_textureCoord = uv;
gl_Position = viewProjection * vec4(position, 1.0);
}
// #keplerEngine - Split
precision highp float;
uniform sampler2D colorSampler;
uniform float exposure;
varying vec2 v_textureCoord;
vec3 HDR_ACES(const vec3 x) {
// Narkowicz 2015, "ACES Filmic Tone Mapping Curve"
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return (x * (a * x + b)) / (x * (c * x + d) + e);
}
vec3 tonemap(const vec3 x) {
return HDR_ACES(x);
}
float linearToSRGB(float c) {
return (c <= 0.0031308) ? c * 12.92 : (pow(abs(c), 1.0 / 2.4) * 1.055) - 0.055;
}
vec3 linearToSRGB(vec3 c) {
return vec3(linearToSRGB(c.r), linearToSRGB(c.g), linearToSRGB(c.b));
}
void main() {
vec3 pbr = texture2D( colorSampler, v_textureCoord ).rgb;
gl_FragColor = vec4( pbr , 1.0 );
//pbr.rgb *= exposure;
//if(v_textureCoord.x < 0.5) {
// gl_FragColor = vec4( linearToSRGB( tonemap(pbr) ), 1.0 );
//} else {
// gl_FragColor = vec4(
//}
}