Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion drivers/gles3/shaders/scene.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -1644,20 +1644,28 @@ void reflection_process(samplerCube reflection_map,
vec3 ref_normal = normalize(reflect(vertex, normal));
ref_normal = (local_matrix * vec4(ref_normal, 0.0)).xyz;

float distance_to_hit_point = 0.0;
float mip = sqrt(roughness) * MAX_ROUGHNESS_LOD;
if (use_box_project) { //box project

vec3 nrdir = normalize(ref_normal);
vec3 rbmax = (box_extents - local_pos) / nrdir;
vec3 rbmin = (-box_extents - local_pos) / nrdir;

vec3 rbminmax = mix(rbmin, rbmax, vec3(greaterThan(nrdir, vec3(0.0, 0.0, 0.0))));
distance_to_hit_point = min(min(rbminmax.x, rbminmax.y), rbminmax.z);

float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);
vec3 posonbox = local_pos + nrdir * fa;
ref_normal = posonbox - box_offset.xyz;

// This clamp helps to make sure that when a fragment is far away, the mip level doesn't climb to a high value and look weird.
float mip_offset = clamp(distance_to_hit_point, 0.0, MAX_ROUGHNESS_LOD);
// Compute new mip level based on the mip offset value (this is mostly arbitrary).
mip = mix(0.0, mip, mip_offset / MAX_ROUGHNESS_LOD);
}

reflection.rgb = srgb_to_linear(textureLod(reflection_map, ref_normal, roughness * MAX_ROUGHNESS_LOD).rgb);
reflection.rgb = srgb_to_linear(textureLod(reflection_map, ref_normal, mip).rgb);

if (exterior) {
reflection.rgb = mix(skybox, reflection.rgb, blend);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -915,23 +915,31 @@ void reflection_process(uint ref_index, vec3 vertex, vec3 ref_vec, vec3 normal,

vec3 local_ref_vec = (reflections.data[ref_index].local_matrix * vec4(ref_vec, 0.0)).xyz;

float distance_to_hit_point = 0.0;
float mip = sqrt(roughness) * MAX_ROUGHNESS_LOD;
if (reflections.data[ref_index].box_project) { //box project

vec3 nrdir = normalize(local_ref_vec);
vec3 rbmax = (box_extents - local_pos) / nrdir;
vec3 rbmin = (-box_extents - local_pos) / nrdir;

vec3 rbminmax = mix(rbmin, rbmax, greaterThan(nrdir, vec3(0.0, 0.0, 0.0)));
distance_to_hit_point = min(min(rbminmax.x, rbminmax.y), rbminmax.z);

float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);
vec3 posonbox = local_pos + nrdir * fa;
local_ref_vec = posonbox - reflections.data[ref_index].box_offset;

// This clamp helps to make sure that when a fragment is far away, the mip level doesn't climb to a high value and look weird.
float mip_offset = clamp(distance_to_hit_point, 0.0, MAX_ROUGHNESS_LOD);
// Compute new mip level based on the mip offset value (this is mostly arbitrary).
mip = mix(0.0, mip, mip_offset / MAX_ROUGHNESS_LOD);
}

vec4 reflection;
float reflection_blend = max(0.0, blend - reflection_accum.a);

reflection.rgb = textureLod(samplerCubeArray(reflection_atlas, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec4(local_ref_vec, reflections.data[ref_index].index), sqrt(roughness) * MAX_ROUGHNESS_LOD).rgb * sc_luminance_multiplier();
reflection.rgb = textureLod(samplerCubeArray(reflection_atlas, DEFAULT_SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP), vec4(local_ref_vec, reflections.data[ref_index].index), mip).rgb * sc_luminance_multiplier();
reflection.rgb *= reflections.data[ref_index].exposure_normalization;
reflection.a = reflection_blend;

Expand Down
Loading