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

PBR support sheen #2448

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
89 changes: 87 additions & 2 deletions packages/core/src/material/PBRMaterial.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MathUtil, Vector2, Vector3, Vector4 } from "@galacean/engine-math";
import { MathUtil, Vector2, Vector3, Vector4, Color } from "@galacean/engine-math";
import { Engine } from "../Engine";
import { ShaderProperty } from "../shader";
import { Shader } from "../shader/Shader";
Expand All @@ -25,6 +25,12 @@
private static _iridescenceTextureProp = ShaderProperty.getByName("material_IridescenceTexture");
private _iridescenceRange = new Vector2(100, 400);

private _sheenEnabled = false;
private static _sheenColorProp = ShaderProperty.getByName("material_SheenColor");
private static _sheenRoughnessProp = ShaderProperty.getByName("material_SheenRoughness");
private static _sheenTextureProp = ShaderProperty.getByName("material_SheenTexture");
private static _sheenRoughnessTextureProp = ShaderProperty.getByName("material_SheenRoughnessTexture");

/**
* Index Of Refraction.
* @defaultValue `1.5`
Expand Down Expand Up @@ -190,7 +196,7 @@

/**
* The range of iridescence thickness, x is minimum, y is maximum.
* @defaultValue `[100, 400]`.
* @defaultValue `[100, 400]`
*/
get iridescenceThicknessRange(): Vector2 {
return this._iridescenceRange;
Expand Down Expand Up @@ -222,6 +228,66 @@
}
}

/**
* Sheen color.
* @defaultValue `[0,0,0]`
*/
get sheenColor(): Color {
return this.shaderData.getColor(PBRMaterial._sheenColorProp);
}

set sheenColor(value: Color) {
const sheenColor = this.shaderData.getColor(PBRMaterial._sheenColorProp);
if (value !== sheenColor) {
sheenColor.copyFrom(value);
}
}

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L240-L244

Added lines #L240 - L244 were not covered by tests

zhuxudong marked this conversation as resolved.
Show resolved Hide resolved
/**
* Sheen roughness, from 0.0 to 1.0.
* @defaultValue `0.0`
*/
get sheenRoughness(): number {
return this.shaderData.getFloat(PBRMaterial._sheenRoughnessProp);
}

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L251-L252

Added lines #L251 - L252 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe should add value = Math.max(0, Math.min(1, value)); if you want clamp the range like comments


set sheenRoughness(value: number) {
this.shaderData.setFloat(PBRMaterial._sheenRoughnessProp, value);
}

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L255-L256

Added lines #L255 - L256 were not covered by tests
zhuxudong marked this conversation as resolved.
Show resolved Hide resolved

/**
* SheenColor texture, multiply ‘sheenColor’.
*/
get sheenColorTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(PBRMaterial._sheenTextureProp);
}

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L262-L263

Added lines #L262 - L263 were not covered by tests

set sheenColorTexture(value: Texture2D) {
this.shaderData.setTexture(PBRMaterial._sheenTextureProp, value);
if (value) {
this.shaderData.enableMacro("MATERIAL_HAS_SHEEN_TEXTURE");
} else {
this.shaderData.disableMacro("MATERIAL_HAS_SHEEN_TEXTURE");
}
}

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L266 - L272 were not covered by tests

/**
* SheenRoughness texture.
* @remarks Use alpha channel, and multiply 'sheenRoughness'.
*/
zhuxudong marked this conversation as resolved.
Show resolved Hide resolved
get sheenRoughnessTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(PBRMaterial._sheenRoughnessTextureProp);
}

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L279-L280

Added lines #L279 - L280 were not covered by tests

set sheenRoughnessTexture(value: Texture2D) {
this.shaderData.setTexture(PBRMaterial._sheenRoughnessTextureProp, value);
if (value) {
this.shaderData.enableMacro("MATERIAL_HAS_SHEEN_ROUGHNESS_TEXTURE");
} else {
this.shaderData.disableMacro("MATERIAL_HAS_SHEEN_ROUGHNESS_TEXTURE");
}
}

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L283-L289

Added lines #L283 - L289 were not covered by tests

/**
* Create a pbr metallic-roughness workflow material instance.
* @param engine - Engine to which the material belongs
Expand All @@ -235,8 +301,27 @@
shaderData.setFloat(PBRMaterial._iorProp, 1.5);
shaderData.setVector3(PBRMaterial._anisotropyInfoProp, new Vector3(1, 0, 0));
shaderData.setVector4(PBRMaterial._iridescenceInfoProp, new Vector4(0, 1.3, 100, 400));
shaderData.setColor(PBRMaterial._sheenColorProp, new Color(0, 0, 0));
// @ts-ignore
this._iridescenceRange._onValueChanged = this._onIridescenceRangeChanged.bind(this);
// @ts-ignore
this.sheenColor._onValueChanged = () => {
const r = this.sheenColor.r;
const g = this.sheenColor.g;
const b = this.sheenColor.b;
/**
* The sheen layer switch.
*/
const enableSheen = r + g + b > 0;
if (enableSheen !== this._sheenEnabled) {
this._sheenEnabled = enableSheen;
if (enableSheen) {
this.shaderData.enableMacro("MATERIAL_ENABLE_SHEEN");
} else {
this.shaderData.disableMacro("MATERIAL_ENABLE_SHEEN");
}
}

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L317-L323

Added lines #L317 - L323 were not covered by tests
};
}

private _onIridescenceRangeChanged(): void {
Expand Down
41 changes: 41 additions & 0 deletions packages/loader/src/gltf/extensions/KHR_materials_sheen.ts
Original file line number Diff line number Diff line change
@@ -1,0 +1,41 @@
import { PBRMaterial, Texture2D } from "@galacean/engine-core";
import { Color } from "@galacean/engine-math";
import { GLTFMaterialParser } from "../parser/GLTFMaterialParser";
import { registerGLTFExtension } from "../parser/GLTFParser";
import { GLTFParserContext, GLTFParserType } from "../parser/GLTFParserContext";
import { GLTFExtensionMode, GLTFExtensionParser } from "./GLTFExtensionParser";
import { IKHRMaterialsSheen } from "./GLTFExtensionSchema";

@registerGLTFExtension("KHR_materials_sheen", GLTFExtensionMode.AdditiveParse)
class KHR_materials_sheen extends GLTFExtensionParser {
override additiveParse(context: GLTFParserContext, material: PBRMaterial, schema: IKHRMaterialsSheen): void {
const { sheenColorFactor, sheenColorTexture, sheenRoughnessFactor, sheenRoughnessTexture } = schema;

if (sheenColorFactor) {
material.sheenColor.set(
Color.linearToGammaSpace(sheenColorFactor[0]),
Color.linearToGammaSpace(sheenColorFactor[1]),
Color.linearToGammaSpace(sheenColorFactor[2]),
undefined
);
}
zhuxudong marked this conversation as resolved.
Show resolved Hide resolved

material.sheenRoughness = sheenRoughnessFactor;
zhuxudong marked this conversation as resolved.
Show resolved Hide resolved

if (sheenColorTexture) {
GLTFMaterialParser._checkOtherTextureTransform(sheenColorTexture, "Sheen texture");

context.get<Texture2D>(GLTFParserType.Texture, sheenColorTexture.index).then((texture) => {
material.sheenColorTexture = texture;
});
}

if (sheenRoughnessTexture) {
GLTFMaterialParser._checkOtherTextureTransform(sheenRoughnessTexture, "SheenRoughness texture");

context.get<Texture2D>(GLTFParserType.Texture, sheenRoughnessTexture.index).then((texture) => {
material.sheenRoughnessTexture = texture;
});
}
}

Check warning on line 40 in packages/loader/src/gltf/extensions/KHR_materials_sheen.ts

View check run for this annotation

Codecov / codecov/patch

packages/loader/src/gltf/extensions/KHR_materials_sheen.ts#L12-L40

Added lines #L12 - L40 were not covered by tests
}
Loading