• Three.js 材质的 blending


    Three.js 材质的 blending

    // blending modes
    export type Blending =
        | typeof NoBlending
        | typeof NormalBlending
        | typeof AdditiveBlending
        | typeof SubtractiveBlending
        | typeof MultiplyBlending
        | typeof CustomBlending;
    
    // custom blending destination factors
    export type BlendingDstFactor =
        | typeof ZeroFactor
        | typeof OneFactor
        | typeof SrcColorFactor
        | typeof OneMinusSrcColorFactor
        | typeof SrcAlphaFactor
        | typeof OneMinusSrcAlphaFactor
        | typeof DstAlphaFactor
        | typeof OneMinusDstAlphaFactor
        | typeof DstColorFactor
        | typeof OneMinusDstColorFactor;
    
    // custom blending equations
    export type BlendingEquation =
        | typeof AddEquation
        | typeof SubtractEquation
        | typeof ReverseSubtractEquation
        | typeof MinEquation
        | typeof MaxEquation;
    
    // custom blending src factors
    export const SrcAlphaSaturateFactor: 210;
    export type BlendingSrcFactor = typeof SrcAlphaSaturateFactor;
    
    // custom blending destination factors
    export type BlendingDstFactor =
        | typeof ZeroFactor
        | typeof OneFactor
        | typeof SrcColorFactor
        | typeof OneMinusSrcColorFactor
        | typeof SrcAlphaFactor
        | typeof OneMinusSrcAlphaFactor
        | typeof DstAlphaFactor
        | typeof OneMinusDstAlphaFactor
        | typeof DstColorFactor
        | typeof OneMinusDstColorFactor;
    
    class Material {
      blending: Blending;
    
      blendEquation: BlendingEquation;
      blendEquationAlpha: BlendingEquation;
    
      blendDst: BlendingDstFactor;
      blendDstAlpha: BlendingDstFactor;
    
      blendSrc: BlendingSrcFactor | BlendingDstFactor;
      blendSrcAlpha: BlendingSrcFactor | BlendingDstFactor;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    1、blending

    材质的混合模式。

    id = gl.BLEND
    
    // NoBlending
    gl.disable( id );
    
    // NormalBlending
    // AdditiveBlending
    // SubtractiveBlending
    // MultiplyBlending
    // CustomBlending
    gl.enable( id );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    2、blendEquation

    混合公式确定如何将新像素与 中 WebGLFramebuffer 已有的像素组合。

    混合方程的API:
    gl.blendEquationSeparate: 用于分别设置 RGB 混合方程和 alpha 混合方程
    gl.blendEquation: RGB 混合方程和 alpha 混合方程设置为单个方程。

    // blending:
    //      NormalBlending
    //      AdditiveBlending
    //      SubtractiveBlending
    //      MultiplyBlending
    gl.blendEquation( gl.FUNC_ADD );
    
    // blending:
    //      CustomBlending
    gl.blendEquationSeparate(
        equationToGL[ blendEquation ],
        equationToGL[ blendEquationAlpha ]
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    混合方程的值:

    const equationToGL = {
        [ AddEquation ]: gl.FUNC_ADD,
        [ SubtractEquation ]: gl.FUNC_SUBTRACT,
        [ ReverseSubtractEquation ]: gl.FUNC_REVERSE_SUBTRACT
        [ MinEquation ]: gl.MIN
        [ MaxEquation ]: gl.MAX
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    source: 接下来要画的颜色
    destination: 已经画在了帧缓冲区中的颜色

    AddEquation: source + destination
    SubtractEquation: source - destination
    ReverseSubtractEquation: destination - source
    MinEquation: Math.min(source, destination)
    MaxEquation: Math.max(source, destination)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    3、blendFunc

    用于混合像素算法的函数。

    混合函数API:
    gl.blendFunc: RGB 和 alpha 设置为单个混合函数。
    gl.blendFuncSepar: 分别混合 RGB 和 alpha 分量的混合函数。

    // 混合像素算法的函数
    // sfactor: source 混合因子
    // dfactor: destination 混合因子
    gl.blendFunc(sfactor, dfactor)
    
    // sourceColor: vec4;
    // color(RGBA) = (sourceColor * sfactor) + (destinationColor * dfactor)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    // srcRGB: source RGB 混合因子
    // dstRGB: destination RGB 混合因子
    // dstRGB: source A 混合因子
    // dstRGB: dstAlpha RGB 混合因子
    blendFuncSeparate(srcRGB, dstRGB, srcAlpha, dstAlpha)
    
    // sourceColor: vec3;
    // sourceAlpha: float;
    // color(RGB) = (sourceColor * srcRGB) + (destinationColor * dstRGB)
    // color(A) = (sourceAlpha * srcAlpha) + (destinationAlpha * dstAlpha)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    // blending = NormalBlending
    gl.blendFuncSeparate(
        gl.SRC_ALPHA,
        gl.ONE_MINUS_SRC_ALPHA,
        gl.ONE,
        gl.ONE_MINUS_SRC_ALPHA
    );
    
    // blending = AdditiveBlending
    gl.blendFunc( gl.SRC_ALPHA, gl.ONE );
    
    // blending = SubtractiveBlending
    gl.blendFuncSeparate(
        gl.ZERO,
        gl.ONE_MINUS_SRC_COLOR,
        gl.ZERO,
        gl.ONE
    );
    
    // blending = MultiplyBlending
    gl.blendFunc( gl.ZERO, gl.SRC_COLOR );
    
    // blending = CustomBlending
    gl.blendFuncSeparate(
        factorToGL[ blendSrc ],
        factorToGL[ blendDst ],
        factorToGL[ blendSrcAlpha ],
        factorToGL[ blendDstAlpha ]
    );
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    混合因子的值:

    const factorToGL = {
        [ ZeroFactor ]: gl.ZERO,
        [ OneFactor ]: gl.ONE,
    
        [ SrcColorFactor ]: gl.SRC_COLOR,
        [ SrcAlphaFactor ]: gl.SRC_ALPHA,
        [ SrcAlphaSaturateFactor ]: gl.SRC_ALPHA_SATURATE,
    
        [ DstColorFactor ]: gl.DST_COLOR,
        [ DstAlphaFactor ]: gl.DST_ALPHA,
    
        [ OneMinusSrcColorFactor ]: gl.ONE_MINUS_SRC_COLOR,
        [ OneMinusSrcAlphaFactor ]: gl.ONE_MINUS_SRC_ALPHA,
        [ OneMinusDstColorFactor ]: gl.ONE_MINUS_DST_COLOR,
        [ OneMinusDstAlphaFactor ]: gl.ONE_MINUS_DST_ALPHA
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    R S , G S , B S , A S R_S, G_S, B_S, A_S RS,GS,BS,AS, source 的 RGBA.
    R D , G D , B D , A D R_D, G_D, B_D, A_D RD,GD,BD,AD, destination 的 RGBA.

    FactorRGBA
    Zero ( 0 , 0 , 0 ) (0,0,0) (0,0,0)0
    One ( 1 , 1 , 1 ) (1,1,1) (1,1,1)1
    SrcColor ( R S , G S , B S ) (R_S, G_S, B_S) (RS,GS,BS) A S A_S AS
    SrcAlpha ( A S , A S , A S ) (A_S, A_S, A_S) (AS,AS,AS) A S A_S AS
    SrcAlphaSaturate ( f , f , f ) ; f = m i n ( A S , 1 − A D ) (f,f,f);f=min(A_S, 1 - A_D) (f,f,f);f=min(AS,1AD) 1 1 1
    DstColor ( R D , G D , B D ) (R_D, G_D, B_D) (RD,GD,BD) A D {A_D} AD
    DstAlpha ( A D , A D , A D ) (A_D, A_D, A_D) (AD,AD,AD) A D {A_D} AD
    OneMinusSrcColor$(1,1,1) - (R_S, G_S, B_S) $ A S A_S AS
    OneMinusSrcAlpha ( 1 , 1 , 1 ) − ( A S , A S , A S ) (1,1,1) - (A_S, A_S, A_S) (1,1,1)(AS,AS,AS) 1 − A S 1-A_S 1AS
    OneMinusDstColor ( 1 , 1 , 1 ) − ( R D , G D , B D ) (1,1,1) - (R_D, G_D, B_D) (1,1,1)(RD,GD,BD) 1 − A D 1-A_D 1AD
    OneMinusDstAlpha ( 1 , 1 , 1 ) − ( A D , A D , A D ) (1,1,1) - (A_D, A_D, A_D) (1,1,1)(AD,AD,AD) 1 − A D 1-A_D 1AD
    4、live examples

    WebGL “port” of this.

    gl.blendFunc()
    gl.blendFuncSeparate()

  • 相关阅读:
    【HTML学生作业网页】基于HTML+CSS+JavaScript仿南京师范大学泰州学院(11页)
    高质量项目管理-甘特图模板+教程(附下载包)/ PMP项目管理可用
    Mysql的分布式事务原理理解
    MongoDB学习(一)
    【C++基础】函数指针
    架构师的 36 项修炼第07讲:高性能系统架构设计
    我的创作纪念日
    wgcloud怎么保证数据的安全性
    【ManageEngine】加强企业特权访问安全性的7个方法
    SpringBoot实践(二十六):Security实现Vue-Element-Admin登录拦截
  • 原文地址:https://blog.csdn.net/qq_21476953/article/details/134052694