Archived
1
Fork 0

Clean up sky shaders and extra push constant data

This commit is contained in:
redstrate 2020-08-14 23:32:02 -04:00
parent 3b33741ae4
commit 9cd7860ab9
4 changed files with 46 additions and 40 deletions

View file

@ -480,16 +480,16 @@ void Renderer::render_camera(GFXCommandBuffer* command_buffer, Scene& scene, Obj
struct SkyPushConstant {
Matrix4x4 view;
Vector4 aspectFovY;
Vector4 sunPosition;
Vector4 sun_position_fov;
float aspect;
} pc;
pc.view = matrix_from_quat(scene.get<Transform>(camera_object).rotation) * correction_matrix;
pc.aspectFovY = Vector4(static_cast<float>(extent.width) / static_cast<float>(extent.height), radians(camera.fov), 0, 0);
pc.aspect = static_cast<float>(extent.width) / static_cast<float>(extent.height);
for(const auto& [obj, light] : scene.get_all<Light>()) {
if(light.type == Light::Type::Sun)
pc.sunPosition = Vector4(scene.get<Transform>(obj).get_world_position());
pc.sun_position_fov = Vector4(scene.get<Transform>(obj).get_world_position(), radians(camera.fov));
}
command_buffer->set_pipeline(skyPipeline);
@ -887,7 +887,7 @@ void Renderer::createSkyPipeline() {
};
pipelineInfo.shader_input.push_constants = {
{sizeof(Matrix4x4) + sizeof(Vector4) + sizeof(Vector4), 0}
{sizeof(Matrix4x4) + sizeof(Vector4) + sizeof(float), 0}
};
pipelineInfo.depth.depth_mode = GFXDepthMode::LessOrEqual;

View file

@ -281,17 +281,17 @@ void SceneCapture::render(GFXCommandBuffer* command_buffer, Scene* scene) {
// render sky
struct SkyPushConstant {
Matrix4x4 view;
Vector4 aspectFovY;
Vector4 sunPosition;
Vector4 sun_position_fov;
float aspect;
} pc;
pc.view = sceneTransforms[face];
pc.view[3] = Vector4(0, 0, 0, 1); // zero out translation
pc.aspectFovY = Vector4(1.0f, radians(90.0f), 0, 0);
pc.aspect = 1.0f;
for(auto& [obj, light] : scene->get_all<Light>()) {
if(light.type == Light::Type::Sun)
pc.sunPosition = Vector4(scene->get<Transform>(obj).get_world_position());
pc.sun_position_fov = Vector4(scene->get<Transform>(obj).get_world_position(), radians(90.0f));
}
command_buffer->set_pipeline(skyPipeline);
@ -404,7 +404,7 @@ void SceneCapture::createSkyResources() {
};
pipelineInfo.shader_input.push_constants = {
{sizeof(Matrix4x4) + sizeof(Vector4) + sizeof(Vector4), 0}
{sizeof(Matrix4x4) + sizeof(Vector4) + sizeof(float), 0}
};
pipelineInfo.depth.depth_mode = GFXDepthMode::LessOrEqual;

View file

@ -1,42 +1,48 @@
#extension GL_GOOGLE_include_directive : enable
#include "atmosphere.glsl"
layout (location = 0) in vec2 inUV;
layout (location = 0) in vec2 in_uv;
layout (location = 0) out vec4 outColor;
layout (location = 0) out vec4 out_color;
layout(push_constant, binding = 1) uniform PushConstant{
mat4 view;
vec4 aspectFovY, sunPosition;
vec4 sun_position_fov;
float aspect;
};
vec3 skyRay (vec2 Texcoord) {
float d = 0.5 / tan(aspectFovY.y/2.0);
return normalize(vec3((Texcoord.x - 0.5) * aspectFovY.x,
Texcoord.y - 0.5,
vec3 sky_ray(const vec2 uv) {
const float d = 0.5 / tan(sun_position_fov.w / 2.0);
return normalize(vec3((uv.x - 0.5) * aspect,
uv.y - 0.5,
-d));
}
void main() {
vec3 vPosition = mat3(view) * skyRay(inUV);
vec3 color = atmosphere(
normalize(vPosition), // normalized ray direction
vec3(0,6372e3,0), // ray origin
sunPosition.xyz, // position of the sun
22.0, // intensity of the sun
6371e3, // radius of the planet in meters
6471e3, // radius of the atmosphere in meters
vec3(5.5e-6, 13.0e-6, 22.4e-6), // Rayleigh scattering coefficient
21e-6, // Mie scattering coefficient
8e3, // Rayleigh scale height
1.2e3, // Mie scale height
0.758 // Mie preferred scattering direction
const vec3 color = atmosphere(
// ray direction
normalize(mat3(view) * sky_ray(in_uv)),
// ray origin
vec3(0, 6372e3, 0),
// position of the sun in world space (this will be normalized)
sun_position_fov.xyz,
// intensity of the sun
22.0,
// radius of the plant in meters
6371e3,
// radius of the atmosphere in meters
6471e3,
// Rayleigh scattering coefficient
vec3(5.5e-6, 13.0e-6, 22.4e-6),
// Mie scattering coefficient
21e-6,
// Rayleigh scale height
8e3,
// Mie scale height
1.2e3,
// Mie preferred scattering direction
0.758
);
// Apply exposure.
color = 1.0 - exp(-1.0 * color);
outColor = vec4(color, 1);
// apply exposure
out_color = vec4(1.0 - exp(-1.0 * color), 1.0);
}

View file

@ -1,6 +1,6 @@
layout (location = 0) out vec2 outUV;
layout (location = 0) out vec2 out_uv;
void main() {
outUV = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
gl_Position = vec4(outUV * 2.0f + -1.0f, 1.0f, 1.0f);
out_uv = vec2((gl_VertexIndex << 1) & 2, gl_VertexIndex & 2);
gl_Position = vec4(out_uv * 2.0f + -1.0f, 1.0f, 1.0f);
}