const vec3 DAYSKY_COLOUR = vec3( 0.37, 0.54, 0.97 ); const vec3 NIGHTSKY_COLOUR = vec3( 0.03, 0.05, 0.20 ); const vec3 SUNSET_COLOUR = vec3( 1.00, 0.32, 0.01 ); const vec3 AMBIENT_COLOUR = vec3( 0.13, 0.17, 0.35 ); const vec3 SUNSET_AMBIENT = vec3( 0.25, 0.17, 0.51 ); const vec3 SUN_COLOUR = vec3( 1.10, 0.89, 0.35 ) * 1.125; const float SUN_ANGLE = 0.0001; const float TIME_RATE = 0.025; const float PI = 3.14159265; struct world_info { float time, time_of_day, day_phase, sunset_phase; vec3 sun_dir; }; float luminance( vec3 v ) { return dot( v, vec3(0.2126, 0.7152, 0.0722) ); } vec3 scene_ambient( vec3 dir, const world_info w ) { float sun_azimuth = dot( dir.xz, w.sun_dir.xz ) * 0.4 + 0.6; float sky_gradient = dir.y; /* Blend phase colours */ vec3 ambient = DAYSKY_COLOUR * (w.day_phase-w.sunset_phase*0.1); ambient += SUNSET_COLOUR * (1.0-dir.y*0.5) * w.sunset_phase * sun_azimuth; ambient += NIGHTSKY_COLOUR * (1.0-w.day_phase); /* Add gradient */ ambient -= sky_gradient * luminance(ambient); return ambient; } vec3 scene_sky( vec3 ray_dir, const world_info w ) { ray_dir.y = abs( ray_dir.y ); vec3 sky_colour = scene_ambient( ray_dir, w ); /* Sun */ float sun_theta = dot( ray_dir, w.sun_dir ); float sun_size = max( 0.0, sun_theta * 0.5 + 0.5 + SUN_ANGLE ); float sun_shape = pow( sun_size, 2000.0 ); sun_shape += sun_size * max(w.sun_dir.y,0.0) * 0.5; vec3 sun_colour = mix( vec3(1.0), SUNSET_COLOUR, w.sunset_phase*0.5 ); sun_colour *= sun_shape; vec3 composite = sky_colour + sun_colour; return composite; } vec3 scene_compute_ambient( vec3 normal, const world_info w ) { return scene_ambient( (normal * vec3(1.0,-1.0,1.0)) * 0.5 + 0.5, w ); } vec3 SR_LIGHT( vec3 normal, vec2 dir, vec3 colour ) { vec3 dir3 = vec3 ( cos(dir.y) * cos(dir.x), sin(dir.x), sin(dir.y) * cos(dir.x) ); float flight = max( dot( normal, dir3 ) * 0.75 + 0.25, 0.0 ); return flight * colour; } vec3 scene_lighting_old( vec3 normal, const world_info w ) { vec3 SR_COLOUR_SUN = vec3( 1.36, 1.35, 1.01 ); vec3 SR_COLOUR_FILL = vec3( 0.33, 0.56, 0.64 ); vec3 SR_COLOUR_RIM = vec3( 0.05, 0.05, 0.23 ); return SR_LIGHT( normal, vec2( 0.63, -0.08 ), SR_COLOUR_SUN ) + SR_LIGHT( normal, vec2( -2.60, -0.13 ), SR_COLOUR_FILL ) + SR_LIGHT( normal, vec2( 2.60, -0.84 ), SR_COLOUR_RIM ) ; } vec3 scene_lighting( vec3 normal, float shadow, vec3 halfview, const world_info w ) { float fresnel = 1.0 - abs(dot(normal,halfview)); vec3 sky_reflection = 0.5 * fresnel * mix( DAYSKY_COLOUR, SUNSET_COLOUR, w.sunset_phase ); vec3 light_sun = max(0.0,dot(normal,w.sun_dir)*0.75+0.25) * SUN_COLOUR * w.day_phase; float scaled_shadow = max( shadow, 1.0 - max(w.sun_dir.y,0.0) ); vec3 ambient = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase ); return ambient + (light_sun + sky_reflection) * shadow; float sun_theta = dot( normal, w.sun_dir ); float softness_min = 0.5; float softness = softness_min + w.sunset_phase * (1.0-softness_min); float light_min = 0.0 * w.day_phase; float light_direct = light_min + smoothstep( -softness, softness, sun_theta ) * (1.0-light_min); light_direct *= clamp(w.sun_dir.y * 4.0 + 1.0,0.0,1.0) * shadow; float light_bounce = 0.5 + 0.5 * dot( -normal, w.sun_dir ); light_bounce *= light_bounce * max( w.sun_dir.y, 0.0 ); vec3 light_colour = SUN_COLOUR*w.day_phase + (SUNSET_COLOUR*w.sunset_phase + 0.1); vec3 dark_colour = mix( AMBIENT_COLOUR, SUNSET_AMBIENT, w.sunset_phase ); float spec = newlight_specular( normal, w.sun_dir, halfview, 2.0 ) * 0.2 * shadow * w.day_phase; return mix(dark_colour, light_colour, light_direct) + spec + dark_colour * light_bounce; } void scene_state( float world_time, out world_info w ) { w.time = world_time; w.time_of_day = fract( w.time ); w.day_phase = cos( w.time_of_day * PI * 2.0 ) * 0.5 + 0.5; w.sunset_phase = cos( w.time_of_day * PI * 4.0 + PI ) * 0.5 + 0.5; w.sunset_phase = pow( w.sunset_phase, 6.0 ); float a = w.time_of_day * PI * 2.0; w.sun_dir = normalize( vec3( sin( a ), cos( a ), 0.2) ); }