Skip to content

Commit

Permalink
compat mode blending
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl committed Jan 24, 2022
1 parent 7bb3920 commit 4829824
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
39 changes: 39 additions & 0 deletions source/inochi2d/core/nodes/part/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ enum MaskingMode {
DodgeMask
}

/**
Blending modes
TODO: Implement advanced blending system
*/
enum BlendMode {
// Normal blending mode
Normal,

// Multiply blending mode
Multiply
}

/**
Dynamic Mesh Part
*/
Expand Down Expand Up @@ -160,10 +173,22 @@ private:
partMaskShader.setUniform(mmvp, inGetCamera().matrix * transform.matrix());
partMaskShader.setUniform(mthreshold, maskAlphaThreshold);
partMaskShader.setUniform(mgopacity, opacity);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
} else {
partShader.use();
partShader.setUniform(mvp, inGetCamera().matrix * transform.matrix());
partShader.setUniform(gopacity, opacity);

// COMPAT MODE
switch(blendingMode) {
case BlendMode.Normal:
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); break;
case BlendMode.Multiply:
glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA); break;
default: assert(0);
}

// TODO: EXT MODE
}

// Bind the texture
Expand Down Expand Up @@ -233,6 +258,9 @@ protected:
serializer.arrayEnd(state);
}

serializer.putKey("blend_mode");
serializer.serializeValue(blendingMode);

if (mask.length > 0) {

serializer.putKey("mask_mode");
Expand Down Expand Up @@ -280,6 +308,9 @@ protected:
serializer.arrayEnd(state);
}

serializer.putKey("blend_mode");
serializer.serializeValue(blendingMode);

serializer.putKey("mask_mode");
serializer.serializeValue(maskingMode);

Expand Down Expand Up @@ -329,6 +360,9 @@ protected:
data["opacity"].deserializeValue(this.opacity);
data["mask_threshold"].deserializeValue(this.maskAlphaThreshold);

// Older models may not have blend mode
if (!data["blend_mode"].isEmpty) data["blend_mode"].deserializeValue(this.blendingMode);

if (!data["masked_by"].isEmpty) {
data["mask_mode"].deserializeValue(this.maskingMode);

Expand Down Expand Up @@ -362,6 +396,11 @@ public:
Masking mode
*/
MaskingMode maskingMode = MaskingMode.Mask;

/**
Blending mode
*/
BlendMode blendingMode = BlendMode.Normal;

/**
Alpha Threshold for the masking system, the higher the more opaque pixels will be discarded in the masking process
Expand Down
4 changes: 2 additions & 2 deletions source/inochi2d/core/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void inBeginScene() {
// Everything else is the actual texture used by the meshes at id 0
glActiveTexture(GL_TEXTURE0);

glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}

/**
Expand Down Expand Up @@ -155,7 +155,7 @@ void inDrawScene(vec4 area) {
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

sceneShader.use();
sceneShader.setUniform(sceneMVP,
Expand Down

0 comments on commit 4829824

Please sign in to comment.