From 67d43c463bd561f912c5d6a2a33643874aa35695 Mon Sep 17 00:00:00 2001 From: Yuki Shimada Date: Sat, 25 Mar 2023 00:00:26 +0900 Subject: [PATCH 1/2] feat: Add Blend.ts --- src/foundation/definitions/Blend.ts | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/foundation/definitions/Blend.ts diff --git a/src/foundation/definitions/Blend.ts b/src/foundation/definitions/Blend.ts new file mode 100644 index 000000000..385a007f3 --- /dev/null +++ b/src/foundation/definitions/Blend.ts @@ -0,0 +1,40 @@ +import { EnumClass, EnumIO, _from } from '../misc/EnumIO'; + +export type BlendEnum = EnumIO; + +class BlendClass extends EnumClass implements BlendEnum { + constructor({ index, str }: { index: number; str: string }) { + super({ index, str }); + } +} + +const EquationFuncAdd: BlendEnum = new BlendClass({ + index: 32774, + str: 'Equation_FUNC_ADD', +}); +const One: BlendEnum = new BlendClass({ + index: 0x1, + str: 'ONE', +}); +const SrcAlpha: BlendEnum = new BlendClass({ + index: 770, + str: 'ONE_ALPHA', +}); +const OneMinusSrcAlpha: BlendEnum = new BlendClass({ + index: 771, + str: 'ONE_MINUS_SRC_ALPHA', +}); + +const typeList = [EquationFuncAdd, One, SrcAlpha, OneMinusSrcAlpha]; + +function from(index: number): BlendEnum { + return _from({ typeList, index }) as BlendEnum; +} + +export const Blend = Object.freeze({ + EquationFuncAdd, + One, + SrcAlpha, + OneMinusSrcAlpha, + from, +}); From 7bcbf5c9b0969bc9d5a3a4055c75f60d75b5430c Mon Sep 17 00:00:00 2001 From: Yuki Shimada Date: Sun, 2 Apr 2023 23:04:25 +0900 Subject: [PATCH 2/2] refactor: __blendEquationMode and __blendEquationModeAlpha --- src/foundation/materials/contents/MToonMaterialContent.ts | 6 +++++- src/foundation/materials/core/Material.ts | 7 ++++--- src/webgl/WebGLStrategyCommonMethod.ts | 6 +++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/foundation/materials/contents/MToonMaterialContent.ts b/src/foundation/materials/contents/MToonMaterialContent.ts index ed1dba25f..ecd71194a 100644 --- a/src/foundation/materials/contents/MToonMaterialContent.ts +++ b/src/foundation/materials/contents/MToonMaterialContent.ts @@ -24,6 +24,7 @@ import { RenderingArg } from '../../../webgl/types/CommonTypes'; import { ShaderSemanticsInfo } from '../../definitions/ShaderSemanticsInfo'; import { Vrm0xMaterialProperty } from '../../../types'; import { Sampler } from '../../textures/Sampler'; +import { Blend } from '../../definitions/Blend'; export class MToonMaterialContent extends AbstractMaterialContent { static readonly _Cutoff = new ShaderSemanticsClass({ str: 'cutoff' }); @@ -806,7 +807,10 @@ export class MToonMaterialContent extends AbstractMaterialContent { this.__floatProperties._DstBlend ); - material.setBlendEquationMode(blendEquationMode, blendEquationModeAlpha); + material.setBlendEquationMode( + Blend.from(blendEquationMode), + blendEquationModeAlpha != null ? Blend.from(blendEquationModeAlpha) : undefined + ); material.setBlendFuncFactor(blendFuncSrcFactor, blendFuncDstFactor); } diff --git a/src/foundation/materials/core/Material.ts b/src/foundation/materials/core/Material.ts index fa0fd8475..aaa5ca19b 100644 --- a/src/foundation/materials/core/Material.ts +++ b/src/foundation/materials/core/Material.ts @@ -38,6 +38,7 @@ import { GL_FUNC_ADD, GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA } from '../.. import { ShaderSemanticsInfo, VertexAttributeEnum } from '../../definitions'; import { MaterialTypeName, ShaderVariable } from './MaterialTypes'; import { Sampler } from '../../textures/Sampler'; +import { Blend, BlendEnum } from '../../definitions/Blend'; /** * The material class. @@ -63,8 +64,8 @@ export class Material extends RnObject { public cullFace = true; // If true, enable gl.CULL_FACE public cullFrontFaceCCW = true; private __alphaToCoverage = false; - private __blendEquationMode = GL_FUNC_ADD; // gl.FUNC_ADD - private __blendEquationModeAlpha = GL_FUNC_ADD; // gl.FUNC_ADD + private __blendEquationMode = Blend.EquationFuncAdd; // gl.FUNC_ADD + private __blendEquationModeAlpha = Blend.EquationFuncAdd; // gl.FUNC_ADD private __blendFuncSrcFactor = GL_SRC_ALPHA; // gl.SRC_ALPHA private __blendFuncDstFactor = GL_ONE_MINUS_SRC_ALPHA; // gl.ONE_MINUS_SRC_ALPHA private __blendFuncAlphaSrcFactor = GL_ONE; // gl.ONE @@ -650,7 +651,7 @@ export class Material extends RnObject { * @param blendEquationMode the argument of gl.blendEquation of the first argument of gl.blendEquationSeparate such as gl.FUNC_ADD * @param blendEquationModeAlpha the second argument of gl.blendEquationSeparate */ - public setBlendEquationMode(blendEquationMode: number, blendEquationModeAlpha?: number) { + public setBlendEquationMode(blendEquationMode: BlendEnum, blendEquationModeAlpha?: BlendEnum) { this.__blendEquationMode = blendEquationMode; this.__blendEquationModeAlpha = blendEquationModeAlpha ?? blendEquationMode; } diff --git a/src/webgl/WebGLStrategyCommonMethod.ts b/src/webgl/WebGLStrategyCommonMethod.ts index bd25c04fc..1f5cc7a29 100644 --- a/src/webgl/WebGLStrategyCommonMethod.ts +++ b/src/webgl/WebGLStrategyCommonMethod.ts @@ -73,7 +73,11 @@ function setBlendSettings(material: Material, gl: WebGLRenderingContext) { } if (material.alphaMode === AlphaMode.Translucent) { - setBlendEquationMode(material.blendEquationMode, material.blendEquationModeAlpha, gl); + setBlendEquationMode( + material.blendEquationMode.index, + material.blendEquationModeAlpha.index, + gl + ); setBlendFuncSrcFactor( material.blendFuncSrcFactor, material.blendFuncDstFactor,