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

Support ior for PBRMaterial and glTF plugin #1487

Merged
merged 4 commits into from
Apr 23, 2023
Merged
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
21 changes: 19 additions & 2 deletions packages/core/src/material/PBRMaterial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,23 @@ export class PBRMaterial extends PBRBaseMaterial {
private static _roughnessProp = Shader.getPropertyByName("u_roughness");
private static _roughnessMetallicTextureProp = Shader.getPropertyByName("u_roughnessMetallicTexture");

private static _iorProp = Shader.getPropertyByName("material_IOR");

/**
* Index Of Refraction.
* @defaultValue `1.5`
*/
get ior(): number {
return this.shaderData.getFloat(PBRMaterial._iorProp);
}

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

/**
* Metallic, default 1.0.
* Metallic.
* @defaultValue `1.0`
*/
get metallic(): number {
return this.shaderData.getFloat(PBRMaterial._metallicProp);
Expand All @@ -23,7 +38,8 @@ export class PBRMaterial extends PBRBaseMaterial {
}

/**
* Roughness, default 1.0.
* Roughness. default 1.0.
* @defaultValue `1.0`
*/
get roughness(): number {
return this.shaderData.getFloat(PBRMaterial._roughnessProp);
Expand Down Expand Up @@ -58,6 +74,7 @@ export class PBRMaterial extends PBRBaseMaterial {
super(engine, Shader.find("pbr"));
this.shaderData.setFloat(PBRMaterial._metallicProp, 1);
this.shaderData.setFloat(PBRMaterial._roughnessProp, 1);
this.shaderData.setFloat(PBRMaterial._iorProp, 1.5);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/shaderlib/pbr/pbr_frag_define.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,5 @@ struct Material {
#endif

};

uniform float material_IOR;
4 changes: 3 additions & 1 deletion packages/core/src/shaderlib/pbr/pbr_helper.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ void initMaterial(out Material material, const in Geometry geometry){
vec3 specularColor = u_PBRSpecularColor;
float glossiness = u_glossiness;
float alphaCutoff = u_alphaCutoff;
float F0 = pow2( (material_IOR - 1.0) / (material_IOR + 1.0) )


#ifdef BASETEXTURE
vec4 baseTextureColor = texture2D(u_baseTexture, v_uv);
Expand Down Expand Up @@ -90,7 +92,7 @@ void initMaterial(out Material material, const in Geometry geometry){

#ifdef IS_METALLIC_WORKFLOW
material.diffuseColor = baseColor.rgb * ( 1.0 - metal );
material.specularColor = mix( vec3( 0.04), baseColor.rgb, metal );
material.specularColor = mix( vec3(F0), baseColor.rgb, metal );
material.roughness = roughness;
#else
float specularStrength = max( max( specularColor.r, specularColor.g ), specularColor.b );
Expand Down
15 changes: 15 additions & 0 deletions packages/loader/src/gltf/extensions/KHR_materials_ior.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PBRMaterial } from "@galacean/engine-core";
import { IKHRMaterialsIor } from "./Schema";

import { registerExtension } from "../parser/Parser";
import { ParserContext } from "../parser/ParserContext";
import { ExtensionParser } from "./ExtensionParser";

@registerExtension("KHR_materials_ior")
class KHR_materials_ior extends ExtensionParser {
parseEngineResource(schema: IKHRMaterialsIor, material: PBRMaterial, context: ParserContext): void {
const { ior = 1.5 } = schema;

material.ior = ior;
}
}
1 change: 1 addition & 0 deletions packages/loader/src/gltf/extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ import "./KHR_mesh_quantization";
import "./KHR_texture_basisu";
import "./KHR_texture_transform";
import "./GALACEAN_materials_remap";
import "./KHR_materials_ior";
5 changes: 5 additions & 0 deletions packages/loader/src/gltf/parser/MaterialParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class MaterialParser extends Parser {
KHR_materials_unlit,
KHR_materials_pbrSpecularGlossiness,
KHR_materials_clearcoat,
KHR_materials_ior,
OASIS_materials_remap
} = extensions;

Expand All @@ -74,6 +75,10 @@ export class MaterialParser extends Parser {
Parser.parseEngineResource("KHR_materials_clearcoat", KHR_materials_clearcoat, material, context);
}

if (KHR_materials_ior) {
Parser.parseEngineResource("KHR_materials_ior", KHR_materials_ior, material, context);
}

if (pbrMetallicRoughness) {
const { baseColorFactor, baseColorTexture, metallicFactor, roughnessFactor, metallicRoughnessTexture } =
pbrMetallicRoughness;
Expand Down