Skip to content

Commit

Permalink
Merge pull request #1252 from actnwit/refactor/material
Browse files Browse the repository at this point in the history
refactor: material
  • Loading branch information
emadurandal authored Apr 7, 2023
2 parents 1474297 + 7bcbf5c commit 5a54774
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
40 changes: 40 additions & 0 deletions src/foundation/definitions/Blend.ts
Original file line number Diff line number Diff line change
@@ -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,
});
6 changes: 5 additions & 1 deletion src/foundation/materials/contents/MToonMaterialContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down Expand Up @@ -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);
}

Expand Down
7 changes: 4 additions & 3 deletions src/foundation/materials/core/Material.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion src/webgl/WebGLStrategyCommonMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 5a54774

Please sign in to comment.