Skip to content

Commit 033e7bf

Browse files
jakra-mbgithub-actions[bot]
authored andcommitted
Added shader functionality for model dissolve and cross-fade (internal-8587)
GitOrigin-RevId: d4a80a5601bae218d6f79739bbd656dd41149ca0
1 parent 694c3e2 commit 033e7bf

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

3d-style/shaders/model.fragment.glsl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
uniform float u_opacity;
66

7+
#ifdef DITHERED_DISCARD
8+
uniform float u_dithered_discard_threshold;
9+
#endif
10+
711
uniform vec3 u_lightcolor;
812
uniform vec3 u_lightpos;
913
uniform float u_lightintensity;
@@ -546,6 +550,27 @@ vec4 finalColor;
546550
finalColor = vec4(color, opacity);
547551
#endif // !DIFFUSE_SHADED
548552

553+
#ifdef DITHERED_DISCARD
554+
// fade in/out using discard and 4x4 Bayer matrix for dithering
555+
// creates a smooth transition without alpha blending
556+
// if u_dithered_discard_threshold == 1 or -1, the model gets fully rendered, at 0 it is fully discarded
557+
// values between 0 and +1/-1 get partically discarded, using the ditherValue or the negated dither value as the threshold
558+
// this allows to cross-fade between two models
559+
if (abs(u_dithered_discard_threshold) < 1.0) {
560+
561+
// Get dither value for pixel at coordinate using "Interleaved gradient noise"
562+
float ditherValue = fract(52.9829189 * fract(0.06711056 * gl_FragCoord.x + 0.00583715 * gl_FragCoord.y));
563+
564+
// Fade in: discard if threshold < ditherValue
565+
// Fade out: discard if -threshold < 1.0 - ditherValue
566+
// Use mix to select values without branching
567+
float compareValue = mix(1.0 - ditherValue, ditherValue, step(0.0, u_dithered_discard_threshold));
568+
if (abs(u_dithered_discard_threshold) < compareValue) {
569+
discard;
570+
}
571+
}
572+
#endif
573+
549574
#ifdef FOG
550575
finalColor = fog_dither(fog_apply_premultiplied(finalColor, v_fog_pos, v_position_height.w));
551576
#endif

0 commit comments

Comments
 (0)