Skip to content

Commit

Permalink
pbr support transmission (#2478)
Browse files Browse the repository at this point in the history
* feat: shaderlab pbr support transmission
  • Loading branch information
hhhhkrx authored Dec 26, 2024
1 parent 90c3228 commit ab48451
Show file tree
Hide file tree
Showing 17 changed files with 494 additions and 95 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);
}
}

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);
}
}

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);
}
}
}
Loading

0 comments on commit ab48451

Please sign in to comment.