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 { struct SkyPushConstant {
Matrix4x4 view; Matrix4x4 view;
Vector4 aspectFovY; Vector4 sun_position_fov;
Vector4 sunPosition; float aspect;
} pc; } pc;
pc.view = matrix_from_quat(scene.get<Transform>(camera_object).rotation) * correction_matrix; 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>()) { for(const auto& [obj, light] : scene.get_all<Light>()) {
if(light.type == Light::Type::Sun) 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); command_buffer->set_pipeline(skyPipeline);
@ -887,7 +887,7 @@ void Renderer::createSkyPipeline() {
}; };
pipelineInfo.shader_input.push_constants = { 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; pipelineInfo.depth.depth_mode = GFXDepthMode::LessOrEqual;

View file

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

View file

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

View file

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