@lightningtv/renderer
Version:
Lightning 3 Renderer
86 lines (72 loc) • 2.51 kB
JavaScript
import { calcFactoredRadiusArray } from '../../lib/utils.js';
import { RoundedTemplate, } from '../templates/RoundedTemplate.js';
/**
* Similar to the {@link DefaultShader} but cuts out 4 rounded rectangle corners
* as defined by the specified corner {@link RoundedProps.radius}
*/
export const Rounded = {
props: RoundedTemplate.props,
update(node) {
this.uniform4fa('u_radius', calcFactoredRadiusArray(this.props.radius, node.width, node.height));
},
vertex: `
precision highp float;
precision mediump float;
attribute vec2 a_position;
attribute vec2 a_textureCoords;
attribute vec4 a_color;
attribute vec2 a_nodeCoords;
uniform vec2 u_resolution;
uniform float u_pixelRatio;
uniform vec2 u_dimensions;
varying vec4 v_color;
varying vec2 v_textureCoords;
varying vec2 v_nodeCoords;
void main() {
vec2 normalized = a_position * u_pixelRatio;
vec2 screenSpace = vec2(2.0 / u_resolution.x, -2.0 / u_resolution.y);
v_color = a_color;
v_nodeCoords = a_nodeCoords;
v_textureCoords = a_textureCoords;
gl_Position = vec4(normalized.x * screenSpace.x - 1.0, normalized.y * -abs(screenSpace.y) + 1.0, 0.0, 1.0);
gl_Position.y = -sign(screenSpace.y) * gl_Position.y;
}
`,
fragment: `
precision highp float;
precision mediump float;
//renderer applies these uniforms automatically
uniform vec2 u_resolution;
uniform vec2 u_dimensions;
uniform float u_alpha;
uniform sampler2D u_texture;
//custom uniforms
uniform vec4 u_radius;
varying vec4 v_color;
varying vec2 v_textureCoords;
varying vec2 v_nodeCoords;
float roundedBox(vec2 p, vec2 s, vec4 r) {
r.xy = (p.x > 0.0) ? r.yz : r.xw;
r.x = (p.y > 0.0) ? r.y : r.x;
vec2 q = abs(p) - s + r.x;
return (min(max(q.x, q.y), 0.0) + length(max(q, 0.0))) - r.x;
}
void main() {
vec4 color = texture2D(u_texture, v_textureCoords) * v_color;
vec2 halfDimensions = (u_dimensions * 0.5);
vec2 boxUv = v_nodeCoords.xy * u_dimensions - halfDimensions;
float boxDist = roundedBox(boxUv, halfDimensions, u_radius);
float roundedAlpha = 1.0 - smoothstep(0.0, 1.0, boxDist);
vec4 resColor = vec4(0.0);
resColor = mix(resColor, color, roundedAlpha);
gl_FragColor = resColor * u_alpha;
}
`,
};
//# sourceMappingURL=Rounded.js.map