Skip to content

Commit

Permalink
refactor: opt code
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoLei1990 committed Dec 26, 2024
1 parent 0a776c4 commit ce75450
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 51 deletions.
106 changes: 57 additions & 49 deletions packages/core/src/material/BaseMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ export class BaseMaterial extends Material {
protected static _emissiveColorProp: ShaderProperty = ShaderProperty.getByName("material_EmissiveColor");
protected static _emissiveTextureProp: ShaderProperty = ShaderProperty.getByName("material_EmissiveTexture");

private static _alphaCutoffProp: ShaderProperty = ShaderProperty.getByName("material_AlphaCutoff");
protected static _alphaCutoffProp: ShaderProperty = ShaderProperty.getByName("material_AlphaCutoff");
private static _alphaCutoffMacro: ShaderMacro = ShaderMacro.getByName("MATERIAL_IS_ALPHA_CUTOFF");

private _renderFace: RenderFace = RenderFace.Front;
private _isTransparent: boolean = false;
protected _isTransparent: boolean = false;
private _blendMode: BlendMode = BlendMode.Normal;

/**
Expand Down Expand Up @@ -73,24 +73,7 @@ export class BaseMaterial extends Material {
}

set isTransparent(value: boolean) {
if (value !== this._isTransparent) {
this.setIsTransparent(0, value);

const { shaderData } = this;
if (value) {
// Use alpha test queue to simulate transparent shadow
shaderData.setFloat(BaseMaterial._shadowCasterRenderQueueProp, RenderQueueType.AlphaTest);
} else {
const alphaCutoff = shaderData.getFloat(BaseMaterial._alphaCutoffProp);
if (alphaCutoff) {
shaderData.setFloat(BaseMaterial._shadowCasterRenderQueueProp, RenderQueueType.AlphaTest);
} else {
shaderData.setFloat(BaseMaterial._shadowCasterRenderQueueProp, RenderQueueType.Opaque);
}
}

this._isTransparent = value;
}
this._seIsTransparent(value);
}

/**
Expand Down Expand Up @@ -119,35 +102,7 @@ export class BaseMaterial extends Material {
}

set alphaCutoff(value: number) {
const { shaderData } = this;
if (shaderData.getFloat(BaseMaterial._alphaCutoffProp) !== value) {
if (value) {
shaderData.enableMacro(BaseMaterial._alphaCutoffMacro);
shaderData.setFloat(BaseMaterial._shadowCasterRenderQueueProp, RenderQueueType.AlphaTest);
} else {
shaderData.disableMacro(BaseMaterial._alphaCutoffMacro);
if (this._isTransparent) {
shaderData.setFloat(BaseMaterial._shadowCasterRenderQueueProp, RenderQueueType.AlphaTest);
} else {
shaderData.setFloat(BaseMaterial._shadowCasterRenderQueueProp, RenderQueueType.Opaque);
}
}

const { renderStates } = this;
for (let i = 0, n = renderStates.length; i < n; i++) {
const renderState = renderStates[i];
if (value > 0) {
renderState.renderQueueType = renderState.blendState.targetBlendState.enabled
? RenderQueueType.Transparent
: RenderQueueType.AlphaTest;
} else {
renderState.renderQueueType = renderState.blendState.targetBlendState.enabled
? RenderQueueType.Transparent
: RenderQueueType.Opaque;
}
}
shaderData.setFloat(BaseMaterial._alphaCutoffProp, value);
}
this._setAlphaCutoff(value);
}

/**
Expand Down Expand Up @@ -279,4 +234,57 @@ export class BaseMaterial extends Material {
target._isTransparent = this._isTransparent;
target._blendMode = this._blendMode;
}

protected _seIsTransparent(value: boolean): void {
if (value !== this._isTransparent) {
this.setIsTransparent(0, value);

const { shaderData } = this;
if (value) {
// Use alpha test queue to simulate transparent shadow
shaderData.setFloat(BaseMaterial._shadowCasterRenderQueueProp, RenderQueueType.AlphaTest);
} else {
const alphaCutoff = shaderData.getFloat(BaseMaterial._alphaCutoffProp);
if (alphaCutoff) {
shaderData.setFloat(BaseMaterial._shadowCasterRenderQueueProp, RenderQueueType.AlphaTest);
} else {
shaderData.setFloat(BaseMaterial._shadowCasterRenderQueueProp, RenderQueueType.Opaque);
}
}

Check warning on line 253 in packages/core/src/material/BaseMaterial.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/BaseMaterial.ts#L247-L253

Added lines #L247 - L253 were not covered by tests

this._isTransparent = value;
}
}

protected _setAlphaCutoff(value: number): void {
const { shaderData } = this;
if (shaderData.getFloat(BaseMaterial._alphaCutoffProp) !== value) {
if (value) {
shaderData.enableMacro(BaseMaterial._alphaCutoffMacro);
shaderData.setFloat(BaseMaterial._shadowCasterRenderQueueProp, RenderQueueType.AlphaTest);
} else {
shaderData.disableMacro(BaseMaterial._alphaCutoffMacro);
if (this._isTransparent) {
shaderData.setFloat(BaseMaterial._shadowCasterRenderQueueProp, RenderQueueType.AlphaTest);
} else {
shaderData.setFloat(BaseMaterial._shadowCasterRenderQueueProp, RenderQueueType.Opaque);
}
}

Check warning on line 272 in packages/core/src/material/BaseMaterial.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/BaseMaterial.ts#L266-L272

Added lines #L266 - L272 were not covered by tests

const { renderStates } = this;
for (let i = 0, n = renderStates.length; i < n; i++) {
const renderState = renderStates[i];
if (value > 0) {
renderState.renderQueueType = renderState.blendState.targetBlendState.enabled
? RenderQueueType.Transparent
: RenderQueueType.AlphaTest;
} else {
renderState.renderQueueType = renderState.blendState.targetBlendState.enabled
? RenderQueueType.Transparent
: RenderQueueType.Opaque;
}

Check warning on line 285 in packages/core/src/material/BaseMaterial.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/BaseMaterial.ts#L282-L285

Added lines #L282 - L285 were not covered by tests
}
shaderData.setFloat(BaseMaterial._alphaCutoffProp, value);
}
}
}
19 changes: 17 additions & 2 deletions packages/core/src/material/PBRMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ShaderMacro, ShaderProperty } from "../shader";
import { Shader } from "../shader/Shader";
import { RenderQueueType } from "../shader/enums/RenderQueueType";
import { Texture2D } from "../texture/Texture2D";
import { BaseMaterial } from "./BaseMaterial";
import { PBRBaseMaterial } from "./PBRBaseMaterial";
import { RefractionMode } from "./enums/Refraction";

Expand Down Expand Up @@ -320,16 +321,30 @@ export class PBRMaterial extends PBRBaseMaterial {
}
}

/**
* @inheritdoc
*/
override get isTransparent(): boolean {
return this._isTransparent;
}

override set isTransparent(value: boolean) {
super.isTransparent = value;
this._seIsTransparent(value);
if (this.transmission > 0) {
// If transmission enabled, always use transparent queue to ensure get correct opaque texture
this.renderState.renderQueueType = RenderQueueType.Transparent;
}

Check warning on line 336 in packages/core/src/material/PBRMaterial.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L334-L336

Added lines #L334 - L336 were not covered by tests
}

/**
* @inheritdoc
*/
override get alphaCutoff(): number {
return this.shaderData.getFloat(BaseMaterial._alphaCutoffProp);
}

Check warning on line 344 in packages/core/src/material/PBRMaterial.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L343-L344

Added lines #L343 - L344 were not covered by tests

override set alphaCutoff(value: number) {
super.alphaCutoff = value;
this._setAlphaCutoff(value);
if (this.transmission > 0) {
// If transmission enabled, always use transparent queue to ensure get correct opaque texture
this.renderState.renderQueueType = RenderQueueType.Transparent;
Expand Down

0 comments on commit ce75450

Please sign in to comment.