Skip to content

Commit

Permalink
fix multiply mode sphere texture with alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
noname0310 committed May 11, 2024
1 parent a6bc88b commit 77a9b0c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.42.3

- fix multiply mode sphere texture with alpha not rendered correctly

## 0.42.2

- fix `MmdAmmoPhysics` rigidbody initialization
Expand Down
2 changes: 1 addition & 1 deletion src/Loader/mmdPluginMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ export class MmdPluginMaterial extends MaterialPluginBase {
#ifdef SPHERE_TEXTURE_BLEND_MODE_MULTIPLY
color *= sphereReflectionColor;
#elif defined(SPHERE_TEXTURE_BLEND_MODE_ADD)
color = vec4(color.rgb + sphereReflectionColor.rgb, color.a * sphereReflectionColor.a);
color = vec4(color.rgb + sphereReflectionColor.rgb, color.a);// * sphereReflectionColor.a);
#endif
#endif
`;
Expand Down
16 changes: 15 additions & 1 deletion src/Loader/mmdStandardMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Color3 } from "@babylonjs/core/Maths/math.color";
import type { Scene } from "@babylonjs/core/scene";
import type { Nullable } from "@babylonjs/core/types";

import type { MmdPluginMaterialSphereTextureBlendMode } from "./mmdPluginMaterial";
import { MmdPluginMaterialSphereTextureBlendMode } from "./mmdPluginMaterial";
import { MmdPluginMaterial } from "./mmdPluginMaterial";

/**
Expand Down Expand Up @@ -242,4 +242,18 @@ export class MmdStandardMaterial extends StandardMaterial {
}
this._renderOutline = value;
}

/**
* Specifies if the material will require alpha blending
* @returns a boolean specifying if alpha blending is needed
*/
public override needAlphaBlending(): boolean {
if (this._disableAlphaBlending) {
return false;
}

return super.needAlphaBlending() ||
(this._pluginMaterial.sphereTexture !== null &&
this._pluginMaterial.sphereTextureBlendMode === MmdPluginMaterialSphereTextureBlendMode.Multiply);
}
}
8 changes: 6 additions & 2 deletions src/Loader/mmdStandardMaterialBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,10 @@ export class MmdStandardMaterialBuilder implements IMmdMaterialBuilder {
if (materialInfo.sphereTextureMode !== PmxObject.Material.SphereTextureMode.Off) {
const sphereTexturePath = imagePathTable[textureInfo?.imagePathIndex ?? -1];
if (sphereTexturePath !== undefined) {
const format = scene.getEngine().isWebGPU || materialInfo.sphereTextureMode === PmxObject.Material.SphereTextureMode.Multiply
? Constants.TEXTUREFORMAT_RGBA
: Constants.TEXTUREFORMAT_RGB;

const sphereTextureFileFullPath = referenceFileResolver.createFullPath(sphereTexturePath);

let sphereTexture: Nullable<Texture>;
Expand All @@ -700,7 +704,7 @@ export class MmdStandardMaterialBuilder implements IMmdMaterialBuilder {
{
...textureInfo,
deleteBuffer: this.deleteTextureBufferAfterLoad,
format: scene.getEngine().isWebGPU ? Constants.TEXTUREFORMAT_RGBA : Constants.TEXTUREFORMAT_RGB,
format: format,
mimeType: file instanceof File ? file.type : file.mimeType
}
));
Expand All @@ -714,7 +718,7 @@ export class MmdStandardMaterialBuilder implements IMmdMaterialBuilder {
{
...textureInfo,
deleteBuffer: this.deleteTextureBufferAfterLoad,
format: scene.getEngine().isWebGPU ? Constants.TEXTUREFORMAT_RGBA : Constants.TEXTUREFORMAT_RGB
format: format
}
));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Test/Scene/alphaForceDepthWriteTestScene.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export class SceneBuilder implements ISceneBuilder {
materialBuilder.afterBuildSingleMaterial = (material): void => {
if (material.diffuseTexture) {
material.diffuseTexture.hasAlpha = true;
material.useAlphaFromDiffuseTexture = true;
}
material.useAlphaFromDiffuseTexture = true;
material.transparencyMode = Material.MATERIAL_ALPHABLEND;
material.forceDepthWrite = true;

Expand All @@ -59,8 +59,8 @@ export class SceneBuilder implements ISceneBuilder {
pmxLoader.buildMorph = true;
const mmdMesh = await SceneLoader.ImportMeshAsync(
undefined,
"res/private_test/model/YYB Hatsune Miku_10th/",
"YYB Hatsune Miku_10th_v1.02 - faceforward.pmx",
"res/private_test/model/YYB Hatsune Miku_NT/",
"YYB Hatsune Miku_NT_1.0ver.pmx",
scene
).then(result => result.meshes[0] as MmdMesh);
for (const mesh of mmdMesh.metadata.meshes) mesh.receiveShadows = true;
Expand Down
2 changes: 1 addition & 1 deletion src/Test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Engine } from "@babylonjs/core/Engines/engine";
// import "@babylonjs/core/Engines/WebGPU/Extensions/engine.readTexture";
// import { WebGPUEngine } from "@babylonjs/core/Engines/webgpuEngine";
import { BaseRuntime } from "./baseRuntime";
import { SceneBuilder } from "./Scene/wasmRuntimeTestScene";
import { SceneBuilder } from "./Scene/alphaForceDepthWriteTestScene";

await new Promise(resolve => window.onload = resolve);

Expand Down

0 comments on commit 77a9b0c

Please sign in to comment.