Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: material #1252

Merged
merged 2 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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