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 1 commit
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
87 changes: 86 additions & 1 deletion 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 static _sheenProp = ShaderProperty.getByName("material_Sheen");
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 @@ -222,6 +228,84 @@
}
}

/**
* The sheen layer intensity, from 0.0 to 1.0.
* @defaultValue `0`
*/
get sheen(): number {
return this.shaderData.getFloat(PBRMaterial._sheenProp);
zhuxudong marked this conversation as resolved.
Show resolved Hide resolved
}

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L236-L237

Added lines #L236 - L237 were not covered by tests

set sheen(value: number) {
if (!!this.shaderData.getFloat(PBRMaterial._sheenProp) !== !!value) {
if (value === 0) {
this.shaderData.disableMacro("MATERIAL_ENABLE_SHEEN");
} else {
this.shaderData.enableMacro("MATERIAL_ENABLE_SHEEN");
zhuxudong marked this conversation as resolved.
Show resolved Hide resolved
}
}
this.shaderData.setFloat(PBRMaterial._sheenProp, value);
}

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

View check run for this annotation

Codecov / codecov/patch

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

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

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

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

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

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#L259-L263

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

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 271 in packages/core/src/material/PBRMaterial.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L270-L271

Added lines #L270 - L271 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 275 in packages/core/src/material/PBRMaterial.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L274-L275

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

/**
* Sheen color texture, multiply ‘sheencolor’.
zhuxudong marked this conversation as resolved.
Show resolved Hide resolved
*/
get sheenColorTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(PBRMaterial._sheenTextureProp);
}

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

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L281-L282

Added lines #L281 - L282 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 291 in packages/core/src/material/PBRMaterial.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L285-L291

Added lines #L285 - L291 were not covered by tests

/**
* Sheen roughness texture, 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 298 in packages/core/src/material/PBRMaterial.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L297-L298

Added lines #L297 - L298 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 307 in packages/core/src/material/PBRMaterial.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/material/PBRMaterial.ts#L301-L307

Added lines #L301 - L307 were not covered by tests

/**
* Create a pbr metallic-roughness workflow material instance.
* @param engine - Engine to which the material belongs
Expand All @@ -235,6 +319,7 @@
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(1, 1, 1));
zhuxudong marked this conversation as resolved.
Show resolved Hide resolved
// @ts-ignore
this._iridescenceRange._onValueChanged = this._onIridescenceRangeChanged.bind(this);
}
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 = new Color(
Color.linearToGammaSpace(sheenColorFactor[0]),
Color.linearToGammaSpace(sheenColorFactor[1]),
Color.linearToGammaSpace(sheenColorFactor[2]),
sheenColorFactor[3]
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