diff --git a/examples/36-sky/fs_sky.sc b/examples/36-sky/fs_sky.sc index 0abe4bafc..7c53d5018 100644 --- a/examples/36-sky/fs_sky.sc +++ b/examples/36-sky/fs_sky.sc @@ -5,22 +5,22 @@ $input v_skyColor, v_screenPos, v_viewDir * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause */ -uniform vec4 u_parameters; // x - sun size, y - sun bloom, z - exposition, w - time -uniform vec4 u_sunDirection; -uniform vec4 u_sunLuminance; +uniform vec4 u_parameters; // x - sun size, y - sun bloom, z - exposition, w - time +uniform vec4 u_sunDirection; +uniform vec4 u_sunLuminance; #include "../common/common.sh" void main() { float size2 = u_parameters.x * u_parameters.x; - + vec3 lightDir = normalize(u_sunDirection.xyz); - float distance = 2.0 * (1.0 - dot(normalize(v_viewDir), lightDir)); - float sun = exp(-distance/ u_parameters.y / size2) + step(distance, size2); + float dist = 2.0 * (1.0 - dot(normalize(v_viewDir), lightDir)); + float sun = exp(-dist/ u_parameters.y / size2) + step(dist, size2); float sun2 = min(sun * sun, 1.0); vec3 color = v_skyColor + sun2; color = toGamma(color); - + gl_FragColor = vec4(color, 1.0); } diff --git a/examples/36-sky/vs_sky.sc b/examples/36-sky/vs_sky.sc index 5c8c4599f..10751bd1f 100644 --- a/examples/36-sky/vs_sky.sc +++ b/examples/36-sky/vs_sky.sc @@ -7,10 +7,10 @@ $output v_skyColor, v_screenPos, v_viewDir */ -uniform vec4 u_sunDirection; -uniform vec4 u_skyLuminanceXYZ; -uniform vec4 u_parameters; // x - sun size, y - sun bloom, z - exposition -uniform vec4 u_perezCoeff[5]; +uniform vec4 u_sunDirection; +uniform vec4 u_skyLuminanceXYZ; +uniform vec4 u_parameters; // x - sun size, y - sun bloom, z - exposition +uniform vec4 u_perezCoeff[5]; #include "../common/common.sh" @@ -19,56 +19,52 @@ vec3 Perez(vec3 A,vec3 B,vec3 C,vec3 D, vec3 E,float costeta, float cosgamma) float _1_costeta = 1.0 / costeta; float cos2gamma = cosgamma * cosgamma; float gamma = acos(cosgamma); - vec3 f = (vec3(1.0, 1.0, 1.0) + A * exp(B * _1_costeta)) * (vec3(1.0, 1.0, 1.0) + C *exp(D * gamma) + E * cos2gamma); + vec3 f = (vec3_splat(1.0) + A * exp(B * _1_costeta)) + * (vec3_splat(1.0) + C * exp(D * gamma) + E * cos2gamma); return f; } void main() { v_screenPos = a_position.xy; - + vec4 rayStart = mul(u_invViewProj, vec4(vec3(a_position.xy, -1.0), 1.0)); vec4 rayEnd = mul(u_invViewProj, vec4(vec3(a_position.xy, 1.0), 1.0)); - + rayStart = rayStart / rayStart.w; rayEnd = rayEnd / rayEnd.w; - + v_viewDir = normalize(rayEnd.xyz - rayStart.xyz); v_viewDir.y = abs(v_viewDir.y); - + gl_Position = vec4(a_position.xy, 1.0, 1.0); vec3 lightDir = normalize(u_sunDirection.xyz); vec3 skyDir = vec3(0.0, 1.0, 0.0); - // Perez coefficients. + // Perez coefficients. vec3 A = u_perezCoeff[0].xyz; vec3 B = u_perezCoeff[1].xyz; vec3 C = u_perezCoeff[2].xyz; vec3 D = u_perezCoeff[3].xyz; vec3 E = u_perezCoeff[4].xyz; - + float costeta = max(dot(v_viewDir, skyDir), 0.001); float cosgamma = clamp(dot(v_viewDir, lightDir), -0.9999, 0.9999); float cosgammas = dot(skyDir, lightDir); - - vec3 P = Perez(A,B,C,D,E, costeta, cosgamma); - vec3 P0 = Perez(A,B,C,D,E, 1.0, cosgammas); - - vec3 skyColorxyY = vec3(u_skyLuminanceXYZ.x / (u_skyLuminanceXYZ.x+u_skyLuminanceXYZ.y + u_skyLuminanceXYZ.z), - u_skyLuminanceXYZ.y / (u_skyLuminanceXYZ.x+u_skyLuminanceXYZ.y + u_skyLuminanceXYZ.z), - u_skyLuminanceXYZ.y); - - vec3 Yp = skyColorxyY * P / P0; - - vec3 skyColorXYZ = vec3(Yp.x * Yp.z / Yp.y,Yp.z, (1.0 - Yp.x- Yp.y)*Yp.z/Yp.y); - - // HDTV rec. 709 matrix - mat3 m = mat3( - 3.240479, -0.969256, 0.055648, - -1.53715, 1.875991, -0.204043, - -0.49853, 0.041556, 1.057311 - ); - v_skyColor = mul((skyColorXYZ * u_parameters.z), m); + vec3 P = Perez(A,B,C,D,E, costeta, cosgamma); + vec3 P0 = Perez(A,B,C,D,E, 1.0, cosgammas); + + vec3 skyColorxyY = vec3( + u_skyLuminanceXYZ.x / (u_skyLuminanceXYZ.x+u_skyLuminanceXYZ.y + u_skyLuminanceXYZ.z) + , u_skyLuminanceXYZ.y / (u_skyLuminanceXYZ.x+u_skyLuminanceXYZ.y + u_skyLuminanceXYZ.z) + , u_skyLuminanceXYZ.y + ); + + vec3 Yp = skyColorxyY * P / P0; + + vec3 skyColorXYZ = vec3(Yp.x * Yp.z / Yp.y,Yp.z, (1.0 - Yp.x- Yp.y)*Yp.z/Yp.y); + + v_skyColor = convertXYZ2RGB(skyColorXYZ * u_parameters.z); } diff --git a/examples/runtime/shaders/dx11/vs_sky.bin b/examples/runtime/shaders/dx11/vs_sky.bin index 34523302e..b51c22b85 100644 Binary files a/examples/runtime/shaders/dx11/vs_sky.bin and b/examples/runtime/shaders/dx11/vs_sky.bin differ diff --git a/examples/runtime/shaders/dx9/vs_sky.bin b/examples/runtime/shaders/dx9/vs_sky.bin index 07984d2af..e3c224b40 100644 Binary files a/examples/runtime/shaders/dx9/vs_sky.bin and b/examples/runtime/shaders/dx9/vs_sky.bin differ diff --git a/examples/runtime/shaders/essl/vs_sky.bin b/examples/runtime/shaders/essl/vs_sky.bin index 30651ac7e..81358a9d3 100644 Binary files a/examples/runtime/shaders/essl/vs_sky.bin and b/examples/runtime/shaders/essl/vs_sky.bin differ diff --git a/examples/runtime/shaders/glsl/vs_sky.bin b/examples/runtime/shaders/glsl/vs_sky.bin index bcbc1dda1..2f4fd1a8b 100644 Binary files a/examples/runtime/shaders/glsl/vs_sky.bin and b/examples/runtime/shaders/glsl/vs_sky.bin differ diff --git a/examples/runtime/shaders/metal/vs_sky.bin b/examples/runtime/shaders/metal/vs_sky.bin index f0b21f2b3..bec2f65e0 100644 Binary files a/examples/runtime/shaders/metal/vs_sky.bin and b/examples/runtime/shaders/metal/vs_sky.bin differ