@@ -3175,7 +3175,7 @@ void BaseMaterial3D::_prepare_stencil_effect() {
31753175 stencil_next_pass->set_grow (stencil_effect_outline_thickness);
31763176 stencil_next_pass->set_albedo (stencil_effect_color);
31773177 stencil_next_pass->set_stencil_mode (STENCIL_MODE_CUSTOM);
3178- stencil_next_pass->set_stencil_flags (STENCIL_FLAG_READ | STENCIL_FLAG_WRITE );
3178+ stencil_next_pass->set_stencil_flags (STENCIL_FLAG_READ);
31793179 stencil_next_pass->set_stencil_compare (STENCIL_COMPARE_NOT_EQUAL);
31803180 stencil_next_pass->set_stencil_reference (stencil_reference);
31813181 break ;
@@ -3190,7 +3190,7 @@ void BaseMaterial3D::_prepare_stencil_effect() {
31903190 stencil_next_pass->set_grow (0 );
31913191 stencil_next_pass->set_albedo (stencil_effect_color);
31923192 stencil_next_pass->set_stencil_mode (STENCIL_MODE_CUSTOM);
3193- stencil_next_pass->set_stencil_flags (STENCIL_FLAG_READ | STENCIL_FLAG_WRITE );
3193+ stencil_next_pass->set_stencil_flags (STENCIL_FLAG_READ);
31943194 stencil_next_pass->set_stencil_compare (STENCIL_COMPARE_NOT_EQUAL);
31953195 stencil_next_pass->set_stencil_reference (stencil_reference);
31963196 break ;
@@ -3232,14 +3232,21 @@ void BaseMaterial3D::set_stencil_flags(int p_stencil_flags) {
32323232 return ;
32333233 }
32343234
3235+ // If enabling read while already writing, switch to read only.
32353236 if ((p_stencil_flags & STENCIL_FLAG_READ) && (stencil_flags & (STENCIL_FLAG_WRITE | STENCIL_FLAG_WRITE_DEPTH_FAIL))) {
32363237 p_stencil_flags = p_stencil_flags & STENCIL_FLAG_READ;
32373238 }
32383239
3240+ // If enabling write while already reading, switch to write or write_depth_fail.
32393241 if ((p_stencil_flags & (STENCIL_FLAG_WRITE | STENCIL_FLAG_WRITE_DEPTH_FAIL)) && (stencil_flags & STENCIL_FLAG_READ)) {
32403242 p_stencil_flags = p_stencil_flags & (STENCIL_FLAG_WRITE | STENCIL_FLAG_WRITE_DEPTH_FAIL);
32413243 }
32423244
3245+ // If enabling read+write while already doing neither, only allow read.
3246+ if ((p_stencil_flags & STENCIL_FLAG_READ) && (p_stencil_flags & (STENCIL_FLAG_WRITE | STENCIL_FLAG_WRITE_DEPTH_FAIL))) {
3247+ p_stencil_flags = p_stencil_flags & STENCIL_FLAG_READ;
3248+ }
3249+
32433250 stencil_flags = p_stencil_flags;
32443251 _queue_shader_change ();
32453252}
0 commit comments