Unravel Engine C++ Reference
Loading...
Searching...
No Matches
atmospheric_pass.cpp
Go to the documentation of this file.
1#include "atmospheric_pass.h"
4#include <graphics/texture.h>
5
6namespace unravel
7{
8
9namespace
10{
11#ifndef ANONYMOUS
12#define ANONYMOUS anonymous
13#endif
14namespace ANONYMOUS
15{
16float hour_of_day(math::vec3 sun_dir)
17{
18 // Define the ground normal vector (assuming flat and horizontal ground)
19 math::vec3 normal(0.0, -1.0, 0.0);
20
21 auto v1 = sun_dir;
22 auto v2 = normal;
23 auto ref = math::vec3(-1.0f, 0.0f, 0.0f);
24
25 float angle = math::orientedAngle(v1, v2, ref); // angle in [-pi, pi]
26 angle = math::mod(angle, 2 * math::pi<float>()); // angle in [0, 2pi]
27 angle = math::degrees(angle);
28 // The hour angle is 0 at 6:00, 90 at 12:00, and 180 at 18:00
29 // Therefore, we can use a simple linear formula to map the hour angle to the hour of day
30 float hour_of_day = angle / 15;
31
32 // Return the hour of day
33 return hour_of_day;
34}
35}
36} // namespace
37
39{
40 auto& am = ctx.get_cached<asset_manager>();
41 auto vs_clip_quad_ex = am.get_asset<gfx::shader>("engine:/data/shaders/atmospherics/vs_atmospherics.sc");
42 auto fs_atmospherics = am.get_asset<gfx::shader>("engine:/data/shaders/atmospherics/fs_atmospherics.sc");
43
44 atmospheric_program_.program = std::make_unique<gpu_program>(vs_clip_quad_ex, fs_atmospherics);
45 atmospheric_program_.cache_uniforms();
46
47 return true;
48}
49
51{
52 const auto& view = camera.get_view_relative();
53 const auto& proj = camera.get_projection();
54
55 const auto surface = input.get();
56 const auto output_size = surface->get_size();
57 gfx::render_pass pass("atmospherics_pass");
58 pass.bind(surface);
59 pass.set_view_proj(view, proj);
60
61 auto hour = ANONYMOUS::hour_of_day(-params.light_direction);
62 // APPLOG_TRACE("Time Of Day {}", hour);
63
64 if(atmospheric_program_.program->is_valid())
65 {
66 atmospheric_program_.program->begin();
67
68 auto turbidity = params.turbidity;
69
70 // Interpolation factor based on turbidity range
71 float t = (turbidity - 1.9f) / (10.0f - 1.9f);
72
73 // Define clear and hazy atmospheric conditions
74 math::vec3 kr_clear(0.12867780436772762f, 0.2478442963618773f, 0.6216065586417131f);
75 math::vec3 kr_hazy(0.05f, 0.1f, 0.25f);
76 math::vec3 u_kr = math::mix(kr_clear, kr_hazy, t);
77
78 float rayleigh_brightness_clear = 9.0f;
79 float rayleigh_brightness_hazy = 5.0f; // Example value, adjust as needed
80 float u_rayleigh_brightness = math::mix(rayleigh_brightness_clear, rayleigh_brightness_hazy, t);
81
82 float mie_brightness_clear = 0.1f;
83 float mie_brightness_hazy = 0.5f; // Example value, adjust as needed
84 float u_mie_brightness = math::mix(mie_brightness_clear, mie_brightness_hazy, t);
85
86 float spot_brightness_clear = 10.0f;
87 float spot_brightness_hazy = 5.0f; // Example value, adjust as needed
88 float u_spot_brightness = math::mix(spot_brightness_clear, spot_brightness_hazy, t);
89
90 float spot_distance_clear = 300.0f;
91 float spot_distance_hazy = 100.0f; // Example value, adjust as needed
92 float u_spot_distance = math::mix(spot_distance_clear, spot_distance_hazy, t);
93
94 float scatter_strength_clear = 0.078f;
95 float scatter_strength_hazy = 0.15f;
96 float u_scatter_strength = math::mix(scatter_strength_clear, scatter_strength_hazy, t);
97
98 float rayleigh_strength_clear = 0.139f;
99 float rayleigh_strength_hazy = 0.05f;
100 float u_rayleigh_strength = math::mix(rayleigh_strength_clear, rayleigh_strength_hazy, t);
101
102 float mie_strength_clear = 0.264f;
103 float mie_strength_hazy = 0.5f;
104 float u_mie_strength = math::mix(mie_strength_clear, mie_strength_hazy, t);
105
106 float rayleigh_collection_power_clear = 0.81f;
107 float rayleigh_collection_power_hazy = 0.6f; // Example value, adjust as needed
108 float u_rayleigh_collection_power =
109 math::mix(rayleigh_collection_power_clear, rayleigh_collection_power_hazy, t);
110
111 float mie_collection_power_clear = 0.39f;
112 float mie_collection_power_hazy = 0.6f; // Example value, adjust as needed
113 float u_mie_collection_power = math::mix(mie_collection_power_clear, mie_collection_power_hazy, t);
114
115 float mie_distribution_clear = 0.53f;
116 float mie_distribution_hazy = 0.7f;
117 float u_mie_distribution = math::mix(mie_distribution_clear, mie_distribution_hazy, t);
118
119 float intensity_clear = 1.8f;
120 float intensity_hazy = 0.8f;
121 float u_intensity = math::mix(intensity_clear, intensity_hazy, t);
122
123 math::vec4 u_parameters(params.light_direction, hour);
124
125 math::vec4 u_kr_and_intensity(u_kr, u_intensity);
126 math::vec4 u_turbidity_parameters1(u_rayleigh_strength, u_mie_strength, u_mie_distribution, u_scatter_strength);
127 math::vec4 u_turbidity_parameters2(u_rayleigh_brightness, u_mie_brightness, u_spot_brightness, u_spot_distance);
128 math::vec4 u_turbidity_parameters3(u_rayleigh_collection_power, u_mie_collection_power, 0.0f, 0.0f);
129
130 gfx::set_uniform(atmospheric_program_.u_parameters, u_parameters);
131 gfx::set_uniform(atmospheric_program_.u_kr_and_intensity, u_kr_and_intensity);
132 gfx::set_uniform(atmospheric_program_.u_turbidity_parameters1, u_turbidity_parameters1);
133 gfx::set_uniform(atmospheric_program_.u_turbidity_parameters2, u_turbidity_parameters2);
134 gfx::set_uniform(atmospheric_program_.u_turbidity_parameters3, u_turbidity_parameters3);
135
136 irect32_t rect(0, 0, irect32_t::value_type(output_size.width), irect32_t::value_type(output_size.height));
138 auto topology = gfx::clip_quad(1.0f);
139
140 gfx::set_state(topology | BGFX_STATE_WRITE_RGB | BGFX_STATE_WRITE_A | BGFX_STATE_DEPTH_TEST_EQUAL);
141
142 gfx::submit(pass.id, atmospheric_program_.program->native_handle());
143 gfx::set_state(BGFX_STATE_DEFAULT);
144 atmospheric_program_.program->end();
145 }
146
147 gfx::discard();
148}
149} // namespace unravel
btVector3 normal
Manages assets, including loading, unloading, and storage.
auto get_asset(const std::string &key, load_flags flags=load_flags::standard) -> asset_handle< T >
Gets an asset by its key.
void run(gfx::frame_buffer::ptr input, const camera &camera, gfx::render_view &rview, delta_t dt, const run_params &params)
auto init(rtti::context &ctx) -> bool
Class representing a camera. Contains functionality for manipulating and updating a camera....
Definition camera.h:35
auto get_projection() const -> const math::transform &
Retrieves the current projection matrix.
Definition camera.cpp:203
auto get_view_relative() const -> const math::transform &
Definition camera.cpp:286
std::chrono::duration< float > delta_t
void submit(view_id _id, program_handle _handle, int32_t _depth, bool _preserveState)
Definition graphics.cpp:899
uint16_t set_scissor(uint16_t _x, uint16_t _y, uint16_t _width, uint16_t _height)
Definition graphics.cpp:778
void set_state(uint64_t _state, uint32_t _rgba)
Definition graphics.cpp:763
void discard(uint8_t _flags)
Definition graphics.cpp:968
void set_uniform(uniform_handle _handle, const void *_value, uint16_t _num)
Definition graphics.cpp:803
auto clip_quad(float depth, float width, float height) -> uint64_t
void set_view_proj(const float *v, const float *p)
gfx::view_id id
Definition render_pass.h:98
void bind(const frame_buffer *fb=nullptr) const
T width() const
std::int32_t value_type
T height() const