/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ // Display a signed distance field as color // // It will be orange outside the shape (positive distance), and blue // inside (negative distance). // // Color will cycle every 10px. // // The shape outline (distance = 0) is drawn as a thin white outline. // // NaNs and infinite values will be drawn as red and yellow respectively. // // Example usage: // // #include debug // // void main(void) { // float d = compute_some_distance_in_pixels(...); // oFragColor = debug_sdf(d); // } // vec4 debug_sdf(float d) { if (isnan(d)) { return vec4(1.0, 0.0, 0.0, 1.0); } if (isinf(d)) { return vec4(1.0, 1.0, 0.0, 1.0); } vec3 color = (d > 0.0) ? vec3(0.9, 0.6, 0.3) : vec3(0.6, 0.8, 1.0); color *= 1.0 - exp(-0.32 * abs(d)); color *= fract(d / 10.0) * 0.4 + 0.6; #ifdef SWGL // SWGL doesn't support smoothstep() for now color = mix(color, vec3(1.0), step(abs(d), 2.0)); #else color = mix(color, vec3(1.0), smoothstep(2.0, 0.0, abs(d))); #endif return vec4(color, 1.0); }