首页 > 其他分享 > Three.js 材质的 blending

Three.js 材质的 blending

时间:2023-10-26 11:56:03浏览次数:35  
标签:SRC destination Three js 混合 typeof blending gl

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、blending

材质的混合模式。

id = gl.BLEND

// NoBlending
gl.disable( id );

// NormalBlending
// AdditiveBlending
// SubtractiveBlending
// MultiplyBlending
// CustomBlending
gl.enable( id );

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 ]
);

混合方程的值:

const equationToGL = {
    [ AddEquation ]: gl.FUNC_ADD,
    [ SubtractEquation ]: gl.FUNC_SUBTRACT,
    [ ReverseSubtractEquation ]: gl.FUNC_REVERSE_SUBTRACT
    [ MinEquation ]: gl.MIN
    [ MaxEquation ]: gl.MAX
};

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

AddEquation: source + destination
SubtractEquation: source - destination
ReverseSubtractEquation: destination - source
MinEquation: Math.min(source, destination)
MaxEquation: Math.max(source, destination)

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)
// 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)
// 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 ]
);

混合因子的值:

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
};

$R_S, G_S, B_S, A_S$, source 的 RGBA.
$R_D, G_D, B_D, A_D$, destination 的 RGBA.

Factor RGB A
Zero $(0,0,0)$ 0
One $(1,1,1)$ 1
SrcColor $(R_S, G_S, B_S)$ $A_S$
SrcAlpha $(A_S, A_S, A_S)$ $A_S$
SrcAlphaSaturate $(f,f,f);f=min(A_S, 1 - A_D)$ $1$
DstColor $(R_D, G_D, B_D)$ ${A_D}$
DstAlpha $(A_D, A_D, A_D)$ ${A_D}$
OneMinusSrcColor $(1,1,1) - (R_S, G_S, B_S) $ $A_S$
OneMinusSrcAlpha $(1,1,1) - (A_S, A_S, A_S)$ $1-A_S$
OneMinusDstColor $(1,1,1) - (R_D, G_D, B_D)$ $1-A_D$
OneMinusDstAlpha $(1,1,1) - (A_D, A_D, A_D)$ $1-A_D$

4、live examples

WebGL "port" of this.

gl.blendFunc()
gl.blendFuncSeparate()

标签:SRC,destination,Three,js,混合,typeof,blending,gl
From: https://www.cnblogs.com/lianming/p/17789083.html

相关文章

  • js--深拷贝和浅拷贝
    一、栈(stack)和堆(heap)栈(stack):由操作系统自动分配释放,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈;每种数据类型占用的空间大小是固定的。堆(heap):一般由程序员分配释放,若程序员不释放,程序结束时可能由OS回收,分配方式倒是类似于链表;二、基本数据类型......
  • JS Date相关内容
    记录一些与Date有关的使用技巧、兼容问题等。1.newDate()接收参数兼容性:以下是所有浏览器都可支持的接收参数形式vard=newDate(2011,01,07);//yyyy,mm-1,ddvard=newDate(2011,01,07,11,05,00);//yyyy,mm-1,dd,hh,mm,ssvard=newDate("02......
  • Python_Json的使用总结
    应用场景json应用于批量数据进行组织管理--将无序变为有序-将输入结构数据进行组织形式标注化直观问题1.函数功能:dump在英文中有转储,转存的意思 json.dump(data,f) json.dump(data,f,indent=4)#使用缩进和换行格式化JSON dumps()dumps是dump......
  • 【2023最新10.25】全国建筑市场监管公共服务平台(四库一平台)js逆向
    目录js逆向思路第一步抓包第一种破解方法,堆栈法python还原js扣代码还原第二种破解方法,搜索法完整代码效果展示js逆向思路第一步看请求网址的发起程序都有哪些接在js文件搜索AES,MD5,等高频加密方式的字段1parse2decrypt3.toString()4Base645表单字段6url关键字......
  • jsp
    1.jsp表达式<%=  %> 等价于  ${}  就相当于输出  2.jsp的脚本片段  <%  %> 3.jsp声明 <%!  %>  4.jsp指令这两个都是一样: header和footer的内容放在网页主体的上下部分 9大内置对象主要用的4个  存东西取...转发......
  • vite中使用scss导出变量到js/ts中作为变量使用
    定义scss定义一个scss文件,注意,这里的文件名要以module.scss结尾,不然会出现读写错误的问题。$background-color_hover:#e4e8ec;$background-color_chosed:#f44343;:export{bgc_hover:$background-color_hover;bgc_chosed:$background-color_chosed;}.modu......
  • Node.js 的ORM(Sequelize) 的使用
    Sequelize是一个Node.js的ORM。什么是ORM呢?对象关系映射(ObjectRelationalMapping)。什么意思?就是在编程语言中,很容易创建对象,如果在面向对象的语言中,它也只有对象,但在关系型数据库中,它只有关系(表)。如果想在程序中操作关系型数据库,就要在对象和表之间做转换,比如,取出对象......
  • js简单动画--页面元素碰撞浏览器边框
    如题,js的简单使用。很多页面特效可以由此展开想象。html:1<!DOCTYPEhtml>2<html>3<head>4<metacharset="utf-8"/>5<title></title>6<scriptsrc="js/j1.js"></script>7......
  • 多线程指南:探究多线程在Node.js中的广泛应用
    前言最初,JavaScript是用于设计执行简单的web任务的,比如表单验证。直到2009年,Node.js的创建者RyanDahl让开发人员认识到了通过JavaScript进行后端开发已成为可能,在后端开发中,用到最多的就是多线程以及线程之间的同步功能,今天小编就为大家介绍一下如何使用Node.js实现多线程的应......
  • C#之System.Text.Json的用法
    System.Text.Json是C#中的一个JSON序列化和反序列化库,它在.NETCore3.0及更高版本中提供了内置支持。以下是System.Text.Json的用法详解:JSON序列化JSON序列化是将.NET对象转换为JSON字符串的过程。usingSystem;usingSystem.Text.Json;publicclassPerson......