ADD sky shaderand panorama sky.

This commit is contained in:
osquallo 2020-03-21 14:18:06 +01:00
parent 9eb6e8ccbd
commit 44f1d9239b
4 changed files with 547 additions and 2 deletions

396
assets/sky/sky.shader Normal file
View file

@ -0,0 +1,396 @@
shader_type canvas_item;
// USING https://www.shadertoy.com/view/XtBXDw (base on it)
uniform float iTime;
uniform int iFrame;
uniform sampler2D iChannel0;
uniform float COVERAGE :hint_range(0,1); //0.5
uniform float THICKNESS :hint_range(0,100); //25.
uniform float ABSORPTION :hint_range(0,10); //1.030725
uniform int STEPS :hint_range(0,100); //25
////////////////////////////////
uniform float earth_radius_km = 6371;
uniform float atmo_radius_km = 6471;
uniform float cam_height_m = 1.8;
//uniform vec3 sun_pos = vec3(0.0, 0.1, -0.5);
uniform vec3 sun_pos = vec3(1.0, 1.0, 1.0);
uniform float sun_intensity = 22.0;
uniform vec3 rayleigh_coeff = vec3(5.5, 13.0, 22.4); // we divide this by 100000
uniform float mie_coeff = 21.0; // we divide this by 100000
uniform float rayleigh_scale = 800;
uniform float mie_scale = 120;
uniform float mie_scatter_dir = 0.758;
uniform sampler2D night_sky : hint_black_albedo;
uniform mat3 rotate_night_sky;
// Atmosphere code from: https://github.com/wwwtyro/glsl-atmosphere
vec2 rsi(vec3 r0, vec3 rd, float sr) {
// ray-sphere intersection that assumes
// the sphere is centered at the origin.
// No intersection when result.x > result.y
float a = dot(rd, rd);
float b = 2.0 * dot(rd, r0);
float c = dot(r0, r0) - (sr * sr);
float d = (b*b) - 4.0*a*c;
if (d < 0.0) return vec2(100000.0,-100000.0);
return vec2(
(-b - sqrt(d))/(2.0*a),
(-b + sqrt(d))/(2.0*a)
);
}
vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g) {
float PI = 3.14159265358979;
int iSteps = 16;
int jSteps = 8;
// Normalize the sun and view directions.
pSun = normalize(pSun);
r = normalize(r);
// Calculate the step size of the primary ray.
vec2 p = rsi(r0, r, rAtmos);
if (p.x > p.y) return vec3(0,0,0);
p.y = min(p.y, rsi(r0, r, rPlanet).x);
float iStepSize = (p.y - p.x) / float(iSteps);
// Initialize the primary ray time.
float iTimeBis = 0.0;
// Initialize accumulators for Rayleigh and Mie scattering.
vec3 totalRlh = vec3(0,0,0);
vec3 totalMie = vec3(0,0,0);
// Initialize optical depth accumulators for the primary ray.
float iOdRlh = 0.0;
float iOdMie = 0.0;
// Calculate the Rayleigh and Mie phases.
float mu = dot(r, pSun);
float mumu = mu * mu;
float gg = g * g;
float pRlh = 3.0 / (16.0 * PI) * (1.0 + mumu);
float pMie = 3.0 / (8.0 * PI) * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg));
// Sample the primary ray.
for (int i = 0; i < iSteps; i++) {
// Calculate the primary ray sample position.
vec3 iPos = r0 + r * (iTimeBis + iStepSize * 0.5);
// Calculate the height of the sample.
float iHeight = length(iPos) - rPlanet;
// Calculate the optical depth of the Rayleigh and Mie scattering for this step.
float odStepRlh = exp(-iHeight / shRlh) * iStepSize;
float odStepMie = exp(-iHeight / shMie) * iStepSize;
// Accumulate optical depth.
iOdRlh += odStepRlh;
iOdMie += odStepMie;
// Calculate the step size of the secondary ray.
float jStepSize = rsi(iPos, pSun, rAtmos).y / float(jSteps);
// Initialize the secondary ray time.
float jTime = 0.0;
// Initialize optical depth accumulators for the secondary ray.
float jOdRlh = 0.0;
float jOdMie = 0.0;
// Sample the secondary ray.
for (int j = 0; j < jSteps; j++) {
// Calculate the secondary ray sample position.
vec3 jPos = iPos + pSun * (jTime + jStepSize * 0.5);
// Calculate the height of the sample.
float jHeight = length(jPos) - rPlanet;
// Accumulate the optical depth.
jOdRlh += exp(-jHeight / shRlh) * jStepSize;
jOdMie += exp(-jHeight / shMie) * jStepSize;
// Increment the secondary ray time.
jTime += jStepSize;
}
// Calculate attenuation.
vec3 attn = exp(-(kMie * (iOdMie + jOdMie) + kRlh * (iOdRlh + jOdRlh)));
// Accumulate scattering.
totalRlh += odStepRlh * attn;
totalMie += odStepMie * attn;
// Increment the primary ray time.
iTimeBis += iStepSize;
}
// Calculate and return the final color.
return iSun * (pRlh * kRlh * totalRlh + pMie * kMie * totalMie);
}
// and our application
vec3 ray_dir_from_uv(vec2 uv) {
float PI = 3.14159265358979;
vec3 dir;
float x = sin(PI * uv.y);
dir.y = cos(PI * uv.y);
dir.x = x * sin(2.0 * PI * (0.5 - uv.x));
dir.z = x * cos(2.0 * PI * (0.5 - uv.x));
return dir;
}
vec2 uv_from_ray_dir(vec3 dir) {
float PI = 3.14159265358979;
vec2 uv;
uv.y = acos(dir.y) / PI;
dir.y = 0.0;
dir = normalize(dir);
uv.x = acos(dir.z) / (2.0 * PI);
if (dir.x < 0.0) {
uv.x = 1.0 - uv.x;
}
uv.x = 0.5 - uv.x;
if (uv.x < 0.0) {
uv.x += 1.0;
}
return uv;
}
////////////////////////////////
float noise( in vec3 x )
{
x*=0.01;
float z = x.z*256.0;
vec2 offz = vec2(0.317,0.123);
vec2 uv1 = x.xy + offz*floor(z);
vec2 uv2 = uv1 + offz;
return mix(textureLod( iChannel0, uv1 ,0.0).x,textureLod( iChannel0, uv2 ,0.0).x,fract(z));
}
float fbm(vec3 pos,float lacunarity){
vec3 p = pos;
float
t = 0.51749673 * noise(p); p *= lacunarity;
t += 0.25584929 * noise(p); p *= lacunarity;
t += 0.12527603 * noise(p); p *= lacunarity;
t += 0.06255931 * noise(p);
return t;
}
float get_noise(vec3 x)
{
float FBM_FREQ=2.76434;
return fbm(x, FBM_FREQ);
}
vec3 render_sky_color(vec3 rd){
vec3 sun_color = vec3(1., .7, .55);
vec3 SUN_DIR = normalize(vec3(0, abs(sin( .3)), -1));
float sun_amount = max(dot(rd, SUN_DIR), 0.0);
vec3 sky = mix(vec3(.0, .1, .4), vec3(.3, .6, .8), 1.0 - rd.y);
sky = sky + sun_color * min(pow(sun_amount, 1500.0) * 5.0, 1.0);
sky = sky + sun_color * min(pow(sun_amount, 10.0) * .6, 1.0);
return sky;
}
bool SphereIntersect(vec3 SpPos, float SpRad, vec3 ro, vec3 rd, out float t, out vec3 norm) {
ro -= SpPos;
float A = dot(rd, rd);
float B = 2.0*dot(ro, rd);
float C = dot(ro, ro)-SpRad*SpRad;
float D = B*B-4.0*A*C;
if (D < 0.0) return false;
D = sqrt(D);
A *= 2.0;
float t1 = (-B+D)/A;
float t2 = (-B-D)/A;
if (t1 < 0.0) t1 = t2;
if (t2 < 0.0) t2 = t1;
t1 = min(t1, t2);
if (t1 < 0.0) return false;
norm = ro+t1*rd;
t = t1;
//norm = normalize(norm);
return true;
}
float density(vec3 pos,vec3 offset,float t){
vec3 p = pos * .0212242 + offset;
float dens = get_noise(p);
float cov = 1. - COVERAGE;
dens *= smoothstep (cov, cov + .05, dens);
return clamp(dens, 0., 1.);
}
vec4 render_clouds(vec3 ro,vec3 rd){
vec3 apos=vec3(0, -450, 0);
float arad=500.;
vec3 WIND=vec3(0, 0, -iTime * .2);
vec3 C = vec3(0, 0, 0);
float alpha = 0.;
vec3 n;
float tt;
if(SphereIntersect(apos,arad,ro,rd,tt,n)){
float thickness = THICKNESS;
int steps = STEPS;
float march_step = thickness / float(steps);
vec3 dir_step = rd / rd.y * march_step;
vec3 pos =n;
float T = 1.;
for (int i = 0; i < steps; i++) {
float h = float(i) / float(steps);
float dens = density (pos, WIND, h);
float T_i = exp(-ABSORPTION * dens * march_step);
T *= T_i;
if (T < .01) break;
C += T * (exp(h) / 1.75) *dens * march_step;
alpha += (1. - T_i) * (1. - alpha);
pos += dir_step;
if (length(pos) > 1e3) break;
}
return vec4(C, alpha);
}
return vec4(C, alpha);
}
float fbm2(in vec3 p)
{
float f = 0.;
f += .50000 * noise(.5 * (p+vec3(0.,0.,-iTime*0.275)));
f += .25000 * noise(1. * (p+vec3(0.,0.,-iTime*0.275)));
f += .12500 * noise(2. * (p+vec3(0.,0.,-iTime*0.275)));
f += .06250 * noise(4. * (p+vec3(0.,0.,-iTime*0.275)));
return f;
}
vec3 cube_bot(vec3 d, vec3 c1, vec3 c2)
{
return fbm2(d) * mix(c1, c2, d * .5 + .5);
}
vec3 rotate_y(vec3 v, float angle)
{
float ca = cos(angle); float sa = sin(angle);
return v*mat3(
vec3(+ca, +.0, -sa),
vec3(+.0,+1.0, +.0),
vec3(+sa, +.0, +ca));
}
vec3 rotate_x(vec3 v, float angle)
{
float ca = cos(angle); float sa = sin(angle);
return v*mat3(
vec3(+1.0, +.0, +.0),
vec3(+.0, +ca, -sa),
vec3(+.0, +sa, +ca));
}
void panorama_uv(vec2 fragCoord, out vec3 ro,out vec3 rd, in vec2 iResolution){
float M_PI = 3.1415926535;
float ymul = 2.0; float ydiff = -1.0;
vec2 uv = fragCoord.xy / iResolution.xy;
uv.x = 2.0 * uv.x - 1.0;
uv.y = ymul * uv.y + ydiff;
ro = vec3(0., 5., 0.);
rd = normalize(rotate_y(rotate_x(vec3(0.0, 0.0, 1.0),-uv.y*M_PI/2.0),-uv.x*M_PI));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord, in vec2 iResolution)
{
vec3 ro = vec3 (0.,0.,0.);
vec3 rd = vec3(0.);
vec3 col=vec3(0.);
panorama_uv(fragCoord,ro,rd,iResolution);
// vec3 sky = render_sky_color(rd);
vec3 sky = vec3( 0.0, 0.0, 0.0);
vec4 cld = vec4(0.);
float skyPow = dot(rd, vec3(0.0, -1.0, 0.0));
float horizonPow =1.-pow(1.0-abs(skyPow), 5.0);
if(rd.y>0.)
{cld=render_clouds(ro,rd);
cld=clamp(cld,vec4(0.),vec4(1.));
cld.rgb+=0.04*cld.rgb*horizonPow;
cld*=clamp(( 1.0 - exp(-2.3 * pow(max((0.0), horizonPow), (2.6)))),0.,1.);
}
else{
cld.rgb = cube_bot(rd,vec3(1.5,1.49,1.71), vec3(1.1,1.15,1.5));
cld*=cld;
//cld=clamp(cld,vec4(0.),vec4(1.));
cld.a=1.;
cld*=clamp(( 1.0 - exp(-1.3 * pow(max((0.0), horizonPow), (2.6)))),0.,1.);
}
col=mix(sky, cld.rgb/(0.0001+cld.a), cld.a);
//col*=col;
fragColor = vec4(col,1.0);
}
void fragment(){
//////////////////////////////////
vec3 dir = ray_dir_from_uv(UV);
// determine our sky color
vec3 color = atmosphere(
vec3( dir.x, -dir.y, dir.z )
, vec3(0.0, earth_radius_km * 100.0 + cam_height_m * 0.1, 0.0)
, sun_pos
, sun_intensity
, earth_radius_km * 100.0
, atmo_radius_km * 100.0
, rayleigh_coeff / 100000.0
, mie_coeff / 100000.0
, rayleigh_scale
, mie_scale
, mie_scatter_dir
);
// Apply exposure.
color = 1.0 - exp(-1.0 * color);
// Mix in night sky (already sRGB)
if (dir.y > 0.0) {
float f = (0.21 * color.r) + (0.72 * color.g) + (0.07 * color.b);
float cutoff = 0.1;
vec2 ns_uv = uv_from_ray_dir(rotate_night_sky * dir);
color += texture(night_sky, ns_uv).rgb * clamp((cutoff - f) / cutoff, 0.0, 1.0);
}
COLOR = vec4(color, 1.0);
////////////////////////////////////////
vec2 iResolution=1./TEXTURE_PIXEL_SIZE;
mainImage(COLOR,UV*iResolution,iResolution);
COLOR = vec4(color, 1.0)+COLOR;
}

View file

@ -26,6 +26,12 @@ Creatures="*res://ressources/scripts/creatures.gd"
MusicManager="*res://scenes/interfaces/music_manager/music_manager.tscn" MusicManager="*res://scenes/interfaces/music_manager/music_manager.tscn"
Connection="*res://scenes/connection/connection.tscn" Connection="*res://scenes/connection/connection.tscn"
[debug]
gdscript/warnings/unused_variable=false
gdscript/warnings/unused_argument=false
gdscript/warnings/return_value_discarded=false
[display] [display]
window/size/width=1280 window/size/width=1280

View file

@ -1,13 +1,84 @@
[gd_scene load_steps=5 format=2] [gd_scene load_steps=14 format=2]
[ext_resource path="res://scenes/player/player.tscn" type="PackedScene" id=1] [ext_resource path="res://scenes/player/player.tscn" type="PackedScene" id=1]
[ext_resource path="res://scenes/decors/terrains/dunes/dunes.tscn" type="PackedScene" id=2] [ext_resource path="res://scenes/decors/terrains/dunes/dunes.tscn" type="PackedScene" id=2]
[ext_resource path="res://scenes/game/game.gd" type="Script" id=3] [ext_resource path="res://scenes/game/game.gd" type="Script" id=3]
[ext_resource path="res://scenes/decors/vegets/tree_001.tscn" type="PackedScene" id=4] [ext_resource path="res://scenes/decors/vegets/tree_001.tscn" type="PackedScene" id=4]
[ext_resource path="res://assets/sky/sky.shader" type="Shader" id=5]
[ext_resource path="res://scenes/game/sky.gd" type="Script" id=6]
[sub_resource type="OpenSimplexNoise" id=1]
period = 8.0
[sub_resource type="NoiseTexture" id=4]
resource_local_to_scene = true
width = 1280
height = 720
seamless = true
noise = SubResource( 1 )
[sub_resource type="ShaderMaterial" id=5]
resource_local_to_scene = true
shader = ExtResource( 5 )
shader_param/iTime = 6165.88
shader_param/iFrame = 199437
shader_param/COVERAGE = 0.5
shader_param/THICKNESS = 25.0
shader_param/ABSORPTION = 1.031
shader_param/STEPS = 50
shader_param/earth_radius_km = 6371.0
shader_param/atmo_radius_km = 6471.0
shader_param/cam_height_m = 1.8
shader_param/sun_pos = Vector3( -1.15706e-06, 100, 8.66537e-06 )
shader_param/sun_intensity = 42.0
shader_param/rayleigh_coeff = Vector3( 5.5, 13, 22.4 )
shader_param/mie_coeff = 21.0
shader_param/rayleigh_scale = 800.0
shader_param/mie_scale = 120.0
shader_param/mie_scatter_dir = 0.758
shader_param/rotate_night_sky = null
shader_param/iChannel0 = SubResource( 4 )
[sub_resource type="ImageTexture" id=6]
resource_local_to_scene = true
size = Vector2( 1280, 720 )
[sub_resource type="ViewportTexture" id=2]
viewport_path = NodePath("sky/viewport")
[sub_resource type="PanoramaSky" id=3]
resource_local_to_scene = true
panorama = SubResource( 2 )
[sub_resource type="Environment" id=7]
resource_local_to_scene = true
background_mode = 2
background_sky = SubResource( 3 )
background_energy = 0.1
ambient_light_energy = 3.82
[node name="game" type="Spatial"] [node name="game" type="Spatial"]
script = ExtResource( 3 ) script = ExtResource( 3 )
[node name="sky" type="Spatial" parent="."]
[node name="viewport" type="Viewport" parent="sky"]
size = Vector2( 1280, 720 )
render_target_update_mode = 3
[node name="sky" type="Sprite" parent="sky/viewport"]
material = SubResource( 5 )
texture = SubResource( 6 )
offset = Vector2( 640, 360 )
script = ExtResource( 6 )
editor_clouds_playing = true
day_time_hours = 3.67154
directional_light_node_path = NodePath("../../../directional_light")
sun_position = Vector3( -1.15706e-06, 100, 8.66537e-06 )
[node name="world_environment" type="WorldEnvironment" parent="."]
environment = SubResource( 7 )
[node name="level" type="Spatial" parent="."] [node name="level" type="Spatial" parent="."]
[node name="dunes" parent="level" instance=ExtResource( 2 )] [node name="dunes" parent="level" instance=ExtResource( 2 )]
@ -22,5 +93,6 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -11.2768, 8, 13.9512 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 14.7098, 0 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 14.7098, 0 )
[node name="directional_light" type="DirectionalLight" parent="."] [node name="directional_light" type="DirectionalLight" parent="."]
transform = Transform( 0.556412, 0.175846, -0.812086, 0.830907, -0.117754, 0.543809, 0, -0.977349, -0.211632, -12.4893, 10.8693, -9.53674e-07 ) transform = Transform( 0.991203, 0.132352, -1.15706e-08, 0, 8.74228e-08, 1, 0.132352, -0.991203, 8.66537e-08, -1.15706e-06, 100, 8.66537e-06 )
light_energy = 1.0
shadow_enabled = true shadow_enabled = true

71
scenes/game/sky.gd Normal file
View file

@ -0,0 +1,71 @@
tool
extends Sprite
export(bool) var editor_clouds_playing = false
export(float, 0.0, 24.0) var day_time_hours = 12.0 setget set_day_time_hours
export(NodePath) var directional_light_node_path
func _ready():
if !Engine.is_editor_hint():
editor_clouds_playing = true
var iTime = 0.0
var iFrame = 0
func _process( delta ):
iTime+=delta
iFrame+=1
if (Engine.is_editor_hint() and self.editor_clouds_playing) or !Engine.is_editor_hint():
self.material.set("shader_param/iTime", iTime)
self.material.set("shader_param/iFrame", iFrame)
day_time_hours += delta
if day_time_hours >= 24:
day_time_hours = 0
func cov_scb(value):
self.material.set("shader_param/COVERAGE",float(value))
func thick_scb(value):
self.material.set("shader_param/THICKNESS",value)
func absb_scb(value):
self.material.set("shader_param/ABSORPTION",float(value))
func step_scb(value):
self.material.set("shader_param/STEPS",value)
export var sun_position = Vector3(0.0, 1.0, 0.0) setget set_sun_position, get_sun_position
func set_sun_position(new_position):
sun_position = new_position
if self.material:
self.material.set_shader_param("sun_pos", sun_position)
# _trigger_update_sky()
func get_sun_position():
return sun_position
func set_time_of_day(hours, directional_light, horizontal_angle = 0.0):
var sun_position = Vector3(0.0, -100.0, 0.0)
sun_position = sun_position.rotated(Vector3(1.0, 0.0, 0.0), hours * PI / 12.0)
sun_position = sun_position.rotated(Vector3(0.0, 1.0, 0.0), horizontal_angle)
if directional_light:
var t = directional_light.transform
t.origin = sun_position
directional_light.transform = t.looking_at(Vector3(0.0, 0.0, 0.0), Vector3(0.0, 1.0, 0.0))
directional_light.light_energy = 1.0 - clamp(abs(hours - 12.0) / 6.0, 0.0, 1.0)
# and update our sky
set_sun_position(sun_position)
func set_day_time_hours(hours):
if directional_light_node_path:
set_time_of_day(hours, get_node(directional_light_node_path), 25.0)
day_time_hours = hours