diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index 322a8a853e81..b381096df806 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -661,20 +661,19 @@ void BaseMaterial3D::_update_shader() { } if (flags[FLAG_USE_TEXTURE_REPEAT]) { - texfilter_str += ",repeat_enable"; - texfilter_height_str += ",repeat_enable"; + texfilter_str += ", repeat_enable"; + texfilter_height_str += ", repeat_enable"; } else { - texfilter_str += ",repeat_disable"; - texfilter_height_str += ",repeat_disable"; + texfilter_str += ", repeat_disable"; + texfilter_height_str += ", repeat_disable"; } - //must create a shader! - // Add a comment to describe the shader origin (useful when converting to ShaderMaterial). String code = vformat( "// NOTE: Shader automatically converted from " VERSION_NAME " " VERSION_FULL_CONFIG "'s %s.\n\n", orm ? "ORMMaterial3D" : "StandardMaterial3D"); + // Define shader type and render mode based on property values. code += "shader_type spatial;\nrender_mode "; switch (blend_mode) { case BLEND_MODE_MIX: @@ -700,13 +699,13 @@ void BaseMaterial3D::_update_shader() { switch (ddm) { case DEPTH_DRAW_OPAQUE_ONLY: - code += ",depth_draw_opaque"; + code += ", depth_draw_opaque"; break; case DEPTH_DRAW_ALWAYS: - code += ",depth_draw_always"; + code += ", depth_draw_always"; break; case DEPTH_DRAW_DISABLED: - code += ",depth_draw_never"; + code += ", depth_draw_never"; break; case DEPTH_DRAW_MAX: break; // Internal value, skip. @@ -714,238 +713,291 @@ void BaseMaterial3D::_update_shader() { switch (cull_mode) { case CULL_BACK: - code += ",cull_back"; + code += ", cull_back"; break; case CULL_FRONT: - code += ",cull_front"; + code += ", cull_front"; break; case CULL_DISABLED: - code += ",cull_disabled"; + code += ", cull_disabled"; break; case CULL_MAX: break; // Internal value, skip. } switch (diffuse_mode) { case DIFFUSE_BURLEY: - code += ",diffuse_burley"; + code += ", diffuse_burley"; break; case DIFFUSE_LAMBERT: - code += ",diffuse_lambert"; + code += ", diffuse_lambert"; break; case DIFFUSE_LAMBERT_WRAP: - code += ",diffuse_lambert_wrap"; + code += ", diffuse_lambert_wrap"; break; case DIFFUSE_TOON: - code += ",diffuse_toon"; + code += ", diffuse_toon"; break; case DIFFUSE_MAX: break; // Internal value, skip. } switch (specular_mode) { case SPECULAR_SCHLICK_GGX: - code += ",specular_schlick_ggx"; + code += ", specular_schlick_ggx"; break; case SPECULAR_TOON: - code += ",specular_toon"; + code += ", specular_toon"; break; case SPECULAR_DISABLED: - code += ",specular_disabled"; + code += ", specular_disabled"; break; case SPECULAR_MAX: break; // Internal value, skip. } if (features[FEATURE_SUBSURFACE_SCATTERING] && flags[FLAG_SUBSURFACE_MODE_SKIN]) { - code += ",sss_mode_skin"; + code += ", sss_mode_skin"; } if (shading_mode == SHADING_MODE_UNSHADED) { - code += ",unshaded"; + code += ", unshaded"; } if (flags[FLAG_DISABLE_DEPTH_TEST]) { - code += ",depth_test_disabled"; + code += ", depth_test_disabled"; } if (flags[FLAG_PARTICLE_TRAILS_MODE]) { - code += ",particle_trails"; + code += ", particle_trails"; } if (shading_mode == SHADING_MODE_PER_VERTEX) { - code += ",vertex_lighting"; + code += ", vertex_lighting"; } if (flags[FLAG_DONT_RECEIVE_SHADOWS]) { - code += ",shadows_disabled"; + code += ", shadows_disabled"; } if (flags[FLAG_DISABLE_AMBIENT_LIGHT]) { - code += ",ambient_light_disabled"; + code += ", ambient_light_disabled"; } if (flags[FLAG_USE_SHADOW_TO_OPACITY]) { - code += ",shadow_to_opacity"; + code += ", shadow_to_opacity"; } if (flags[FLAG_DISABLE_FOG]) { - code += ",fog_disabled"; + code += ", fog_disabled"; } if (transparency == TRANSPARENCY_ALPHA_DEPTH_PRE_PASS) { - code += ",depth_prepass_alpha"; + code += ", depth_prepass_alpha"; } - // Although its technically possible to do alpha antialiasing without using alpha hash or alpha scissor, + // Although it's technically possible to do alpha antialiasing without using alpha hash or alpha scissor, // it is restricted in the base material because it has no use, and abusing it with regular Alpha blending can - // saturate the MSAA mask + // saturate the MSAA mask. if (transparency == TRANSPARENCY_ALPHA_HASH || transparency == TRANSPARENCY_ALPHA_SCISSOR) { - // alpha antialiasing is only useful in ALPHA_HASH or ALPHA_SCISSOR + // Alpha antialiasing is only useful with ALPHA_HASH or ALPHA_SCISSOR. if (alpha_antialiasing_mode == ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE) { - code += ",alpha_to_coverage"; + code += ", alpha_to_coverage"; } else if (alpha_antialiasing_mode == ALPHA_ANTIALIASING_ALPHA_TO_COVERAGE_AND_TO_ONE) { - code += ",alpha_to_coverage_and_one"; + code += ", alpha_to_coverage_and_one"; } } code += ";\n"; - code += "uniform vec4 albedo : source_color;\n"; - code += "uniform sampler2D texture_albedo : source_color," + texfilter_str + ";\n"; + // Generate list of uniforms. + code += vformat(R"( +uniform vec4 albedo : source_color; +uniform sampler2D texture_albedo : source_color, %s; +)", + texfilter_str); + if (grow_enabled) { - code += "uniform float grow;\n"; + code += "uniform float grow : hint_range(-16.0, 16.0, 0.001);\n"; } if (proximity_fade_enabled) { - code += "uniform float proximity_fade_distance;\n"; + code += "uniform float proximity_fade_distance : hint_range(0.0, 4096.0, 0.01);\n"; } if (distance_fade != DISTANCE_FADE_DISABLED) { - code += "uniform float distance_fade_min;\n"; - code += "uniform float distance_fade_max;\n"; + code += R"( +uniform float distance_fade_min : hint_range(0.0, 4096.0, 0.01); +uniform float distance_fade_max : hint_range(0.0, 4096.0, 0.01); +)"; } if (flags[FLAG_ALBEDO_TEXTURE_MSDF]) { - code += "uniform float msdf_pixel_range;\n"; - code += "uniform float msdf_outline_size;\n"; + code += R"( +uniform float msdf_pixel_range : hint_range(1.0, 100.0, 1.0); +uniform float msdf_outline_size : hint_range(0.0, 250.0, 1.0); +)"; } - // alpha scissor is only valid if there is not antialiasing edge - // alpha hash is valid whenever, but not with alpha scissor + // Alpha scissor is only valid if there is no antialiasing edge. + // Alpha hash is valid whenever, but not with alpha scissor. if (transparency == TRANSPARENCY_ALPHA_SCISSOR) { - code += "uniform float alpha_scissor_threshold;\n"; + code += "uniform float alpha_scissor_threshold : hint_range(0.0, 1.0, 0.001);\n"; } else if (transparency == TRANSPARENCY_ALPHA_HASH) { - code += "uniform float alpha_hash_scale;\n"; + code += "uniform float alpha_hash_scale : hint_range(0.0, 2.0, 0.01);\n"; } - // if alpha antialiasing isn't off, add in the edge variable + // If alpha antialiasing isn't off, add in the edge variable. if (alpha_antialiasing_mode != ALPHA_ANTIALIASING_OFF && (transparency == TRANSPARENCY_ALPHA_SCISSOR || transparency == TRANSPARENCY_ALPHA_HASH)) { - code += "uniform float alpha_antialiasing_edge;\n"; - code += "uniform ivec2 albedo_texture_size;\n"; + code += R"( +uniform float alpha_antialiasing_edge : hint_range(0.0, 1.0, 0.01); +uniform ivec2 albedo_texture_size; +)"; } - code += "uniform float point_size : hint_range(0,128);\n"; + code += "uniform float point_size : hint_range(0.1, 128.0, 0.1);\n"; - //TODO ALL HINTS if (!orm) { - code += "uniform float roughness : hint_range(0,1);\n"; - code += "uniform sampler2D texture_metallic : hint_default_white," + texfilter_str + ";\n"; - code += "uniform vec4 metallic_texture_channel;\n"; + code += vformat(R"( +uniform float roughness : hint_range(0.0, 1.0); +uniform sampler2D texture_metallic : hint_default_white, %s; +uniform vec4 metallic_texture_channel; +)", + texfilter_str); switch (roughness_texture_channel) { case TEXTURE_CHANNEL_RED: { - code += "uniform sampler2D texture_roughness : hint_roughness_r," + texfilter_str + ";\n"; + code += vformat("uniform sampler2D texture_roughness : hint_roughness_r, %s;\n", texfilter_str); } break; case TEXTURE_CHANNEL_GREEN: { - code += "uniform sampler2D texture_roughness : hint_roughness_g," + texfilter_str + ";\n"; + code += vformat("uniform sampler2D texture_roughness : hint_roughness_g, %s;\n", texfilter_str); } break; case TEXTURE_CHANNEL_BLUE: { - code += "uniform sampler2D texture_roughness : hint_roughness_b," + texfilter_str + ";\n"; + code += vformat("uniform sampler2D texture_roughness : hint_roughness_b, %s;\n", texfilter_str); } break; case TEXTURE_CHANNEL_ALPHA: { - code += "uniform sampler2D texture_roughness : hint_roughness_a," + texfilter_str + ";\n"; + code += vformat("uniform sampler2D texture_roughness : hint_roughness_a, %s;\n", texfilter_str); } break; case TEXTURE_CHANNEL_GRAYSCALE: { - code += "uniform sampler2D texture_roughness : hint_roughness_gray," + texfilter_str + ";\n"; + code += vformat("uniform sampler2D texture_roughness : hint_roughness_gray, %s;\n", texfilter_str); } break; case TEXTURE_CHANNEL_MAX: break; // Internal value, skip. } - code += "uniform float specular;\n"; - code += "uniform float metallic;\n"; + code += R"( +uniform float specular : hint_range(0.0, 1.0, 0.01); +uniform float metallic : hint_range(0.0, 1.0, 0.01); +)"; } else { - code += "uniform sampler2D texture_orm : hint_roughness_g," + texfilter_str + ";\n"; + code += "uniform sampler2D texture_orm : hint_roughness_g, " + texfilter_str + ";\n"; } if (billboard_mode == BILLBOARD_PARTICLES) { - code += "uniform int particles_anim_h_frames;\n"; - code += "uniform int particles_anim_v_frames;\n"; - code += "uniform bool particles_anim_loop;\n"; + code += R"( +uniform int particles_anim_h_frames : hint_range(1, 128); +uniform int particles_anim_v_frames : hint_range(1, 128); +uniform bool particles_anim_loop; +)"; } if (features[FEATURE_EMISSION]) { - code += "uniform sampler2D texture_emission : source_color, hint_default_black," + texfilter_str + ";\n"; - code += "uniform vec4 emission : source_color;\n"; - code += "uniform float emission_energy;\n"; + code += vformat(R"( +uniform sampler2D texture_emission : source_color, hint_default_black, %s; +uniform vec4 emission : source_color; +uniform float emission_energy : hint_range(0.0, 100.0, 0.01); +)", + texfilter_str); } if (features[FEATURE_REFRACTION]) { - code += "uniform sampler2D texture_refraction : " + texfilter_str + ";\n"; - code += "uniform float refraction : hint_range(-16,16);\n"; - code += "uniform vec4 refraction_texture_channel;\n"; + code += vformat(R"( +uniform sampler2D texture_refraction : %s; +uniform float refraction : hint_range(-1.0, 1.0, 0.001); +uniform vec4 refraction_texture_channel; +)", + texfilter_str); } if (features[FEATURE_REFRACTION]) { - code += "uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_linear_mipmap;"; + code += "uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_linear_mipmap;\n"; } if (proximity_fade_enabled) { - code += "uniform sampler2D depth_texture : hint_depth_texture, repeat_disable, filter_nearest;"; + code += "uniform sampler2D depth_texture : hint_depth_texture, repeat_disable, filter_nearest;\n"; } if (features[FEATURE_NORMAL_MAPPING]) { - code += "uniform sampler2D texture_normal : hint_roughness_normal," + texfilter_str + ";\n"; - code += "uniform float normal_scale : hint_range(-16,16);\n"; + code += vformat(R"( +uniform sampler2D texture_normal : hint_roughness_normal, %s; +uniform float normal_scale : hint_range(-16.0, 16.0); +)", + texfilter_str); } if (features[FEATURE_RIM]) { - code += "uniform float rim : hint_range(0,1);\n"; - code += "uniform float rim_tint : hint_range(0,1);\n"; - code += "uniform sampler2D texture_rim : hint_default_white," + texfilter_str + ";\n"; + code += vformat(R"( +uniform float rim : hint_range(0.0, 1.0, 0.01); +uniform float rim_tint : hint_range(0.0, 1.0, 0.01); +uniform sampler2D texture_rim : hint_default_white, %s; +)", + texfilter_str); } if (features[FEATURE_CLEARCOAT]) { - code += "uniform float clearcoat : hint_range(0,1);\n"; - code += "uniform float clearcoat_roughness : hint_range(0,1);\n"; - code += "uniform sampler2D texture_clearcoat : hint_default_white," + texfilter_str + ";\n"; + code += vformat(R"( +uniform float clearcoat : hint_range(0.0, 1.0, 0.01); +uniform float clearcoat_roughness : hint_range(0.0, 1.0, 0.01); +uniform sampler2D texture_clearcoat : hint_default_white, %s; +)", + texfilter_str); } if (features[FEATURE_ANISOTROPY]) { - code += "uniform float anisotropy_ratio : hint_range(0,256);\n"; - code += "uniform sampler2D texture_flowmap : hint_anisotropy," + texfilter_str + ";\n"; + code += vformat(R"( +uniform float anisotropy_ratio : hint_range(0.0, 1.0, 0.01); +uniform sampler2D texture_flowmap : hint_anisotropy, %s; +)", + texfilter_str); } if (features[FEATURE_AMBIENT_OCCLUSION]) { - code += "uniform sampler2D texture_ambient_occlusion : hint_default_white, " + texfilter_str + ";\n"; - code += "uniform vec4 ao_texture_channel;\n"; - code += "uniform float ao_light_affect;\n"; + code += vformat(R"( +uniform sampler2D texture_ambient_occlusion : hint_default_white, %s; +uniform vec4 ao_texture_channel; +uniform float ao_light_affect : hint_range(0.0, 1.0, 0.01); +)", + texfilter_str); } if (features[FEATURE_DETAIL]) { - code += "uniform sampler2D texture_detail_albedo : source_color," + texfilter_str + ";\n"; - code += "uniform sampler2D texture_detail_normal : hint_normal," + texfilter_str + ";\n"; - code += "uniform sampler2D texture_detail_mask : hint_default_white," + texfilter_str + ";\n"; + code += vformat(R"( +uniform sampler2D texture_detail_albedo : source_color, %s; +uniform sampler2D texture_detail_normal : hint_normal, %s; +uniform sampler2D texture_detail_mask : hint_default_white, %s; +)", + texfilter_str, texfilter_str, texfilter_str); } if (features[FEATURE_SUBSURFACE_SCATTERING]) { - code += "uniform float subsurface_scattering_strength : hint_range(0,1);\n"; - code += "uniform sampler2D texture_subsurface_scattering : hint_default_white," + texfilter_str + ";\n"; + code += vformat(R"( +uniform float subsurface_scattering_strength : hint_range(0.0, 1.0, 0.01); +uniform sampler2D texture_subsurface_scattering : hint_default_white, %s; +)", + texfilter_str); } if (features[FEATURE_SUBSURFACE_TRANSMITTANCE]) { - code += "uniform vec4 transmittance_color : source_color;\n"; - code += "uniform float transmittance_depth;\n"; - code += "uniform sampler2D texture_subsurface_transmittance : hint_default_white," + texfilter_str + ";\n"; - code += "uniform float transmittance_boost;\n"; + code += vformat(R"( +uniform vec4 transmittance_color : source_color; +uniform float transmittance_depth : hint_range(0.001, 8.0, 0.001); +uniform sampler2D texture_subsurface_transmittance : hint_default_white, %s; +uniform float transmittance_boost : hint_range(0.0, 1.0, 0.01); +)", + texfilter_str); } if (features[FEATURE_BACKLIGHT]) { - code += "uniform vec4 backlight : source_color;\n"; - code += "uniform sampler2D texture_backlight : hint_default_black," + texfilter_str + ";\n"; + code += vformat(R"( +uniform vec4 backlight : source_color; +uniform sampler2D texture_backlight : hint_default_black, %s; +)", + texfilter_str); } if (features[FEATURE_HEIGHT_MAPPING]) { - code += "uniform sampler2D texture_heightmap : hint_default_black," + texfilter_height_str + ";\n"; - code += "uniform float heightmap_scale;\n"; - code += "uniform int heightmap_min_layers;\n"; - code += "uniform int heightmap_max_layers;\n"; - code += "uniform vec2 heightmap_flip;\n"; + code += vformat(R"( +uniform sampler2D texture_heightmap : hint_default_black, %s; +uniform float heightmap_scale : hint_range(-16.0, 16.0, 0.001); +uniform int heightmap_min_layers : hint_range(1, 64); +uniform int heightmap_max_layers : hint_range(1, 64); +uniform vec2 heightmap_flip; +)", + texfilter_height_str); } if (flags[FLAG_UV1_USE_TRIPLANAR]) { code += "varying vec3 uv1_triplanar_pos;\n"; @@ -954,132 +1006,216 @@ void BaseMaterial3D::_update_shader() { code += "varying vec3 uv2_triplanar_pos;\n"; } if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += "uniform float uv1_blend_sharpness;\n"; - code += "varying vec3 uv1_power_normal;\n"; + code += R"( +uniform float uv1_blend_sharpness : hint_range(0.0, 150.0, 0.001); +varying vec3 uv1_power_normal; +)"; } if (flags[FLAG_UV2_USE_TRIPLANAR]) { - code += "uniform float uv2_blend_sharpness;\n"; - code += "varying vec3 uv2_power_normal;\n"; + code += R"(uniform float uv2_blend_sharpness : hint_range(0.0, 150.0, 0.001); +varying vec3 uv2_power_normal; +)"; } - code += "uniform vec3 uv1_scale;\n"; - code += "uniform vec3 uv1_offset;\n"; - code += "uniform vec3 uv2_scale;\n"; - code += "uniform vec3 uv2_offset;\n"; - - code += "\n\n"; + code += R"( +uniform vec3 uv1_scale; +uniform vec3 uv1_offset; +uniform vec3 uv2_scale; +uniform vec3 uv2_offset; +)"; - code += "void vertex() {\n"; + // Generate vertex shader. + code += R"( +void vertex() {)"; if (flags[FLAG_SRGB_VERTEX_COLOR]) { - code += " if (!OUTPUT_IS_SRGB) {\n"; - code += " COLOR.rgb = mix(pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb * (1.0 / 12.92), lessThan(COLOR.rgb, vec3(0.04045)));\n"; - code += " }\n"; + code += R"( + // Vertex Color is sRGB: Enabled + if (!OUTPUT_IS_SRGB) { + COLOR.rgb = mix( + pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), + COLOR.rgb * (1.0 / 12.92), + lessThan(COLOR.rgb, vec3(0.04045))); + } +)"; } if (flags[FLAG_USE_POINT_SIZE]) { - code += " POINT_SIZE=point_size;\n"; + code += R"( + // Use Point Size: Enabled + POINT_SIZE = point_size; +)"; } if (shading_mode == SHADING_MODE_PER_VERTEX) { - code += " ROUGHNESS=roughness;\n"; + code += R"( + // Shading Mode: Per Vertex + ROUGHNESS = roughness; +)"; } if (!flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " UV=UV*uv1_scale.xy+uv1_offset.xy;\n"; + code += R"( + UV = UV * uv1_scale.xy + uv1_offset.xy; +)"; + } + + if (detail_uv == DETAIL_UV_2 && !flags[FLAG_UV2_USE_TRIPLANAR]) { + // Don't add a newline if the UV assignment above is already performed, + // so that UV1 and UV2 are closer to each other. + if (flags[FLAG_UV1_USE_TRIPLANAR]) { + code += "\n"; + } + code += R"( // Detail UV Layer: UV2 + UV2 = UV2 * uv2_scale.xy + uv2_offset.xy; +)"; } switch (billboard_mode) { case BILLBOARD_DISABLED: { } break; case BILLBOARD_ENABLED: { - // MAIN_CAM_INV_VIEW_MATRIX is inverse of the camera, even on shadow passes: this ensures the billboard faces the camera when casting shadows. - code += " MODELVIEW_MATRIX = VIEW_MATRIX * mat4(MAIN_CAM_INV_VIEW_MATRIX[0], MAIN_CAM_INV_VIEW_MATRIX[1], MAIN_CAM_INV_VIEW_MATRIX[2], MODEL_MATRIX[3]);\n"; - + // `MAIN_CAM_INV_VIEW_MATRIX` is inverse of the camera, even on shadow passes. + // This ensures the billboard faces the camera when casting shadows. + code += R"( + // Billboard Mode: Enabled + MODELVIEW_MATRIX = VIEW_MATRIX * mat4( + MAIN_CAM_INV_VIEW_MATRIX[0], + MAIN_CAM_INV_VIEW_MATRIX[1], + MAIN_CAM_INV_VIEW_MATRIX[2], + MODEL_MATRIX[3]); +)"; if (flags[FLAG_BILLBOARD_KEEP_SCALE]) { - code += " MODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4(vec4(length(MODEL_MATRIX[0].xyz), 0.0, 0.0, 0.0), vec4(0.0, length(MODEL_MATRIX[1].xyz), 0.0, 0.0), vec4(0.0, 0.0, length(MODEL_MATRIX[2].xyz), 0.0), vec4(0.0, 0.0, 0.0, 1.0));\n"; + code += R"( + // Billboard Keep Scale: Enabled + MODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4( + vec4(length(MODEL_MATRIX[0].xyz), 0.0, 0.0, 0.0), + vec4(0.0, length(MODEL_MATRIX[1].xyz), 0.0, 0.0), + vec4(0.0, 0.0, length(MODEL_MATRIX[2].xyz), 0.0), + vec4(0.0, 0.0, 0.0, 1.0)); +)"; } code += " MODELVIEW_NORMAL_MATRIX = mat3(MODELVIEW_MATRIX);\n"; } break; case BILLBOARD_FIXED_Y: { - // MAIN_CAM_INV_VIEW_MATRIX is inverse of the camera, even on shadow passes: this ensures the billboard faces the camera when casting shadows. - code += " MODELVIEW_MATRIX = VIEW_MATRIX * mat4(vec4(normalize(cross(vec3(0.0, 1.0, 0.0), MAIN_CAM_INV_VIEW_MATRIX[2].xyz)), 0.0), vec4(0.0, 1.0, 0.0, 0.0), vec4(normalize(cross(MAIN_CAM_INV_VIEW_MATRIX[0].xyz, vec3(0.0, 1.0, 0.0))), 0.0), MODEL_MATRIX[3]);\n"; - + // `MAIN_CAM_INV_VIEW_MATRIX` is inverse of the camera, even on shadow passes. + // This ensures the billboard faces the camera when casting shadows. + code += R"( + // Billboard Mode: Y-Billboard + MODELVIEW_MATRIX = VIEW_MATRIX * mat4( + vec4(normalize(cross(vec3(0.0, 1.0, 0.0), MAIN_CAM_INV_VIEW_MATRIX[2].xyz)), 0.0), + vec4(0.0, 1.0, 0.0, 0.0), + vec4(normalize(cross(MAIN_CAM_INV_VIEW_MATRIX[0].xyz, vec3(0.0, 1.0, 0.0))), 0.0), + MODEL_MATRIX[3]); +)"; if (flags[FLAG_BILLBOARD_KEEP_SCALE]) { - code += " MODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4(vec4(length(MODEL_MATRIX[0].xyz), 0.0, 0.0, 0.0), vec4(0.0, length(MODEL_MATRIX[1].xyz), 0.0, 0.0), vec4(0.0, 0.0, length(MODEL_MATRIX[2].xyz), 0.0), vec4(0.0, 0.0, 0.0, 1.0));\n"; + code += R"( + // Billboard Keep Scale: Enabled + MODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4( + vec4(length(MODEL_MATRIX[0].xyz), 0.0, 0.0, 0.0), + vec4(0.0, length(MODEL_MATRIX[1].xyz), 0.0, 0.0), + vec4(0.0, 0.0, length(MODEL_MATRIX[2].xyz), 0.0), + vec4(0.0, 0.0, 0.0, 1.0)); +)"; } code += " MODELVIEW_NORMAL_MATRIX = mat3(MODELVIEW_MATRIX);\n"; } break; case BILLBOARD_PARTICLES: { - //make billboard - code += " mat4 mat_world = mat4(normalize(INV_VIEW_MATRIX[0]), normalize(INV_VIEW_MATRIX[1]) ,normalize(INV_VIEW_MATRIX[2]), MODEL_MATRIX[3]);\n"; - //rotate by rotation - code += " mat_world = mat_world * mat4(vec4(cos(INSTANCE_CUSTOM.x), -sin(INSTANCE_CUSTOM.x), 0.0, 0.0), vec4(sin(INSTANCE_CUSTOM.x), cos(INSTANCE_CUSTOM.x), 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));\n"; - //set modelview + // Make billboard and rotated by rotation. + code += R"( + // Billboard Mode: Particles + mat4 mat_world = mat4( + normalize(INV_VIEW_MATRIX[0]), + normalize(INV_VIEW_MATRIX[1]), + normalize(INV_VIEW_MATRIX[2]), + MODEL_MATRIX[3]); + mat_world = mat_world * mat4( + vec4(cos(INSTANCE_CUSTOM.x), -sin(INSTANCE_CUSTOM.x), 0.0, 0.0), + vec4(sin(INSTANCE_CUSTOM.x), cos(INSTANCE_CUSTOM.x), 0.0, 0.0), + vec4(0.0, 0.0, 1.0, 0.0), + vec4(0.0, 0.0, 0.0, 1.0)); +)"; + // Set modelview. code += " MODELVIEW_MATRIX = VIEW_MATRIX * mat_world;\n"; if (flags[FLAG_BILLBOARD_KEEP_SCALE]) { - code += " MODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4(vec4(length(MODEL_MATRIX[0].xyz), 0.0, 0.0, 0.0),vec4(0.0, length(MODEL_MATRIX[1].xyz), 0.0, 0.0), vec4(0.0, 0.0, length(MODEL_MATRIX[2].xyz), 0.0), vec4(0.0, 0.0, 0.0, 1.0));\n"; + code += R"( + // Billboard Keep Scale: Enabled + MODELVIEW_MATRIX = MODELVIEW_MATRIX * mat4( + vec4(length(MODEL_MATRIX[0].xyz), 0.0, 0.0, 0.0), + vec4(0.0, length(MODEL_MATRIX[1].xyz), 0.0, 0.0), + vec4(0.0, 0.0, length(MODEL_MATRIX[2].xyz), 0.0), + vec4(0.0, 0.0, 0.0, 1.0)); +)"; } - //set modelview normal - code += " MODELVIEW_NORMAL_MATRIX = mat3(MODELVIEW_MATRIX);\n"; - - //handle animation - code += " float h_frames = float(particles_anim_h_frames);\n"; - code += " float v_frames = float(particles_anim_v_frames);\n"; - code += " float particle_total_frames = float(particles_anim_h_frames * particles_anim_v_frames);\n"; - code += " float particle_frame = floor(INSTANCE_CUSTOM.z * float(particle_total_frames));\n"; - code += " if (!particles_anim_loop) {\n"; - code += " particle_frame = clamp(particle_frame, 0.0, particle_total_frames - 1.0);\n"; - code += " } else {\n"; - code += " particle_frame = mod(particle_frame, particle_total_frames);\n"; - code += " }\n"; - code += " UV /= vec2(h_frames, v_frames);\n"; - code += " UV += vec2(mod(particle_frame, h_frames) / h_frames, floor((particle_frame + 0.5) / h_frames) / v_frames);\n"; + // Set modelview normal and handle animation. + code += R"( + MODELVIEW_NORMAL_MATRIX = mat3(MODELVIEW_MATRIX); + + float h_frames = float(particles_anim_h_frames); + float v_frames = float(particles_anim_v_frames); + float particle_total_frames = float(particles_anim_h_frames * particles_anim_v_frames); + float particle_frame = floor(INSTANCE_CUSTOM.z * float(particle_total_frames)); + if (!particles_anim_loop) { + particle_frame = clamp(particle_frame, 0.0, particle_total_frames - 1.0); + } else { + particle_frame = mod(particle_frame, particle_total_frames); + } + UV /= vec2(h_frames, v_frames); + UV += vec2(mod(particle_frame, h_frames) / h_frames, floor((particle_frame + 0.5) / h_frames) / v_frames); +)"; } break; case BILLBOARD_MAX: break; // Internal value, skip. } if (flags[FLAG_FIXED_SIZE]) { - code += " if (PROJECTION_MATRIX[3][3] != 0.0) {\n"; - //orthogonal matrix, try to do about the same - //with viewport size - code += " float h = abs(1.0 / (2.0 * PROJECTION_MATRIX[1][1]));\n"; - code += " float sc = (h * 2.0); //consistent with Y-fov\n"; - code += " MODELVIEW_MATRIX[0]*=sc;\n"; - code += " MODELVIEW_MATRIX[1]*=sc;\n"; - code += " MODELVIEW_MATRIX[2]*=sc;\n"; - code += " } else {\n"; - //just scale by depth - code += " float sc = -(MODELVIEW_MATRIX)[3].z;\n"; - code += " MODELVIEW_MATRIX[0]*=sc;\n"; - code += " MODELVIEW_MATRIX[1]*=sc;\n"; - code += " MODELVIEW_MATRIX[2]*=sc;\n"; - code += " }\n"; + code += R"( + // Fixed Size: Enabled + if (PROJECTION_MATRIX[3][3] != 0.0) { + // Orthogonal matrix; try to do about the same with viewport size. + float h = abs(1.0 / (2.0 * PROJECTION_MATRIX[1][1])); + // Consistent with vertical FOV (Keep Height). + float sc = (h * 2.0); + MODELVIEW_MATRIX[0] *= sc; + MODELVIEW_MATRIX[1] *= sc; + MODELVIEW_MATRIX[2] *= sc; + } else { + // Scale by depth. + float sc = -(MODELVIEW_MATRIX)[3].z; + MODELVIEW_MATRIX[0] *= sc; + MODELVIEW_MATRIX[1] *= sc; + MODELVIEW_MATRIX[2] *= sc; } - - if (detail_uv == DETAIL_UV_2 && !flags[FLAG_UV2_USE_TRIPLANAR]) { - code += " UV2=UV2*uv2_scale.xy+uv2_offset.xy;\n"; +)"; } + if (flags[FLAG_UV1_USE_TRIPLANAR] || flags[FLAG_UV2_USE_TRIPLANAR]) { - //generate tangent and binormal in world space + // Generate tangent and binormal in world space. if (flags[FLAG_UV1_USE_WORLD_TRIPLANAR]) { - code += " vec3 normal = MODEL_NORMAL_MATRIX * NORMAL;\n"; + code += R"( + vec3 normal = MODEL_NORMAL_MATRIX * NORMAL; +)"; } else { - code += " vec3 normal = NORMAL;\n"; - } - code += " TANGENT = vec3(0.0,0.0,-1.0) * abs(normal.x);\n"; - code += " TANGENT+= vec3(1.0,0.0,0.0) * abs(normal.y);\n"; - code += " TANGENT+= vec3(1.0,0.0,0.0) * abs(normal.z);\n"; + code += R"( + vec3 normal = NORMAL; +)"; + } + code += R"( + TANGENT = vec3(0.0, 0.0, -1.0) * abs(normal.x); + TANGENT += vec3(1.0, 0.0, 0.0) * abs(normal.y); + TANGENT += vec3(1.0, 0.0, 0.0) * abs(normal.z); +)"; if (flags[FLAG_UV1_USE_WORLD_TRIPLANAR]) { code += " TANGENT = inverse(MODEL_NORMAL_MATRIX) * normalize(TANGENT);\n"; } else { code += " TANGENT = normalize(TANGENT);\n"; } - code += " BINORMAL = vec3(0.0,1.0,0.0) * abs(normal.x);\n"; - code += " BINORMAL+= vec3(0.0,0.0,-1.0) * abs(normal.y);\n"; - code += " BINORMAL+= vec3(0.0,1.0,0.0) * abs(normal.z);\n"; + code += R"( + BINORMAL = vec3(0.0, 1.0, 0.0) * abs(normal.x); + BINORMAL += vec3(0.0, 0.0, -1.0) * abs(normal.y); + BINORMAL += vec3(0.0, 1.0, 0.0) * abs(normal.z); +)"; if (flags[FLAG_UV1_USE_WORLD_TRIPLANAR]) { code += " BINORMAL = inverse(MODEL_NORMAL_MATRIX) * normalize(BINORMAL);\n"; } else { @@ -1089,58 +1225,89 @@ void BaseMaterial3D::_update_shader() { if (flags[FLAG_UV1_USE_TRIPLANAR]) { if (flags[FLAG_UV1_USE_WORLD_TRIPLANAR]) { - code += " uv1_power_normal=pow(abs(normal),vec3(uv1_blend_sharpness));\n"; - code += " uv1_triplanar_pos = (MODEL_MATRIX * vec4(VERTEX, 1.0f)).xyz * uv1_scale + uv1_offset;\n"; + code += R"( + // UV1 Triplanar: Enabled (with World Triplanar) + uv1_power_normal = pow(abs(normal), vec3(uv1_blend_sharpness)); + uv1_triplanar_pos = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz * uv1_scale + uv1_offset; +)"; } else { - code += " uv1_power_normal=pow(abs(NORMAL),vec3(uv1_blend_sharpness));\n"; - code += " uv1_triplanar_pos = VERTEX * uv1_scale + uv1_offset;\n"; + code += R"( + // UV1 Triplanar: Enabled + uv1_power_normal = pow(abs(NORMAL), vec3(uv1_blend_sharpness)); + uv1_triplanar_pos = VERTEX * uv1_scale + uv1_offset; +)"; } - code += " uv1_power_normal/=dot(uv1_power_normal,vec3(1.0));\n"; - code += " uv1_triplanar_pos *= vec3(1.0,-1.0, 1.0);\n"; + code += R"( uv1_power_normal /= dot(uv1_power_normal, vec3(1.0)); + uv1_triplanar_pos *= vec3(1.0, -1.0, 1.0); +)"; } if (flags[FLAG_UV2_USE_TRIPLANAR]) { if (flags[FLAG_UV2_USE_WORLD_TRIPLANAR]) { - code += " uv2_power_normal=pow(abs(mat3(MODEL_MATRIX) * NORMAL), vec3(uv2_blend_sharpness));\n"; - code += " uv2_triplanar_pos = (MODEL_MATRIX * vec4(VERTEX, 1.0f)).xyz * uv2_scale + uv2_offset;\n"; + code += R"( + // UV2 Triplanar: Enabled (with World Triplanar) + uv2_power_normal = pow(abs(mat3(MODEL_MATRIX) * NORMAL), vec3(uv2_blend_sharpness)); + uv2_triplanar_pos = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz * uv2_scale + uv2_offset; +)"; } else { - code += " uv2_power_normal=pow(abs(NORMAL), vec3(uv2_blend_sharpness));\n"; - code += " uv2_triplanar_pos = VERTEX * uv2_scale + uv2_offset;\n"; + code += R"( + // UV2 Triplanar: Enabled + uv2_power_normal = pow(abs(NORMAL), vec3(uv2_blend_sharpness)); + uv2_triplanar_pos = VERTEX * uv2_scale + uv2_offset; +)"; } - code += " uv2_power_normal/=dot(uv2_power_normal,vec3(1.0));\n"; - code += " uv2_triplanar_pos *= vec3(1.0,-1.0, 1.0);\n"; + code += R"( uv2_power_normal /= dot(uv2_power_normal, vec3(1.0)); + uv2_triplanar_pos *= vec3(1.0, -1.0, 1.0); +)"; } if (grow_enabled) { - code += " VERTEX+=NORMAL*grow;\n"; + code += R"( + // Grow: Enabled + VERTEX += NORMAL * grow; +)"; } code += "}\n"; - code += "\n\n"; + if (flags[FLAG_ALBEDO_TEXTURE_MSDF]) { - code += "float msdf_median(float r, float g, float b, float a) {\n"; - code += " return min(max(min(r, g), min(max(r, g), b)), a);\n"; - code += "}\n"; + code += R"( +float msdf_median(float r, float g, float b, float a) { + return min(max(min(r, g), min(max(r, g), b)), a); +} +)"; } - code += "\n\n"; + if (flags[FLAG_UV1_USE_TRIPLANAR] || flags[FLAG_UV2_USE_TRIPLANAR]) { - code += "vec4 triplanar_texture(sampler2D p_sampler,vec3 p_weights,vec3 p_triplanar_pos) {\n"; - code += " vec4 samp=vec4(0.0);\n"; - code += " samp+= texture(p_sampler,p_triplanar_pos.xy) * p_weights.z;\n"; - code += " samp+= texture(p_sampler,p_triplanar_pos.xz) * p_weights.y;\n"; - code += " samp+= texture(p_sampler,p_triplanar_pos.zy * vec2(-1.0,1.0)) * p_weights.x;\n"; - code += " return samp;\n"; - code += "}\n"; + code += R"( +vec4 triplanar_texture(sampler2D p_sampler, vec3 p_weights, vec3 p_triplanar_pos) { + vec4 samp = vec4(0.0); + samp += texture(p_sampler, p_triplanar_pos.xy) * p_weights.z; + samp += texture(p_sampler, p_triplanar_pos.xz) * p_weights.y; + samp += texture(p_sampler, p_triplanar_pos.zy * vec2(-1.0, 1.0)) * p_weights.x; + return samp; +} +)"; } - code += "\n\n"; - code += "void fragment() {\n"; + + // Generate fragment shader. + code += R"( +void fragment() {)"; if (!flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " vec2 base_uv = UV;\n"; + code += R"( + vec2 base_uv = UV; +)"; } if ((features[FEATURE_DETAIL] && detail_uv == DETAIL_UV_2) || (features[FEATURE_AMBIENT_OCCLUSION] && flags[FLAG_AO_ON_UV2]) || (features[FEATURE_EMISSION] && flags[FLAG_EMISSION_ON_UV2])) { - code += " vec2 base_uv2 = UV2;\n"; + // Don't add a newline if the UV assignment above is already performed, + // so that UV1 and UV2 are closer to each other. + if (flags[FLAG_UV1_USE_TRIPLANAR]) { + code += "\n"; + } + code += R"( vec2 base_uv2 = UV2; +)"; } if (features[FEATURE_HEIGHT_MAPPING] && flags[FLAG_UV1_USE_TRIPLANAR]) { @@ -1157,42 +1324,57 @@ void BaseMaterial3D::_update_shader() { } } - if (!RenderingServer::get_singleton()->is_low_end() && features[FEATURE_HEIGHT_MAPPING] && !flags[FLAG_UV1_USE_TRIPLANAR]) { //heightmap not supported with triplanar - code += " {\n"; - code += " vec3 view_dir = normalize(normalize(-VERTEX + EYE_OFFSET) * mat3(TANGENT * heightmap_flip.x, -BINORMAL * heightmap_flip.y, NORMAL));\n"; // binormal is negative due to mikktspace, flip 'unflips' it ;-) + // Heightmapping isn't supported at the same time as triplanar mapping. + if (!RenderingServer::get_singleton()->is_low_end() && features[FEATURE_HEIGHT_MAPPING] && !flags[FLAG_UV1_USE_TRIPLANAR]) { + // Binormal is negative due to mikktspace. Flipping it "unflips" it. + code += R"( + { + // Height: Enabled + vec3 view_dir = normalize(normalize(-VERTEX + EYE_OFFSET) * mat3(TANGENT * heightmap_flip.x, -BINORMAL * heightmap_flip.y, NORMAL)); +)"; if (deep_parallax) { - code += " float num_layers = mix(float(heightmap_max_layers),float(heightmap_min_layers), abs(dot(vec3(0.0, 0.0, 1.0), view_dir)));\n"; - code += " float layer_depth = 1.0 / num_layers;\n"; - code += " float current_layer_depth = 0.0;\n"; // Multiply the heightmap scale by 0.01 to improve heightmap scale usability. - code += " vec2 P = view_dir.xy * heightmap_scale * 0.01;\n"; - code += " vec2 delta = P / num_layers;\n"; - code += " vec2 ofs = base_uv;\n"; + code += R"( + // Height Deep Parallax: Enabled + float num_layers = mix(float(heightmap_max_layers), float(heightmap_min_layers), abs(dot(vec3(0.0, 0.0, 1.0), view_dir))); + float layer_depth = 1.0 / num_layers; + float current_layer_depth = 0.0; + vec2 p = view_dir.xy * heightmap_scale * 0.01; + vec2 delta = p / num_layers; + vec2 ofs = base_uv; +)"; if (flags[FLAG_INVERT_HEIGHTMAP]) { code += " float depth = texture(texture_heightmap, ofs).r;\n"; } else { code += " float depth = 1.0 - texture(texture_heightmap, ofs).r;\n"; } - code += " float current_depth = 0.0;\n"; - code += " while(current_depth < depth) {\n"; - code += " ofs -= delta;\n"; + code += R"( + float current_depth = 0.0; + while (current_depth < depth) { + ofs -= delta; +)"; if (flags[FLAG_INVERT_HEIGHTMAP]) { code += " depth = texture(texture_heightmap, ofs).r;\n"; } else { code += " depth = 1.0 - texture(texture_heightmap, ofs).r;\n"; } - code += " current_depth += layer_depth;\n"; - code += " }\n"; - code += " vec2 prev_ofs = ofs + delta;\n"; - code += " float after_depth = depth - current_depth;\n"; + code += R"( + current_depth += layer_depth; + } + + vec2 prev_ofs = ofs + delta; + float after_depth = depth - current_depth; +)"; if (flags[FLAG_INVERT_HEIGHTMAP]) { code += " float before_depth = texture(texture_heightmap, prev_ofs).r - current_depth + layer_depth;\n"; } else { - code += " float before_depth = ( 1.0 - texture(texture_heightmap, prev_ofs).r ) - current_depth + layer_depth;\n"; + code += " float before_depth = (1.0 - texture(texture_heightmap, prev_ofs).r) - current_depth + layer_depth;\n"; } - code += " float weight = after_depth / (after_depth - before_depth);\n"; - code += " ofs = mix(ofs,prev_ofs,weight);\n"; + code += R"( + float weight = after_depth / (after_depth - before_depth); + ofs = mix(ofs, prev_ofs, weight); +)"; } else { if (flags[FLAG_INVERT_HEIGHTMAP]) { @@ -1206,28 +1388,41 @@ void BaseMaterial3D::_update_shader() { code += " vec2 ofs = base_uv - view_dir.xy * depth * heightmap_scale * 0.01;\n"; } - code += " base_uv=ofs;\n"; + code += " base_uv = ofs;\n"; if (features[FEATURE_DETAIL] && detail_uv == DETAIL_UV_2) { - code += " base_uv2-=ofs;\n"; + code += " base_uv2 -= ofs;\n"; } code += " }\n"; } if (flags[FLAG_USE_POINT_SIZE]) { - code += " vec4 albedo_tex = texture(texture_albedo,POINT_COORD);\n"; + code += R"( + // Use Point Size: Enabled + vec4 albedo_tex = texture(texture_albedo, POINT_COORD); +)"; } else { if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " vec4 albedo_tex = triplanar_texture(texture_albedo,uv1_power_normal,uv1_triplanar_pos);\n"; + code += R"( + vec4 albedo_tex = triplanar_texture(texture_albedo, uv1_power_normal, uv1_triplanar_pos); +)"; } else { - code += " vec4 albedo_tex = texture(texture_albedo,base_uv);\n"; + code += R"( + vec4 albedo_tex = texture(texture_albedo, base_uv); +)"; } } if (flags[FLAG_ALBEDO_TEXTURE_MSDF]) { - code += " {\n"; - code += " albedo_tex.rgb = mix(vec3(1.0 + 0.055) * pow(albedo_tex.rgb, vec3(1.0 / 2.4)) - vec3(0.055), vec3(12.92) * albedo_tex.rgb.rgb, lessThan(albedo_tex.rgb, vec3(0.0031308)));\n"; - code += " vec2 msdf_size = vec2(msdf_pixel_range) / vec2(textureSize(texture_albedo, 0));\n"; + code += R"( + { + // Albedo Texture MSDF: Enabled + albedo_tex.rgb = mix( + vec3(1.0 + 0.055) * pow(albedo_tex.rgb, vec3(1.0 / 2.4)) - vec3(0.055), + vec3(12.92) * albedo_tex.rgb, + lessThan(albedo_tex.rgb, vec3(0.0031308))); + vec2 msdf_size = vec2(msdf_pixel_range) / vec2(textureSize(texture_albedo, 0)); +)"; if (flags[FLAG_USE_POINT_SIZE]) { code += " vec2 dest_size = vec2(1.0) / fwidth(POINT_COORD);\n"; } else { @@ -1237,120 +1432,175 @@ void BaseMaterial3D::_update_shader() { code += " vec2 dest_size = vec2(1.0) / fwidth(base_uv);\n"; } } - code += " float px_size = max(0.5 * dot(msdf_size, dest_size), 1.0);\n"; - code += " float d = msdf_median(albedo_tex.r, albedo_tex.g, albedo_tex.b, albedo_tex.a) - 0.5;\n"; - code += " if (msdf_outline_size > 0.0) {\n"; - code += " float cr = clamp(msdf_outline_size, 0.0, msdf_pixel_range / 2.0) / msdf_pixel_range;\n"; - code += " albedo_tex.a = clamp((d + cr) * px_size, 0.0, 1.0);\n"; - code += " } else {\n"; - code += " albedo_tex.a = clamp(d * px_size + 0.5, 0.0, 1.0);\n"; - code += " }\n"; - code += " albedo_tex.rgb = vec3(1.0);\n"; - code += " }\n"; + code += R"( + float px_size = max(0.5 * dot(msdf_size, dest_size), 1.0); + float d = msdf_median(albedo_tex.r, albedo_tex.g, albedo_tex.b, albedo_tex.a) - 0.5; + if (msdf_outline_size > 0.0) { + float cr = clamp(msdf_outline_size, 0.0, msdf_pixel_range / 2.0) / msdf_pixel_range; + albedo_tex.a = clamp((d + cr) * px_size, 0.0, 1.0); + } else { + albedo_tex.a = clamp(d * px_size + 0.5, 0.0, 1.0); + } + albedo_tex.rgb = vec3(1.0); + } +)"; } else if (flags[FLAG_ALBEDO_TEXTURE_FORCE_SRGB]) { - code += " albedo_tex.rgb = mix(pow((albedo_tex.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)),vec3(2.4)),albedo_tex.rgb.rgb * (1.0 / 12.92),lessThan(albedo_tex.rgb,vec3(0.04045)));\n"; + code += R"( + // Albedo Texture Force sRGB: Enabled + albedo_tex.rgb = mix( + pow((albedo_tex.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), + albedo_tex.rgb.rgb * (1.0 / 12.92), + lessThan(albedo_tex.rgb, vec3(0.04045))); +)"; } if (flags[FLAG_ALBEDO_FROM_VERTEX_COLOR]) { - code += " albedo_tex *= COLOR;\n"; + code += R"( + // Vertex Color Use as Albedo: Enabled + albedo_tex *= COLOR; + +)"; } code += " ALBEDO = albedo.rgb * albedo_tex.rgb;\n"; if (!orm) { if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " float metallic_tex = dot(triplanar_texture(texture_metallic,uv1_power_normal,uv1_triplanar_pos),metallic_texture_channel);\n"; + code += R"( + float metallic_tex = dot(triplanar_texture(texture_metallic, uv1_power_normal, uv1_triplanar_pos), metallic_texture_channel); +)"; } else { - code += " float metallic_tex = dot(texture(texture_metallic,base_uv),metallic_texture_channel);\n"; + code += R"( + float metallic_tex = dot(texture(texture_metallic, base_uv), metallic_texture_channel); +)"; } - code += " METALLIC = metallic_tex * metallic;\n"; + code += R"( METALLIC = metallic_tex * metallic; + SPECULAR = specular; +)"; switch (roughness_texture_channel) { case TEXTURE_CHANNEL_RED: { - code += " vec4 roughness_texture_channel = vec4(1.0,0.0,0.0,0.0);\n"; + code += R"( + vec4 roughness_texture_channel = vec4(1.0, 0.0, 0.0, 0.0); +)"; } break; case TEXTURE_CHANNEL_GREEN: { - code += " vec4 roughness_texture_channel = vec4(0.0,1.0,0.0,0.0);\n"; + code += R"( + vec4 roughness_texture_channel = vec4(0.0, 1.0, 0.0, 0.0); +)"; } break; case TEXTURE_CHANNEL_BLUE: { - code += " vec4 roughness_texture_channel = vec4(0.0,0.0,1.0,0.0);\n"; + code += R"( + vec4 roughness_texture_channel = vec4(0.0, 0.0, 1.0, 0.0); +)"; } break; case TEXTURE_CHANNEL_ALPHA: { - code += " vec4 roughness_texture_channel = vec4(0.0,0.0,0.0,1.0);\n"; + code += R"( + vec4 roughness_texture_channel = vec4(0.0, 0.0, 0.0, 1.0); +)"; } break; case TEXTURE_CHANNEL_GRAYSCALE: { - code += " vec4 roughness_texture_channel = vec4(0.333333,0.333333,0.333333,0.0);\n"; + code += R"( + vec4 roughness_texture_channel = vec4(0.333333, 0.333333, 0.333333, 0.0); +)"; } break; case TEXTURE_CHANNEL_MAX: break; // Internal value, skip. } if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " float roughness_tex = dot(triplanar_texture(texture_roughness,uv1_power_normal,uv1_triplanar_pos),roughness_texture_channel);\n"; + code += " float roughness_tex = dot(triplanar_texture(texture_roughness, uv1_power_normal, uv1_triplanar_pos), roughness_texture_channel);\n"; } else { - code += " float roughness_tex = dot(texture(texture_roughness,base_uv),roughness_texture_channel);\n"; + code += " float roughness_tex = dot(texture(texture_roughness, base_uv), roughness_texture_channel);\n"; } - code += " ROUGHNESS = roughness_tex * roughness;\n"; - code += " SPECULAR = specular;\n"; + code += R"( ROUGHNESS = roughness_tex * roughness; +)"; } else { if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " vec4 orm_tex = triplanar_texture(texture_orm,uv1_power_normal,uv1_triplanar_pos);\n"; + code += R"( + vec4 orm_tex = triplanar_texture(texture_orm, uv1_power_normal, uv1_triplanar_pos); +)"; } else { - code += " vec4 orm_tex = texture(texture_orm,base_uv);\n"; + code += R"( + vec4 orm_tex = texture(texture_orm, base_uv); +)"; } - code += " ROUGHNESS = orm_tex.g;\n"; - code += " METALLIC = orm_tex.b;\n"; + code += R"( ROUGHNESS = orm_tex.g; + METALLIC = orm_tex.b; +)"; } if (features[FEATURE_NORMAL_MAPPING]) { + code += R"( + // Normal Map: Enabled +)"; if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " NORMAL_MAP = triplanar_texture(texture_normal,uv1_power_normal,uv1_triplanar_pos).rgb;\n"; + code += " NORMAL_MAP = triplanar_texture(texture_normal, uv1_power_normal, uv1_triplanar_pos).rgb;\n"; } else { - code += " NORMAL_MAP = texture(texture_normal,base_uv).rgb;\n"; + code += " NORMAL_MAP = texture(texture_normal, base_uv).rgb;\n"; } code += " NORMAL_MAP_DEPTH = normal_scale;\n"; } if (features[FEATURE_EMISSION]) { + code += R"( + // Emission: Enabled +)"; if (flags[FLAG_EMISSION_ON_UV2]) { if (flags[FLAG_UV2_USE_TRIPLANAR]) { - code += " vec3 emission_tex = triplanar_texture(texture_emission,uv2_power_normal,uv2_triplanar_pos).rgb;\n"; + code += " vec3 emission_tex = triplanar_texture(texture_emission, uv2_power_normal, uv2_triplanar_pos).rgb;\n"; } else { - code += " vec3 emission_tex = texture(texture_emission,base_uv2).rgb;\n"; + code += " vec3 emission_tex = texture(texture_emission, base_uv2).rgb;\n"; } } else { if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " vec3 emission_tex = triplanar_texture(texture_emission,uv1_power_normal,uv1_triplanar_pos).rgb;\n"; + code += " vec3 emission_tex = triplanar_texture(texture_emission, uv1_power_normal, uv1_triplanar_pos).rgb;\n"; } else { - code += " vec3 emission_tex = texture(texture_emission,base_uv).rgb;\n"; + code += " vec3 emission_tex = texture(texture_emission, base_uv).rgb;\n"; } } if (emission_op == EMISSION_OP_ADD) { - code += " EMISSION = (emission.rgb+emission_tex)*emission_energy;\n"; + code += R"( // Emission Operator: Add + EMISSION = (emission.rgb + emission_tex) * emission_energy; +)"; } else { - code += " EMISSION = (emission.rgb*emission_tex)*emission_energy;\n"; + code += R"( // Emission Operator: Multiply + EMISSION = (emission.rgb * emission_tex) * emission_energy; +)"; } } if (features[FEATURE_REFRACTION]) { if (features[FEATURE_NORMAL_MAPPING]) { - code += " vec3 unpacked_normal = NORMAL_MAP;\n"; - code += " unpacked_normal.xy = unpacked_normal.xy * 2.0 - 1.0;\n"; - code += " unpacked_normal.z = sqrt(max(0.0, 1.0 - dot(unpacked_normal.xy, unpacked_normal.xy)));\n"; - code += " vec3 ref_normal = normalize( mix(NORMAL,TANGENT * unpacked_normal.x + BINORMAL * unpacked_normal.y + NORMAL * unpacked_normal.z,NORMAL_MAP_DEPTH) );\n"; + code += R"( + // Refraction: Enabled (with normal map texture) + vec3 unpacked_normal = NORMAL_MAP; + unpacked_normal.xy = unpacked_normal.xy * 2.0 - 1.0; + unpacked_normal.z = sqrt(max(0.0, 1.0 - dot(unpacked_normal.xy, unpacked_normal.xy))); + vec3 ref_normal = normalize(mix( + NORMAL, + TANGENT * unpacked_normal.x + BINORMAL * unpacked_normal.y + NORMAL * unpacked_normal.z, + NORMAL_MAP_DEPTH)); +)"; } else { - code += " vec3 ref_normal = NORMAL;\n"; + code += R"( + // Refraction: Enabled + vec3 ref_normal = NORMAL; +)"; } if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " vec2 ref_ofs = SCREEN_UV - ref_normal.xy * dot(triplanar_texture(texture_refraction,uv1_power_normal,uv1_triplanar_pos),refraction_texture_channel) * refraction;\n"; + code += " vec2 ref_ofs = SCREEN_UV - ref_normal.xy * dot(triplanar_texture(texture_refraction, uv1_power_normal, uv1_triplanar_pos), refraction_texture_channel) * refraction;\n"; } else { - code += " vec2 ref_ofs = SCREEN_UV - ref_normal.xy * dot(texture(texture_refraction,base_uv),refraction_texture_channel) * refraction;\n"; + code += " vec2 ref_ofs = SCREEN_UV - ref_normal.xy * dot(texture(texture_refraction, base_uv), refraction_texture_channel) * refraction;\n"; } - code += " float ref_amount = 1.0 - albedo.a * albedo_tex.a;\n"; - code += " EMISSION += textureLod(screen_texture,ref_ofs,ROUGHNESS * 8.0).rgb * ref_amount * EXPOSURE;\n"; - code += " ALBEDO *= 1.0 - ref_amount;\n"; - code += " ALPHA = 1.0;\n"; + code += R"( + float ref_amount = 1.0 - albedo.a * albedo_tex.a; + EMISSION += textureLod(screen_texture, ref_ofs, ROUGHNESS * 8.0).rgb * ref_amount * EXPOSURE; + ALBEDO *= 1.0 - ref_amount; + // Force transparency on the material (required for refraction). + ALPHA = 1.0; +)"; } else if (transparency != TRANSPARENCY_DISABLED || flags[FLAG_USE_SHADOW_TO_OPACITY] || (distance_fade == DISTANCE_FADE_PIXEL_ALPHA) || proximity_fade_enabled) { code += " ALPHA *= albedo.a * albedo_tex.a;\n"; @@ -1366,10 +1616,13 @@ void BaseMaterial3D::_update_shader() { } if (proximity_fade_enabled) { - code += " float depth_tex = textureLod(depth_texture,SCREEN_UV,0.0).r;\n"; - code += " vec4 world_pos = INV_PROJECTION_MATRIX * vec4(SCREEN_UV*2.0-1.0,depth_tex,1.0);\n"; - code += " world_pos.xyz/=world_pos.w;\n"; - code += " ALPHA*=clamp(1.0-smoothstep(world_pos.z+proximity_fade_distance,world_pos.z,VERTEX.z),0.0,1.0);\n"; + code += R"( + // Proximity Fade: Enabled + float depth_tex = textureLod(depth_texture, SCREEN_UV, 0.0).r; + vec4 world_pos = INV_PROJECTION_MATRIX * vec4(SCREEN_UV * 2.0 - 1.0, depth_tex, 1.0); + world_pos.xyz /= world_pos.w; + ALPHA *= clamp(1.0 - smoothstep(world_pos.z + proximity_fade_distance, world_pos.z, VERTEX.z), 0.0, 1.0); +)"; } if (distance_fade != DISTANCE_FADE_DISABLED) { @@ -1377,72 +1630,96 @@ void BaseMaterial3D::_update_shader() { // (Z distance), so that the fade is always the same regardless of the camera angle. if ((distance_fade == DISTANCE_FADE_OBJECT_DITHER || distance_fade == DISTANCE_FADE_PIXEL_DITHER)) { if (!RenderingServer::get_singleton()->is_low_end()) { - code += " {\n"; + code += "\n {"; if (distance_fade == DISTANCE_FADE_OBJECT_DITHER) { - code += " float fade_distance = length((VIEW_MATRIX * MODEL_MATRIX[3]));\n"; + code += R"( + // Distance Fade: Object Dither + float fade_distance = length((VIEW_MATRIX * MODEL_MATRIX[3])); +)"; } else { - code += " float fade_distance = length(VERTEX);\n"; + code += R"( + // Distance Fade: Pixel Dither + float fade_distance = length(VERTEX); +)"; } - // Use interleaved gradient noise, which is fast but still looks good. - code += " const vec3 magic = vec3(0.06711056f, 0.00583715f, 52.9829189f);"; - code += " float fade = clamp(smoothstep(distance_fade_min, distance_fade_max, fade_distance), 0.0, 1.0);\n"; - // Use a hard cap to prevent a few stray pixels from remaining when past the fade-out distance. - code += " if (fade < 0.001 || fade < fract(magic.z * fract(dot(FRAGCOORD.xy, magic.xy)))) {\n"; - code += " discard;\n"; - code += " }\n"; - - code += " }\n\n"; + code += R"( + // Use interleaved gradient noise, which is fast but still looks good. + const vec3 magic = vec3(0.06711056, 0.00583715, 52.9829189); + float fade = clamp(smoothstep(distance_fade_min, distance_fade_max, fade_distance), 0.0, 1.0); + // Use a hard cap to prevent a few stray pixels from remaining when past the fade-out distance. + if (fade < 0.001 || fade < fract(magic.z * fract(dot(FRAGCOORD.xy, magic.xy)))) { + discard; + } + } +)"; } - } else { - code += " ALPHA *= clamp(smoothstep(distance_fade_min, distance_fade_max, length(VERTEX)), 0.0, 1.0);\n"; + code += R"( + // Distance Fade: Pixel Alpha + ALPHA *= clamp(smoothstep(distance_fade_min, distance_fade_max, length(VERTEX)), 0.0, 1.0); +)"; } } if (features[FEATURE_RIM]) { + code += R"( + // Rim: Enabled +)"; if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " vec2 rim_tex = triplanar_texture(texture_rim,uv1_power_normal,uv1_triplanar_pos).xy;\n"; + code += " vec2 rim_tex = triplanar_texture(texture_rim, uv1_power_normal, uv1_triplanar_pos).xy;\n"; } else { - code += " vec2 rim_tex = texture(texture_rim,base_uv).xy;\n"; + code += " vec2 rim_tex = texture(texture_rim, base_uv).xy;\n"; } - code += " RIM = rim*rim_tex.x;"; - code += " RIM_TINT = rim_tint*rim_tex.y;\n"; + code += R"( RIM = rim * rim_tex.x; + RIM_TINT = rim_tint * rim_tex.y; +)"; } if (features[FEATURE_CLEARCOAT]) { + code += R"( + // Clearcoat: Enabled +)"; if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " vec2 clearcoat_tex = triplanar_texture(texture_clearcoat,uv1_power_normal,uv1_triplanar_pos).xy;\n"; + code += " vec2 clearcoat_tex = triplanar_texture(texture_clearcoat, uv1_power_normal, uv1_triplanar_pos).xy;\n"; } else { - code += " vec2 clearcoat_tex = texture(texture_clearcoat,base_uv).xy;\n"; + code += " vec2 clearcoat_tex = texture(texture_clearcoat, base_uv).xy;\n"; } - code += " CLEARCOAT = clearcoat*clearcoat_tex.x;"; - code += " CLEARCOAT_ROUGHNESS = clearcoat_roughness*clearcoat_tex.y;\n"; + code += R"( CLEARCOAT = clearcoat * clearcoat_tex.x; + CLEARCOAT_ROUGHNESS = clearcoat_roughness * clearcoat_tex.y; +)"; } if (features[FEATURE_ANISOTROPY]) { + code += R"( + // Anisotropy: Enabled +)"; if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " vec3 anisotropy_tex = triplanar_texture(texture_flowmap,uv1_power_normal,uv1_triplanar_pos).rga;\n"; + code += " vec3 anisotropy_tex = triplanar_texture(texture_flowmap, uv1_power_normal, uv1_triplanar_pos).rga;\n"; } else { - code += " vec3 anisotropy_tex = texture(texture_flowmap,base_uv).rga;\n"; + code += " vec3 anisotropy_tex = texture(texture_flowmap, base_uv).rga;\n"; } - code += " ANISOTROPY = anisotropy_ratio*anisotropy_tex.b;\n"; - code += " ANISOTROPY_FLOW = anisotropy_tex.rg*2.0-1.0;\n"; + code += R"( ANISOTROPY = anisotropy_ratio * anisotropy_tex.b; + ANISOTROPY_FLOW = anisotropy_tex.rg * 2.0 - 1.0; +)"; } if (features[FEATURE_AMBIENT_OCCLUSION]) { + code += R"( + // Ambient Occlusion: Enabled +)"; if (!orm) { if (flags[FLAG_AO_ON_UV2]) { if (flags[FLAG_UV2_USE_TRIPLANAR]) { - code += " AO = dot(triplanar_texture(texture_ambient_occlusion,uv2_power_normal,uv2_triplanar_pos),ao_texture_channel);\n"; + code += " AO = dot(triplanar_texture(texture_ambient_occlusion, uv2_power_normal, uv2_triplanar_pos), ao_texture_channel);\n"; } else { - code += " AO = dot(texture(texture_ambient_occlusion,base_uv2),ao_texture_channel);\n"; + code += " AO = dot(texture(texture_ambient_occlusion, base_uv2), ao_texture_channel);\n"; } } else { if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " AO = dot(triplanar_texture(texture_ambient_occlusion,uv1_power_normal,uv1_triplanar_pos),ao_texture_channel);\n"; + code += " AO = dot(triplanar_texture(texture_ambient_occlusion, uv1_power_normal, uv1_triplanar_pos), ao_texture_channel);\n"; } else { - code += " AO = dot(texture(texture_ambient_occlusion,base_uv),ao_texture_channel);\n"; + code += " AO = dot(texture(texture_ambient_occlusion, base_uv), ao_texture_channel);\n"; } } } else { @@ -1453,75 +1730,103 @@ void BaseMaterial3D::_update_shader() { } if (features[FEATURE_SUBSURFACE_SCATTERING]) { + code += R"( + // Subsurface Scattering: Enabled +)"; if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " float sss_tex = triplanar_texture(texture_subsurface_scattering,uv1_power_normal,uv1_triplanar_pos).r;\n"; + code += " float sss_tex = triplanar_texture(texture_subsurface_scattering, uv1_power_normal, uv1_triplanar_pos).r;\n"; } else { - code += " float sss_tex = texture(texture_subsurface_scattering,base_uv).r;\n"; + code += " float sss_tex = texture(texture_subsurface_scattering, base_uv).r;\n"; } - code += " SSS_STRENGTH=subsurface_scattering_strength*sss_tex;\n"; + code += " SSS_STRENGTH = subsurface_scattering_strength * sss_tex;\n"; } if (features[FEATURE_SUBSURFACE_TRANSMITTANCE]) { + code += R"( + // Subsurface Scattering Transmittance: Enabled +)"; if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " vec4 trans_color_tex = triplanar_texture(texture_subsurface_transmittance,uv1_power_normal,uv1_triplanar_pos);\n"; + code += " vec4 trans_color_tex = triplanar_texture(texture_subsurface_transmittance, uv1_power_normal, uv1_triplanar_pos);\n"; } else { - code += " vec4 trans_color_tex = texture(texture_subsurface_transmittance,base_uv);\n"; + code += " vec4 trans_color_tex = texture(texture_subsurface_transmittance, base_uv);\n"; } - code += " SSS_TRANSMITTANCE_COLOR=transmittance_color*trans_color_tex;\n"; + code += " SSS_TRANSMITTANCE_COLOR = transmittance_color * trans_color_tex;\n"; - code += " SSS_TRANSMITTANCE_DEPTH=transmittance_depth;\n"; - code += " SSS_TRANSMITTANCE_BOOST=transmittance_boost;\n"; + code += R"( SSS_TRANSMITTANCE_DEPTH = transmittance_depth; + SSS_TRANSMITTANCE_BOOST = transmittance_boost; +)"; } if (features[FEATURE_BACKLIGHT]) { + code += R"( + // Backlight: Enabled +)"; if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " vec3 backlight_tex = triplanar_texture(texture_backlight,uv1_power_normal,uv1_triplanar_pos).rgb;\n"; + code += " vec3 backlight_tex = triplanar_texture(texture_backlight, uv1_power_normal, uv1_triplanar_pos).rgb;\n"; } else { - code += " vec3 backlight_tex = texture(texture_backlight,base_uv).rgb;\n"; + code += " vec3 backlight_tex = texture(texture_backlight, base_uv).rgb;\n"; } - code += " BACKLIGHT = (backlight.rgb+backlight_tex);\n"; + code += " BACKLIGHT = (backlight.rgb + backlight_tex);\n"; } if (features[FEATURE_DETAIL]) { - bool triplanar = (flags[FLAG_UV1_USE_TRIPLANAR] && detail_uv == DETAIL_UV_1) || (flags[FLAG_UV2_USE_TRIPLANAR] && detail_uv == DETAIL_UV_2); - + code += R"( + // Detail: Enabled +)"; + const bool triplanar = (flags[FLAG_UV1_USE_TRIPLANAR] && detail_uv == DETAIL_UV_1) || (flags[FLAG_UV2_USE_TRIPLANAR] && detail_uv == DETAIL_UV_2); if (triplanar) { - String tp_uv = detail_uv == DETAIL_UV_1 ? "uv1" : "uv2"; - code += " vec4 detail_tex = triplanar_texture(texture_detail_albedo," + tp_uv + "_power_normal," + tp_uv + "_triplanar_pos);\n"; - code += " vec4 detail_norm_tex = triplanar_texture(texture_detail_normal," + tp_uv + "_power_normal," + tp_uv + "_triplanar_pos);\n"; - + const String tp_uv = detail_uv == DETAIL_UV_1 ? "uv1" : "uv2"; + code += vformat(R"( vec4 detail_tex = triplanar_texture(texture_detail_albedo, %s_power_normal, %s_triplanar_pos); + vec4 detail_norm_tex = triplanar_texture(texture_detail_normal, %s_power_normal, %s_triplanar_pos); +)", + tp_uv, tp_uv, tp_uv, tp_uv); } else { - String det_uv = detail_uv == DETAIL_UV_1 ? "base_uv" : "base_uv2"; - code += " vec4 detail_tex = texture(texture_detail_albedo," + det_uv + ");\n"; - code += " vec4 detail_norm_tex = texture(texture_detail_normal," + det_uv + ");\n"; + const String det_uv = detail_uv == DETAIL_UV_1 ? "base_uv" : "base_uv2"; + code += vformat(R"( vec4 detail_tex = texture(texture_detail_albedo, %s); + vec4 detail_norm_tex = texture(texture_detail_normal, %s); +)", + det_uv, det_uv); } if (flags[FLAG_UV1_USE_TRIPLANAR]) { - code += " vec4 detail_mask_tex = triplanar_texture(texture_detail_mask,uv1_power_normal,uv1_triplanar_pos);\n"; + code += " vec4 detail_mask_tex = triplanar_texture(texture_detail_mask, uv1_power_normal, uv1_triplanar_pos);\n"; } else { - code += " vec4 detail_mask_tex = texture(texture_detail_mask,base_uv);\n"; + code += " vec4 detail_mask_tex = texture(texture_detail_mask, base_uv);\n"; } switch (detail_blend_mode) { case BLEND_MODE_MIX: { - code += " vec3 detail = mix(ALBEDO.rgb,detail_tex.rgb,detail_tex.a);\n"; + code += R"( + // Detail Blend Mode: Mix + vec3 detail = mix(ALBEDO.rgb, detail_tex.rgb, detail_tex.a); +)"; } break; case BLEND_MODE_ADD: { - code += " vec3 detail = mix(ALBEDO.rgb,ALBEDO.rgb+detail_tex.rgb,detail_tex.a);\n"; + code += R"( + // Detail Blend Mode: Add + vec3 detail = mix(ALBEDO.rgb, ALBEDO.rgb + detail_tex.rgb, detail_tex.a); +)"; } break; case BLEND_MODE_SUB: { - code += " vec3 detail = mix(ALBEDO.rgb,ALBEDO.rgb-detail_tex.rgb,detail_tex.a);\n"; + code += R"( + // Detail Blend Mode: Subtract + vec3 detail = mix(ALBEDO.rgb, ALBEDO.rgb - detail_tex.rgb, detail_tex.a); +)"; } break; case BLEND_MODE_MUL: { - code += " vec3 detail = mix(ALBEDO.rgb,ALBEDO.rgb*detail_tex.rgb,detail_tex.a);\n"; + code += R"( + // Detail Blend Mode: Multiply + vec3 detail = mix(ALBEDO.rgb, ALBEDO.rgb * detail_tex.rgb, detail_tex.a); +)"; } break; case BLEND_MODE_MAX: break; // Internal value, skip. } - code += " vec3 detail_norm = mix(NORMAL_MAP,detail_norm_tex.rgb,detail_tex.a);\n"; - code += " NORMAL_MAP = mix(NORMAL_MAP,detail_norm,detail_mask_tex.r);\n"; - code += " ALBEDO.rgb = mix(ALBEDO.rgb,detail,detail_mask_tex.r);\n"; + code += R"( vec3 detail_norm = mix(NORMAL_MAP, detail_norm_tex.rgb, detail_tex.a); + NORMAL_MAP = mix(NORMAL_MAP, detail_norm, detail_mask_tex.r); + ALBEDO.rgb = mix(ALBEDO.rgb, detail, detail_mask_tex.r); +)"; } code += "}\n";