Skip to content

Commit

Permalink
Physical sky bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
CedricGuillemet committed Oct 15, 2018
1 parent 1071f10 commit bc6e7f4
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 4 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Expand Up @@ -49,7 +49,6 @@ include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/ext/tcc-0.9.27/
${CMAKE_CURRENT_SOURCE_DIR}/ext/NativeFileDialog/src/include/
${CMAKE_CURRENT_SOURCE_DIR}/ext/enkiTS-C-11/src/
${CMAKE_CURRENT_SOURCE_DIR}/ext/gli
)

link_directories(
Expand Down
3 changes: 3 additions & 0 deletions bin/C/Imogen.h
Expand Up @@ -19,6 +19,9 @@ typedef struct Evaluation_t
int padding;
float mouse[4];
int inputIndices[8];
float inv_proj[16];
float inv_view_rot[9];
float viewport[2];
} Evaluation;

enum BlendOp
Expand Down
101 changes: 101 additions & 0 deletions bin/GLSL/PhysicalSky.glsl
@@ -0,0 +1,101 @@

layout (std140) uniform PhysicalSkyBlock
{
vec4 ambient;
vec3 lightdir, Kr;
float rayleigh_brightness, mie_brightness, spot_brightness, scatter_strength, rayleigh_strength, mie_strength;
float rayleigh_collection_power, mie_collection_power, mie_distribution;
};




float surface_height = 0.99;
float range = 0.01;
float intensity = 1.8;
const int step_count = 16;

vec3 get_world_normal()
{
vec2 frag_coord = gl_FragCoord.xy/viewport;
frag_coord = (frag_coord-0.5)*2.0;
vec4 device_normal = vec4(frag_coord, 0.0, 1.0);
vec3 eye_normal = normalize((inv_proj * device_normal).xyz);
vec3 world_normal = normalize(inv_view_rot*eye_normal);
return world_normal;
}

float atmospheric_depth(vec3 position, vec3 dir)
{
float a = dot(dir, dir);
float b = 2.0*dot(dir, position);
float c = dot(position, position)-1.0;
float det = b*b-4.0*a*c;
float detSqrt = sqrt(det);
float q = (-b - detSqrt)/2.0;
float t1 = c/q;
return t1;
}

float phase(float alpha, float g)
{
float a = 3.0*(1.0-g*g);
float b = 2.0*(2.0+g*g);
float c = 1.0+alpha*alpha;
float d = pow(1.0+g*g-2.0*g*alpha, 1.5);
return (a/b)*(c/d);
}

float horizon_extinction(vec3 position, vec3 dir, float radius)
{
float u = dot(dir, -position);
if(u<0.0){
return 1.0;
}
vec3 near = position + u*dir;
if(length(near) < radius){
return 0.0;
}
else{
vec3 v2 = normalize(near)*radius - position;
float diff = acos(dot(normalize(v2), dir));
return smoothstep(0.0, 1.0, pow(diff*2.0, 3.0));
}
}

vec3 absorb(float dist, vec3 color, float factor)
{
return color-color*pow(Kr, vec3(factor/dist));
}

vec4 PhysicalSky()
{
vec3 eyedir = get_world_normal();
float alpha = dot(eyedir, lightdir);
float rayleigh_factor = phase(alpha, -0.01)*rayleigh_brightness;
float mie_factor = phase(alpha, mie_distribution)*mie_brightness;
float spot = smoothstep(0.0, 15.0, phase(alpha, 0.9995))*spot_brightness;
vec3 eye_position = vec3(0.0, surface_height, 0.0);
float eye_depth = atmospheric_depth(eye_position, eyedir);
float step_length = eye_depth/float(step_count);
float eye_extinction = horizon_extinction(eye_position, eyedir, surface_height-0.15);
vec3 rayleigh_collected = vec3(0.0, 0.0, 0.0);
vec3 mie_collected = vec3(0.0, 0.0, 0.0);
for(int i=0; i<step_count; i++)
{
float sample_distance = step_length*float(i);
vec3 position = eye_position + eyedir*sample_distance;
float extinction = horizon_extinction(position, lightdir, surface_height-0.35);
float sample_depth = atmospheric_depth(position, lightdir);
vec3 influx = absorb(sample_depth, vec3(intensity), scatter_strength)*extinction;
rayleigh_collected += absorb(sample_distance, Kr*influx, rayleigh_strength);
mie_collected += absorb(sample_distance, influx, mie_strength);
}

rayleigh_collected = (rayleigh_collected*eye_extinction*pow(eye_depth, rayleigh_collection_power))/float(step_count);
mie_collected = (mie_collected*eye_extinction*pow(eye_depth, mie_collection_power))/float(step_count);
vec3 color = vec3(spot*mie_collected + mie_factor*mie_collected + rayleigh_factor*rayleigh_collected);
return vec4(max(color * ambient.w, ambient.xyz), 1.0);
}

#endif
4 changes: 3 additions & 1 deletion bin/GLSL/Shader.glsl
Expand Up @@ -27,7 +27,9 @@ layout (std140) uniform EvaluationBlock
int padding;
vec4 mouse; // x,y, lbut down, rbut down
int inputIndices[8];

mat4 inv_proj;
mat3 inv_view_rot;
vec2 viewport;
} EvaluationParam;


Expand Down
Binary file modified bin/library.dat
Binary file not shown.
3 changes: 3 additions & 0 deletions src/Evaluation.h
Expand Up @@ -73,6 +73,9 @@ struct EvaluationInfo
int padding;
float mouse[4];
int inputIndices[8];
float inv_proj[16];
float inv_view_rot[9];
float viewport[2];
};

struct TextureFormat
Expand Down
24 changes: 22 additions & 2 deletions src/NodesDelegate.h
Expand Up @@ -175,9 +175,9 @@ struct TileNodeEditGraphDelegate : public NodeGraphDelegate
static const uint32_t hcPaint = IM_COL32(100, 250, 180, 255);


metaNodeCount = 30;
metaNodeCount = 31;

static const MetaNode metaNodes[30] = {
static const MetaNode metaNodes[31] = {

{
"Circle", hcGenerator, 1
Expand Down Expand Up @@ -431,6 +431,26 @@ struct TileNodeEditGraphDelegate : public NodeGraphDelegate
,{ "Face size", (int)Con_Enum, 0.f,0.f,0.f,0.f, false, false, " 32\0 64\0 128\0 256\0 512\0 1024\0" }
}
}

,
{
"PhysicalSky", hcGenerator, 1
,{ }
,{ { "", (int)Con_Float4 } }
,{ { "ambient", (int)Con_Float4 }
,{ "lightdir", (int)Con_Float3 }
,{ "Kr", (int)Con_Float3 }
,{ "rayleigh brightness", (int)Con_Float }
,{ "mie brightness", (int)Con_Float }
,{ "spot brightness", (int)Con_Float }
,{ "scatter strength", (int)Con_Float }
,{ "rayleigh strength", (int)Con_Float }
,{ "mie strength" , (int)Con_Float }
,{ "rayleigh collection power", (int)Con_Float }
,{ "mie collection power", (int)Con_Float }
,{ "mie distribution", (int)Con_Float } }

}
};

return metaNodes;
Expand Down

0 comments on commit bc6e7f4

Please sign in to comment.