|
| 1 | +/* |
| 2 | + * If not stated otherwise in this file or this component's LICENSE file the |
| 3 | + * following copyright and licenses apply: |
| 4 | + * |
| 5 | + * Copyright 2023 Comcast Cable Communications Management, LLC. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the License); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +import { getNormalizedRgbaComponents } from '../../../../lib/utils.js'; |
| 20 | +import { |
| 21 | + type DefaultEffectProps, |
| 22 | + ShaderEffect, |
| 23 | + type ShaderEffectUniforms, |
| 24 | +} from './ShaderEffect.js'; |
| 25 | + |
| 26 | +/** |
| 27 | + * Properties of the {@link RadialGradientEffect} effect |
| 28 | + */ |
| 29 | +export interface RadialGradientEffectProps extends DefaultEffectProps { |
| 30 | + /** |
| 31 | + * Array of colors to be used in the RadialGradientEffect |
| 32 | + * |
| 33 | + * @default [0xff000000, 0xffffffff] |
| 34 | + */ |
| 35 | + colors?: number[]; |
| 36 | + /** |
| 37 | + * Array of color stops |
| 38 | + */ |
| 39 | + stops?: number[]; |
| 40 | + /** |
| 41 | + * Width of the RadialGradientEffect |
| 42 | + */ |
| 43 | + width?: number; |
| 44 | + /** |
| 45 | + * height of the RadialGradientEffect |
| 46 | + * |
| 47 | + * @remarks if not defined uses the width value |
| 48 | + */ |
| 49 | + height?: number; |
| 50 | + /** |
| 51 | + * center point of where the RadialGradientEffect is drawn |
| 52 | + */ |
| 53 | + pivot?: number[]; |
| 54 | +} |
| 55 | + |
| 56 | +export class RadialGradientEffect extends ShaderEffect { |
| 57 | + static z$__type__Props: RadialGradientEffectProps; |
| 58 | + override readonly name = 'radialGradient'; |
| 59 | + |
| 60 | + static override getEffectKey(props: RadialGradientEffectProps): string { |
| 61 | + return `radialGradient${props.colors!.length}`; |
| 62 | + } |
| 63 | + |
| 64 | + static override resolveDefaults( |
| 65 | + props: RadialGradientEffectProps, |
| 66 | + ): Required<RadialGradientEffectProps> { |
| 67 | + const colors = props.colors ?? [0xff000000, 0xffffffff]; |
| 68 | + |
| 69 | + let stops = props.stops; |
| 70 | + if (!stops) { |
| 71 | + stops = []; |
| 72 | + const calc = colors.length - 1; |
| 73 | + for (let i = 0; i < colors.length; i++) { |
| 74 | + stops.push(i * (1 / calc)); |
| 75 | + } |
| 76 | + } |
| 77 | + return { |
| 78 | + colors, |
| 79 | + stops, |
| 80 | + width: props.width ?? 0, |
| 81 | + height: props.height ?? props.width ?? 0, |
| 82 | + pivot: props.pivot ?? [0.5, 0.5], |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + static override uniforms: ShaderEffectUniforms = { |
| 87 | + width: { |
| 88 | + value: 0, |
| 89 | + method: 'uniform1f', |
| 90 | + type: 'float', |
| 91 | + }, |
| 92 | + height: { |
| 93 | + value: 0, |
| 94 | + method: 'uniform1f', |
| 95 | + type: 'float', |
| 96 | + }, |
| 97 | + pivot: { |
| 98 | + value: [0.5, 0.5], |
| 99 | + method: 'uniform2fv', |
| 100 | + type: 'vec2', |
| 101 | + }, |
| 102 | + colors: { |
| 103 | + value: 0xffffffff, |
| 104 | + validator: (rgbas: number[]): number[] => { |
| 105 | + const cols = rgbas.map((rgbas) => getNormalizedRgbaComponents(rgbas)); |
| 106 | + return cols.reduce((acc, val) => acc.concat(val), [] as number[]); |
| 107 | + }, |
| 108 | + size: (props: RadialGradientEffectProps) => props.colors!.length, |
| 109 | + method: 'uniform4fv', |
| 110 | + type: 'vec4', |
| 111 | + }, |
| 112 | + stops: { |
| 113 | + value: [], |
| 114 | + validator: ( |
| 115 | + value: number[], |
| 116 | + props: RadialGradientEffectProps, |
| 117 | + ): number[] => { |
| 118 | + const colors = props.colors ?? []; |
| 119 | + let stops = value; |
| 120 | + const tmp: number[] = value; |
| 121 | + if (stops.length === 0 || (stops && stops.length !== colors.length)) { |
| 122 | + for (let i = 0; i < colors.length; i++) { |
| 123 | + if (stops[i]) { |
| 124 | + tmp[i] = stops[i]!; |
| 125 | + if (stops[i - 1] === undefined && tmp[i - 2] !== undefined) { |
| 126 | + tmp[i - 1] = tmp[i - 2]! + (stops[i]! - tmp[i - 2]!) / 2; |
| 127 | + } |
| 128 | + } else { |
| 129 | + tmp[i] = i * (1 / (colors.length - 1)); |
| 130 | + } |
| 131 | + } |
| 132 | + stops = tmp; |
| 133 | + } |
| 134 | + return tmp; |
| 135 | + }, |
| 136 | + size: (props: RadialGradientEffectProps) => props.colors!.length, |
| 137 | + method: 'uniform1fv', |
| 138 | + type: 'float', |
| 139 | + }, |
| 140 | + }; |
| 141 | + |
| 142 | + static override onColorize = (props: RadialGradientEffectProps) => { |
| 143 | + const colors = props.colors!.length || 1; |
| 144 | + return ` |
| 145 | + vec2 point = v_textureCoordinate.xy * u_dimensions; |
| 146 | + vec2 projection = vec2(pivot.x * u_dimensions.x, pivot.y * u_dimensions.y); |
| 147 | +
|
| 148 | + float dist = length((point - projection) / vec2(width, height)); |
| 149 | +
|
| 150 | + float stopCalc = (dist - stops[0]) / (stops[1] - stops[0]); |
| 151 | + vec4 colorOut = mix(colors[0], colors[1], stopCalc); |
| 152 | + for(int i = 1; i < ${colors}-1; i++) { |
| 153 | + stopCalc = (dist - stops[i]) / (stops[i + 1] - stops[i]); |
| 154 | + colorOut = mix(colorOut, colors[i + 1], clamp(stopCalc, 0.0, 1.0)); |
| 155 | + } |
| 156 | + return mix(maskColor, colorOut, colorOut.a); |
| 157 | + `; |
| 158 | + }; |
| 159 | +} |
0 commit comments