diff options
author | Cosmic Linden <cosmic@lindenlab.com> | 2023-10-13 09:58:35 -0700 |
---|---|---|
committer | Cosmic Linden <cosmic@lindenlab.com> | 2023-10-13 09:58:35 -0700 |
commit | 1820f23b96a50962a3a6a3d48de1aff8e1acb2c0 (patch) | |
tree | dd8d4f4a5770e6c830ce5a3f5533c409bbf1a4f1 /indra | |
parent | d2da77698005579f9fdaecad5a5d80189ad03f23 (diff) |
DRTVWR-592: Fix issues with blending of materials, for example when fading out an emissive texture. Also affects base color and ORM
Diffstat (limited to 'indra')
-rw-r--r-- | indra/newview/app_settings/shaders/class1/deferred/pbrterrainF.glsl | 105 |
1 files changed, 62 insertions, 43 deletions
diff --git a/indra/newview/app_settings/shaders/class1/deferred/pbrterrainF.glsl b/indra/newview/app_settings/shaders/class1/deferred/pbrterrainF.glsl index 4de6b9609c..85691ae9b6 100644 --- a/indra/newview/app_settings/shaders/class1/deferred/pbrterrainF.glsl +++ b/indra/newview/app_settings/shaders/class1/deferred/pbrterrainF.glsl @@ -80,38 +80,63 @@ vec4 terrain_mix(vec4[4] samples, float alpha1, float alpha2, float alphaFinal) return mix( mix(samples[3], samples[2], alpha2), mix(samples[1], samples[0], alpha1), alphaFinal ); } -vec3 sample_and_mix_color3(float alpha1, float alpha2, float alphaFinal, vec2 texcoord, sampler2D tex0, sampler2D tex1, sampler2D tex2, sampler2D tex3) +vec3 sample_and_mix_color3(float alpha1, float alpha2, float alphaFinal, vec2 texcoord, vec3[4] factors, sampler2D tex0, sampler2D tex1, sampler2D tex2, sampler2D tex3) { vec3[4] samples; - samples[0] = srgb_to_linear(texture2D(tex0, texcoord).xyz); - samples[1] = srgb_to_linear(texture2D(tex1, texcoord).xyz); - samples[2] = srgb_to_linear(texture2D(tex2, texcoord).xyz); - samples[3] = srgb_to_linear(texture2D(tex3, texcoord).xyz); + samples[0] = texture2D(tex0, texcoord).xyz; + samples[1] = texture2D(tex1, texcoord).xyz; + samples[2] = texture2D(tex2, texcoord).xyz; + samples[3] = texture2D(tex3, texcoord).xyz; + samples[0] = srgb_to_linear(samples[0]); + samples[1] = srgb_to_linear(samples[1]); + samples[2] = srgb_to_linear(samples[2]); + samples[3] = srgb_to_linear(samples[3]); + samples[0] *= factors[0]; + samples[1] *= factors[1]; + samples[2] *= factors[2]; + samples[3] *= factors[3]; return terrain_mix(samples, alpha1, alpha2, alphaFinal); } -vec4 sample_and_mix_color4(float alpha1, float alpha2, float alphaFinal, vec2 texcoord, sampler2D tex0, sampler2D tex1, sampler2D tex2, sampler2D tex3) +vec4 sample_and_mix_color4(float alpha1, float alpha2, float alphaFinal, vec2 texcoord, vec4[4] factors, sampler2D tex0, sampler2D tex1, sampler2D tex2, sampler2D tex3) { vec4[4] samples; samples[0] = texture2D(tex0, texcoord); samples[1] = texture2D(tex1, texcoord); samples[2] = texture2D(tex2, texcoord); samples[3] = texture2D(tex3, texcoord); - // TODO: Why is this needed for pbropaqueF but not here? (and is there different behavior with base color vs emissive color, that also needs to be corrected?) - //samples[0].xyz = srgb_to_linear(samples[0].xyz); - //samples[1].xyz = srgb_to_linear(samples[1].xyz); - //samples[2].xyz = srgb_to_linear(samples[2].xyz); - //samples[3].xyz = srgb_to_linear(samples[3].xyz); + samples[0].xyz = srgb_to_linear(samples[0].xyz); + samples[1].xyz = srgb_to_linear(samples[1].xyz); + samples[2].xyz = srgb_to_linear(samples[2].xyz); + samples[3].xyz = srgb_to_linear(samples[3].xyz); + samples[0] *= factors[0]; + samples[1] *= factors[1]; + samples[2] *= factors[2]; + samples[3] *= factors[3]; return terrain_mix(samples, alpha1, alpha2, alphaFinal); } -vec4 sample_and_mix_vector(float alpha1, float alpha2, float alphaFinal, vec2 texcoord, sampler2D tex0, sampler2D tex1, sampler2D tex2, sampler2D tex3) +vec3 sample_and_mix_vector3(float alpha1, float alpha2, float alphaFinal, vec2 texcoord, vec3[4] factors, sampler2D tex0, sampler2D tex1, sampler2D tex2, sampler2D tex3) { - vec4[4] samples; - samples[0] = texture2D(tex0, texcoord); - samples[1] = texture2D(tex1, texcoord); - samples[2] = texture2D(tex2, texcoord); - samples[3] = texture2D(tex3, texcoord); + vec3[4] samples; + samples[0] = texture2D(tex0, texcoord).xyz; + samples[1] = texture2D(tex1, texcoord).xyz; + samples[2] = texture2D(tex2, texcoord).xyz; + samples[3] = texture2D(tex3, texcoord).xyz; + samples[0] *= factors[0]; + samples[1] *= factors[1]; + samples[2] *= factors[2]; + samples[3] *= factors[3]; + return terrain_mix(samples, alpha1, alpha2, alphaFinal); +} + +vec3 sample_and_mix_vector3_no_scale(float alpha1, float alpha2, float alphaFinal, vec2 texcoord, sampler2D tex0, sampler2D tex1, sampler2D tex2, sampler2D tex3) +{ + vec3[4] samples; + samples[0] = texture2D(tex0, texcoord).xyz; + samples[1] = texture2D(tex1, texcoord).xyz; + samples[2] = texture2D(tex2, texcoord).xyz; + samples[3] = texture2D(tex3, texcoord).xyz; return terrain_mix(samples, alpha1, alpha2, alphaFinal); } @@ -124,23 +149,30 @@ void main() float alpha2 = texture2D(alpha_ramp,vary_texcoord1.xy).a; float alphaFinal = texture2D(alpha_ramp, vary_texcoord1.zw).a; - vec4 base_color = sample_and_mix_color4(alpha1, alpha2, alphaFinal, terrain_texcoord, detail_0_base_color, detail_1_base_color, detail_2_base_color, detail_3_base_color); + vec4 col = sample_and_mix_color4(alpha1, alpha2, alphaFinal, terrain_texcoord, baseColorFactors, detail_0_base_color, detail_1_base_color, detail_2_base_color, detail_3_base_color); float minimum_alpha = terrain_mix(minimum_alphas, alpha1, alpha2, alphaFinal); - if (base_color.a < minimum_alpha) + if (col.a < minimum_alpha) { discard; } - vec4 normal_texture = sample_and_mix_vector(alpha1, alpha2, alphaFinal, terrain_texcoord, detail_0_normal, detail_1_normal, detail_2_normal, detail_3_normal); - vec4 metallic_roughness = sample_and_mix_vector(alpha1, alpha2, alphaFinal, terrain_texcoord, detail_0_metallic_roughness, detail_1_metallic_roughness, detail_2_metallic_roughness, detail_3_metallic_roughness); - vec3 emissive_texture = sample_and_mix_color3(alpha1, alpha2, alphaFinal, terrain_texcoord, detail_0_emissive, detail_1_emissive, detail_2_emissive, detail_3_emissive); + vec3 normal_texture = sample_and_mix_vector3_no_scale(alpha1, alpha2, alphaFinal, terrain_texcoord, detail_0_normal, detail_1_normal, detail_2_normal, detail_3_normal); - vec4 baseColorFactor = terrain_mix(baseColorFactors, alpha1, alpha2, alphaFinal); - float metallicFactor = terrain_mix(metallicFactors, alpha1, alpha2, alphaFinal); - float roughnessFactor = terrain_mix(roughnessFactors, alpha1, alpha2, alphaFinal); - vec3 emissiveColor = terrain_mix(emissiveColors, alpha1, alpha2, alphaFinal); + vec3[4] orm_factors; + orm_factors[0] = vec3(1.0, roughnessFactors.x, metallicFactors.x); + orm_factors[1] = vec3(1.0, roughnessFactors.y, metallicFactors.y); + orm_factors[2] = vec3(1.0, roughnessFactors.z, metallicFactors.z); + orm_factors[3] = vec3(1.0, roughnessFactors.w, metallicFactors.w); + // RGB = Occlusion, Roughness, Metal + // default values, see LLViewerTexture::sDefaultPBRORMImagep + // occlusion 1.0 + // roughness 0.0 + // metal 0.0 + vec3 spec = sample_and_mix_vector3(alpha1, alpha2, alphaFinal, terrain_texcoord, orm_factors, detail_0_metallic_roughness, detail_1_metallic_roughness, detail_2_metallic_roughness, detail_3_metallic_roughness); - vec3 col = baseColorFactor.rgb * srgb_to_linear(base_color.rgb); + vec3 emissive = sample_and_mix_color3(alpha1, alpha2, alphaFinal, terrain_texcoord, emissiveColors, detail_0_emissive, detail_1_emissive, detail_2_emissive, detail_3_emissive); + + float base_color_factor_alpha = terrain_mix(vec4(baseColorFactors[0].z, baseColorFactors[1].z, baseColorFactors[2].z, baseColorFactors[3].z), alpha1, alpha2, alphaFinal); // from mikktspace.com vec3 vNt = normal_texture.xyz*2.0-1.0; @@ -151,25 +183,12 @@ void main() vec3 vB = sign * cross(vN, vT); vec3 tnorm = normalize( vNt.x * vT + vNt.y * vB + vNt.z * vN ); - // RGB = Occlusion, Roughness, Metal - // default values, see LLViewerTexture::sDefaultPBRORMImagep - // occlusion 1.0 - // roughness 0.0 - // metal 0.0 - vec3 spec = metallic_roughness.rgb; - - spec.g *= roughnessFactor; - spec.b *= metallicFactor; - - vec3 emissive = emissiveColor; - emissive *= emissive_texture.rgb; - tnorm *= gl_FrontFacing ? 1.0 : -1.0; - frag_data[0] = max(vec4(col, 0.0), vec4(0)); // Diffuse - frag_data[1] = max(vec4(spec.rgb,baseColorFactor.a), vec4(0)); // PBR linear packed Occlusion, Roughness, Metal. - frag_data[2] = max(vec4(encode_normal(tnorm), baseColorFactor.a, GBUFFER_FLAG_HAS_PBR), vec4(0)); // normal, environment intensity, flags + frag_data[0] = max(vec4(col.xyz, 0.0), vec4(0)); // Diffuse + frag_data[1] = max(vec4(spec.rgb, base_color_factor_alpha), vec4(0)); // PBR linear packed Occlusion, Roughness, Metal. + frag_data[2] = max(vec4(encode_normal(tnorm), base_color_factor_alpha, GBUFFER_FLAG_HAS_PBR), vec4(0)); // normal, environment intensity, flags frag_data[3] = max(vec4(emissive,0), vec4(0)); // PBR sRGB Emissive } |