From b0947003e01cfe9d193cbe577cf9950fefc5cfde Mon Sep 17 00:00:00 2001 From: Apples <2352020+apples@users.noreply.github.com> Date: Sat, 21 Jun 2025 11:04:29 -0500 Subject: [PATCH] Fix stencil preset next_pass stencil flags --- scene/resources/material.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/scene/resources/material.cpp b/scene/resources/material.cpp index a7d5693f185..4965e5509a7 100644 --- a/scene/resources/material.cpp +++ b/scene/resources/material.cpp @@ -3175,7 +3175,7 @@ void BaseMaterial3D::_prepare_stencil_effect() { stencil_next_pass->set_grow(stencil_effect_outline_thickness); stencil_next_pass->set_albedo(stencil_effect_color); stencil_next_pass->set_stencil_mode(STENCIL_MODE_CUSTOM); - stencil_next_pass->set_stencil_flags(STENCIL_FLAG_READ | STENCIL_FLAG_WRITE); + stencil_next_pass->set_stencil_flags(STENCIL_FLAG_READ); stencil_next_pass->set_stencil_compare(STENCIL_COMPARE_NOT_EQUAL); stencil_next_pass->set_stencil_reference(stencil_reference); break; @@ -3190,7 +3190,7 @@ void BaseMaterial3D::_prepare_stencil_effect() { stencil_next_pass->set_grow(0); stencil_next_pass->set_albedo(stencil_effect_color); stencil_next_pass->set_stencil_mode(STENCIL_MODE_CUSTOM); - stencil_next_pass->set_stencil_flags(STENCIL_FLAG_READ | STENCIL_FLAG_WRITE); + stencil_next_pass->set_stencil_flags(STENCIL_FLAG_READ); stencil_next_pass->set_stencil_compare(STENCIL_COMPARE_NOT_EQUAL); stencil_next_pass->set_stencil_reference(stencil_reference); break; @@ -3232,14 +3232,21 @@ void BaseMaterial3D::set_stencil_flags(int p_stencil_flags) { return; } + // If enabling read while already writing, switch to read only. if ((p_stencil_flags & STENCIL_FLAG_READ) && (stencil_flags & (STENCIL_FLAG_WRITE | STENCIL_FLAG_WRITE_DEPTH_FAIL))) { p_stencil_flags = p_stencil_flags & STENCIL_FLAG_READ; } + // If enabling write while already reading, switch to write or write_depth_fail. if ((p_stencil_flags & (STENCIL_FLAG_WRITE | STENCIL_FLAG_WRITE_DEPTH_FAIL)) && (stencil_flags & STENCIL_FLAG_READ)) { p_stencil_flags = p_stencil_flags & (STENCIL_FLAG_WRITE | STENCIL_FLAG_WRITE_DEPTH_FAIL); } + // If enabling read+write while already doing neither, only allow read. + if ((p_stencil_flags & STENCIL_FLAG_READ) && (p_stencil_flags & (STENCIL_FLAG_WRITE | STENCIL_FLAG_WRITE_DEPTH_FAIL))) { + p_stencil_flags = p_stencil_flags & STENCIL_FLAG_READ; + } + stencil_flags = p_stencil_flags; _queue_shader_change(); }