import * as THREE from 'three'; /** * Shader for the non-interactive Panel background. * * This shader renders a simple, anti-aliased rounded rectangle (squircle). It * can display either a solid background color or a texture map. It is more * performant than the SpatialPanelShader as it omits all interactive highlight * calculations. */ export const SquircleShader = { uniforms: { uMainTex: {value: null}, uUseImage: {value: 0.0}, uBackgroundColor: { value: new THREE.Vector4(0.4, 0.8, 1.0, 1.0), }, uBoxSize: {value: new THREE.Vector2(0.5, 0.5)}, uRadius: {value: 0.05}, uOpacity: {value: 1.0}, }, vertexShader: /* glsl */ ` #define USE_UV #include #include #include #include #include #include #include #include #include #include void main() { #include #include #include #include #include #if defined ( USE_ENVMAP ) || defined ( USE_SKINNING ) #include #include #include #include #include #endif #include #include #include #include #include #include #include #include #include } `, fragmentShader: /* glsl */ ` precision mediump float; uniform sampler2D uMainTex; uniform vec4 uBackgroundColor; uniform vec2 uBoxSize; uniform float uRadius; uniform float uUseImage; uniform float uOpacity; #define USE_UV #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Distance function for rounded box. float distRoundBox(vec2 p, vec2 b, float r) { return length(max(abs(p) - b + r, 0.0)) - r; } void main(void) { #include #include #include #include #include #include #include #include vec2 size = uBoxSize * 1000.0; // Calculates the adjusted radius based on box size. float radius = min(size.x, size.y) * (0.05 + uRadius); vec2 half_size = 0.5 * size; // Compute the distance from the rounded box edge. float dist = distRoundBox(vUv * size - half_size, half_size, radius); // Use lerp for smooth color transition based on distance. vec4 colorInside = uBackgroundColor; if (uUseImage > 0.5) { colorInside = texture2D(uMainTex, vUv); colorInside.a = 1.0; } // Transparent black for outside. vec4 colorOutside = vec4(0.0, 0.0, 0.0, 0.0); vec4 finalColor = mix(colorInside, colorOutside, smoothstep(0.0, 1.0, dist)); // Return premultiplied alpha. gl_FragColor = uOpacity * finalColor.a * vec4(finalColor.rgb, 1.0); } `, };