8000 metallic roughness workflow support specular by hhhhkrx · Pull Request #2756 · galacean/engine · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

metallic roughness workflow support specular #2756

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

Open
wants to merge 6 commits into
base: dev/1.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
115 changes: 115 additions & 0 deletions packages/core/src/material/PBRMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,21 @@ export class PBRMaterial extends PBRBaseMaterial {
private static _thicknessProp = ShaderProperty.getByName("material_Thickness");
private static _thicknessTextureProp = ShaderProperty.getByName("material_ThicknessTexture");

private static _specularMacro: ShaderMacro = ShaderMacro.getByName("MATERIAL_ENABLE_SPECULAR");
private static _specularTextureMacro = ShaderMacro.getByName("MATERIAL_ENABLE_SPECULAR_TEXTURE");
private static _specularColorTextureMacro = ShaderMacro.getByName("MATERIAL_ENABLE_SPECULAR_COLOR_TEXTURE");
private static _specularProp = ShaderProperty.getByName("material_Specular");
private static _specularColorProp = ShaderProperty.getByName("material_SpecularColor");
private static _specularTextureProp = ShaderProperty.getByName("material_SpecularTexture");
private static _specularColorTextureProp = ShaderProperty.getByName("material_SpecularColorTexture");
private static _reflectanceProp = ShaderProperty.getByName("material_Reflectance");

private _refractionMode: RefractionMode;
private _anisotropyRotation: number = 0;
private _iridescenceRange = new Vector2(100, 400);
private _sheenEnabled = false;
private _specularEnabled = false;
private _specularColorEnabled = false;

/**
* Index Of Refraction.
Expand All @@ -56,6 +67,7 @@ export class PBRMaterial extends PBRBaseMaterial {

set ior(v: number) {
this.shaderData.setFloat(PBRMaterial._iorProp, Math.max(v, 0));
this._updateReflectance();
}

/**
Expand Down Expand Up @@ -451,6 +463,78 @@ export class PBRMaterial extends PBRBaseMaterial {
this.shaderData.disableMacro(PBRMaterial._thicknessTextureMacro);
}
}

/**
* The strength of the specular reflection.
* @defaultValue `1.0`
*/

get specular(): number {
return this.shaderData.getFloat(PBRMaterial._specularProp);
}

set specular(value: number) {
this.shaderData.setFloat(PBRMaterial._specularProp, value);
const enableSpecular = value > 0;
if (enableSpecular !== this._specularEnabled) {
this._specularEnabled = enableSpecular;
if (enableSpecular) {
this.shaderData.enableMacro(PBRMaterial._specularMacro);
} else {
this.shaderData.disableMacro(PBRMaterial._specularMacro);
}
}
this._updateReflectance();
}

/**
* The specular color.
* @defaultValue `[1,1,1]`
*/
get specularColor(): Color {
return this.shaderData.getColor(PBRMaterial._specularColorProp);
}
set specularColor(value: Color) {
const specularColor = this.shaderData.getColor(PBRMaterial._specularColorProp);
if (value !== specularColor) {
specularColor.copyFrom(value);
}
}

/**
* Specular texture.
* @remarks A channel will be multiplied by `specular`.
*/
get specularTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(PBRMaterial._specularTextureProp);
}

set specularTexture(value: Texture2D) {
this.shaderData.setTexture(PBRMaterial._specularTextureProp, value);
if (value) {
this.shaderData.enableMacro(PBRMaterial._specularTextureMacro);
} else {
this.shaderData.disableMacro(PBRMaterial._specularTextureMacro);
}
}

/**
* Specular color texture.
* @remarks The intensity of Specular color, multiplied by `specularColor`.
*/
get specularColorTexture(): Texture2D {
return <Texture2D>this.shaderData.getTexture(PBRMaterial._specularColorTextureProp);
}

set specularColorTexture(value: Texture2D) {
this.shaderData.setTexture(PBRMaterial._specularColorTextureProp, value);
if (value) {
this.shaderData.enableMacro(PBRMaterial._specularColorTextureMacro);
} else {
this.shaderData.disableMacro(PBRMaterial._specularColorTextureMacro);
}
}

/**
* Create a pbr metallic-roughness workflow material instance.
* @param engine - Engine to which the material belongs
Expand All @@ -472,11 +556,17 @@ export class PBRMaterial extends PBRBaseMaterial {
shaderData.setFloat(PBRMaterial._attenuationDistanceProp, Infinity);
const attenuationColor = new Color(1, 1, 1);
shaderData.setColor(PBRMaterial._attenuationColorProp, attenuationColor);
shaderData.setFloat(PBRMaterial._specularProp, 1);
const specularColor = new Color(1, 1, 1);
shaderData.setColor(PBRMaterial._specularColorProp, specularColor);
shaderData.setFloat(PBRMaterial._reflectanceProp, 0.5);

// @ts-ignore
this._iridescenceRange._onValueChanged = this._onIridescenceRangeChanged.bind(this);
// @ts-ignore
sheenColor._onValueChanged = this._onSheenColorChanged.bind(this);
// @ts-ignore
specularColor._onValueChanged = this._onSpecularColorChanged.bind(this);
}

/**
Expand Down Expand Up @@ -506,4 +596,29 @@ export class PBRMaterial extends PBRBaseMaterial {
}
}
}

private _onSpecularColorChanged(): void {
const specularColor = this.specularColor;
const enableSpecularColor = specularColor.r + specularColor.g + specularColor.b > 0;
if (enableSpecularColor !== this._specularColorEnabled) {
this._specularColorEnabled = enableSpecularColor;
if (enableSpecularColor) {
this.shaderData.enableMacro("MATERIAL_ENABLE_SPECULAR_COLOR");
} else {
this.shaderData.disableMacro("MATERIAL_ENABLE_SPECULAR_COLOR");
}
}
}

private _updateReflectance(): void {
let reflectance: number;
if (this.specular) {
reflectance = this.shaderData.getFloat(PBRMaterial._reflectanceProp);
} else {
const ior = this.ior;
const f0 = (ior - 1.0) / (ior + 1.0);
reflectance = Math.sqrt((f0 * f0) / 0.16);
}
this.shaderData.setFloat(PBRMaterial._reflectanceProp, reflectance);
}
}
81 changes: 0 additions & 81 deletions packages/core/src/material/PBRSpecularMaterial.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/src/material/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ export { TextureCoordinate } from "./enums/TextureCoordinate";
export { Material } from "./Material";
export { PBRBaseMaterial } from "./PBRBaseMaterial";
export { PBRMaterial } from "./PBRMaterial";
export { PBRSpecularMaterial } from "./PBRSpecularMaterial";
export { UnlitMaterial } from "./UnlitMaterial";
export { RefractionMode } from "./enums/Refraction";
19 changes: 4 additions & 15 deletions packages/loader/src/gltf/extensions/GLTFExtensionSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ export interface IKHRMaterialsIor {
*/
export interface IKHRMaterialsUnlit {}

/**
* Interfaces from the KHR_materials_pbrSpecularGlossiness extension
*/
export interface IKHRMaterialsPbrSpecularGlossiness {
diffuseFactor: number[];
diffuseTexture: ITextureInfo;
specularFactor: number[];
glossinessFactor: number;
specularGlossinessTexture: ITextureInfo;
}

/**
* Interfaces from the KHR_materials_sheen extension
*/
Expand All @@ -70,9 +59,10 @@ export interface IKHRMaterialsSheen {
* Interfaces from the KHR_materials_specular extension
*/
export interface IKHRMaterialsSpecular {
specularFactor: number;
specularColorFactor: number[];
specularTexture: ITextureInfo;
specularFactor?: number;
specularColorFactor?: number[];
specularTexture?: ITextureInfo;
specularColorTexture?: ITextureInfo;
}

/**
Expand Down Expand Up @@ -203,7 +193,6 @@ export type GLTFExtensionSchema =
| IKHRMaterialsClearcoat
| IKHRMaterialsIor
| IKHRMaterialsUnlit
| IKHRMaterialsPbrSpecularGlossiness
| IKHRMaterialsSheen
| IKHRMaterialsSpecular
| IKHRMaterialsTransmission
Expand Down

This file was deleted.

45 changes: 45 additions & 0 deletions packages/loader/src/gltf/extensions/KHR_materials_specular.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Logger, PBRMaterial, Texture2D } from "@galacean/engine-core";
import { GLTFMaterialParser } from "../parser/GLTFMaterialParser";
import { registerGLTFExtension } from "../parser/GLTFParser";
import { GLTFParserContext, GLTFParserType } from "../parser/GLTFParserContext";
import { GLTFExtensionMode, GLTFExtensionParser } from "./GLTFExtensionParser";
import { IKHRMaterialsSpecular } from "./GLTFExtensionSchema";

@registerGLTFExtension("KHR_materials_specular", GLTFExtensionMode.CreateAndParse)
class KHR_materials_specular extends GLTFExtensionParser {
override additiveParse(context: GLTFParserContext, material: PBRMaterial, schema: IKHRMaterialsSpecular): void {
const { specularFactor, specularTexture, specularColorFactor, specularColorTexture } = schema;

material.specular = specularFactor;
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add null check for specularFactor

The specularFactor could be undefined since it's optional in the schema. Add a null check before assignment.

-    material.specular = specularFactor;
+    if (specularFactor !== undefined) {
+      material.specular = specularFactor;
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
material.specular = specularFactor;
if (specularFactor !== undefined) {
material.specular = specularFactor;
}
🤖 Prompt for AI Agents
In packages/loader/src/gltf/extensions/KHR_materials_specular.ts at line 13, the
assignment of specularFactor to material.specular lacks a null check. Add a
condition to verify that specularFactor is not null or undefined before
assigning it to material.specular to prevent potential runtime errors.


if (specularColorFactor) {
material.specularColor.set(specularColorFactor[0], specularColorFactor[1], specularColorFactor[2], undefined);
}

if (specularTexture) {
GLTFMaterialParser._checkOtherTextureTransform(specularTexture, "Specular texture");

context
.get<Texture2D>(GLTFParserType.Texture, specularTexture.index)
.then((texture) => {
material.specularTexture = texture;
})
.catch((e) => {
Logger.error("KHR_materials_specular: specularTexture error", e);
});
}

if (specularColorTexture) {
GLTFMaterialParser._checkOtherTextureTransform(specularColorTexture, "SpecularColor texture");

context
.get<Texture2D>(GLTFParserType.Texture, specularColorTexture.index)
.then((texture) => {
material.specularColorTexture = texture;
})
.catch((e) => {
Logger.error("KHR_materials_specular: SpecularColorTexture error", e);
});
}
}
}
Loading
Loading
0