|
| 1 | +import { NormalBlending, AddEquation, SrcAlphaFactor, OneMinusSrcAlphaFactor } from '../../constants.js'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Represents blending configuration. |
| 5 | + * |
| 6 | + * This class encapsulates all blending-related properties that control how |
| 7 | + * a material's colors are combined with the colors already in the frame buffer. |
| 8 | + */ |
| 9 | +class BlendMode { |
| 10 | + |
| 11 | + /** |
| 12 | + * Constructs a new blending configuration. |
| 13 | + * |
| 14 | + * @param {(NoBlending|NormalBlending|AdditiveBlending|SubtractiveBlending|MultiplyBlending|CustomBlending|MaterialBlending)} [blending=NormalBlending] - The blending mode. |
| 15 | + */ |
| 16 | + constructor( blending = NormalBlending ) { |
| 17 | + |
| 18 | + /** |
| 19 | + * Defines the blending type. |
| 20 | + * |
| 21 | + * It must be set to `CustomBlending` if custom blending properties like |
| 22 | + * {@link BlendMode#blendSrc}, {@link BlendMode#blendDst} or {@link BlendMode#blendEquation} |
| 23 | + * should have any effect. |
| 24 | + * |
| 25 | + * @type {(NoBlending|NormalBlending|AdditiveBlending|SubtractiveBlending|MultiplyBlending|CustomBlending|MaterialBlending)} |
| 26 | + * @default NormalBlending |
| 27 | + */ |
| 28 | + this.blending = blending; |
| 29 | + |
| 30 | + /** |
| 31 | + * Defines the blending source factor. |
| 32 | + * |
| 33 | + * This determines how the source (incoming) fragment color is factored before being added |
| 34 | + * to the destination (existing) fragment color in the frame buffer. |
| 35 | + * |
| 36 | + * @type {(ZeroFactor|OneFactor|SrcColorFactor|OneMinusSrcColorFactor|SrcAlphaFactor|OneMinusSrcAlphaFactor|DstAlphaFactor|OneMinusDstAlphaFactor|DstColorFactor|OneMinusDstColorFactor|SrcAlphaSaturateFactor|ConstantColorFactor|OneMinusConstantColorFactor|ConstantAlphaFactor|OneMinusConstantAlphaFactor)} |
| 37 | + * @default SrcAlphaFactor |
| 38 | + */ |
| 39 | + this.blendSrc = SrcAlphaFactor; |
| 40 | + |
| 41 | + /** |
| 42 | + * Defines the blending destination factor. |
| 43 | + * |
| 44 | + * This determines how the destination (existing) fragment color in the frame buffer |
| 45 | + * is factored before being combined with the source (incoming) fragment color. |
| 46 | + * |
| 47 | + * @type {(ZeroFactor|OneFactor|SrcColorFactor|OneMinusSrcColorFactor|SrcAlphaFactor|OneMinusSrcAlphaFactor|DstAlphaFactor|OneMinusDstAlphaFactor|DstColorFactor|OneMinusDstColorFactor|SrcAlphaSaturateFactor|ConstantColorFactor|OneMinusConstantColorFactor|ConstantAlphaFactor|OneMinusConstantAlphaFactor)} |
| 48 | + * @default OneMinusSrcAlphaFactor |
| 49 | + */ |
| 50 | + this.blendDst = OneMinusSrcAlphaFactor; |
| 51 | + |
| 52 | + /** |
| 53 | + * Defines the blending equation. |
| 54 | + * |
| 55 | + * This determines how the source and destination colors are combined. |
| 56 | + * |
| 57 | + * @type {(AddEquation|SubtractEquation|ReverseSubtractEquation|MinEquation|MaxEquation)} |
| 58 | + * @default AddEquation |
| 59 | + */ |
| 60 | + this.blendEquation = AddEquation; |
| 61 | + |
| 62 | + /** |
| 63 | + * Defines the blending source alpha factor. |
| 64 | + * |
| 65 | + * When set, this allows separate control of the alpha channel's source blending factor. |
| 66 | + * If `null`, {@link BlendMode#blendSrc} is used for the alpha channel as well. |
| 67 | + * |
| 68 | + * @type {?(ZeroFactor|OneFactor|SrcColorFactor|OneMinusSrcColorFactor|SrcAlphaFactor|OneMinusSrcAlphaFactor|DstAlphaFactor|OneMinusDstAlphaFactor|DstColorFactor|OneMinusDstColorFactor|SrcAlphaSaturateFactor|ConstantColorFactor|OneMinusConstantColorFactor|ConstantAlphaFactor|OneMinusConstantAlphaFactor)} |
| 69 | + * @default null |
| 70 | + */ |
| 71 | + this.blendSrcAlpha = null; |
| 72 | + |
| 73 | + /** |
| 74 | + * Defines the blending destination alpha factor. |
| 75 | + * |
| 76 | + * When set, this allows separate control of the alpha channel's destination blending factor. |
| 77 | + * If `null`, {@link BlendMode#blendDst} is used for the alpha channel as well. |
| 78 | + * |
| 79 | + * @type {?(ZeroFactor|OneFactor|SrcColorFactor|OneMinusSrcColorFactor|SrcAlphaFactor|OneMinusSrcAlphaFactor|DstAlphaFactor|OneMinusDstAlphaFactor|DstColorFactor|OneMinusDstColorFactor|SrcAlphaSaturateFactor|ConstantColorFactor|OneMinusConstantColorFactor|ConstantAlphaFactor|OneMinusConstantAlphaFactor)} |
| 80 | + * @default null |
| 81 | + */ |
| 82 | + this.blendDstAlpha = null; |
| 83 | + |
| 84 | + /** |
| 85 | + * Defines the blending equation of the alpha channel. |
| 86 | + * |
| 87 | + * When set, this allows separate control of the alpha channel's blending equation. |
| 88 | + * If `null`, {@link BlendMode#blendEquation} is used for the alpha channel as well. |
| 89 | + * |
| 90 | + * @type {?(AddEquation|SubtractEquation|ReverseSubtractEquation|MinEquation|MaxEquation)} |
| 91 | + * @default null |
| 92 | + */ |
| 93 | + this.blendEquationAlpha = null; |
| 94 | + |
| 95 | + /** |
| 96 | + * Defines whether to premultiply the alpha (transparency) value. |
| 97 | + * |
| 98 | + * If `true`, the RGB color of the texture or material is multiplied by its alpha value. |
| 99 | + * This is useful for transparent textures/materials where the color data |
| 100 | + * should already include the transparency information. |
| 101 | + * |
| 102 | + * @type {boolean} |
| 103 | + * @default false |
| 104 | + */ |
| 105 | + this.premultiplyAlpha = false; |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Copies the blending properties from the given source to this instance. |
| 111 | + * |
| 112 | + * @param {BlendMode} source - The blending configuration to copy from. |
| 113 | + * @return {BlendMode} A reference to this instance. |
| 114 | + */ |
| 115 | + copy( source ) { |
| 116 | + |
| 117 | + this.blending = source.blending; |
| 118 | + this.blendSrc = source.blendSrc; |
| 119 | + this.blendDst = source.blendDst; |
| 120 | + this.blendEquation = source.blendEquation; |
| 121 | + this.blendSrcAlpha = source.blendSrcAlpha; |
| 122 | + this.blendDstAlpha = source.blendDstAlpha; |
| 123 | + this.blendEquationAlpha = source.blendEquationAlpha; |
| 124 | + this.premultiplyAlpha = source.premultiplyAlpha; |
| 125 | + |
| 126 | + return this; |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Returns a clone of this blending configuration. |
| 132 | + * |
| 133 | + * @return {BlendMode} A new Blending instance with the same properties. |
| 134 | + */ |
| 135 | + clone() { |
| 136 | + |
| 137 | + return new this.constructor().copy( this ); |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | +} |
| 142 | + |
| 143 | +export default BlendMode; |
0 commit comments