Skip to content

Commit

Permalink
feat: add PBRMaterial emissionFactor support
Browse files Browse the repository at this point in the history
  • Loading branch information
06wj committed Feb 22, 2021
1 parent 08f1252 commit de7cf25
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/loader/GLTFParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,12 @@ const GLTFParser = Class.create(/** @lends GLTFParser.prototype */{
if (emissiveTexture) {
material.emission = this.getTexture(emissiveTexture);
}

const emissiveFactor = values.emissiveFactor;
if (emissiveFactor) {
material.emissionFactor.fromArray(emissiveFactor);
material.emissionFactor.a = 1;
}
} else {
material.lightType = 'NONE';
}
Expand Down
13 changes: 11 additions & 2 deletions src/material/PBRMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,19 @@ const PBRMaterial = Class.create(/** @lends PBRMaterial.prototype */ {
isSpecularEnvMapIncludeMipmaps: false,

/**
* 放射光贴图(sRGB 空间),或颜色
* 放射光贴图(sRGB 空间)
* @default null
* @type {Texture|Color}
* @type {Texture}
*/
emission: null,

/**
* The emissive color of the material.
* @default new Color(0, 0, 0)
* @type {Color}
*/
emissionFactor: null,

/**
* 是否基于反射光泽度的 PBR,具体见 [KHR_materials_pbrSpecularGlossiness]{@link https://github.com/KhronosGroup/glTF/tree/master/extensions/Khronos/KHR_materials_pbrSpecularGlossiness}
* @default false
Expand Down Expand Up @@ -259,6 +266,7 @@ const PBRMaterial = Class.create(/** @lends PBRMaterial.prototype */ {
constructor(params) {
this.baseColor = new Color(1, 1, 1);
this.specular = new Color(1, 1, 1);
this.emissionFactor = new Color(0, 0, 0);

PBRMaterial.superclass.constructor.call(this, params);

Expand All @@ -267,6 +275,7 @@ const PBRMaterial = Class.create(/** @lends PBRMaterial.prototype */ {
u_metallic: 'METALLIC',
u_roughness: 'ROUGHNESS',
u_specular: 'SPECULAR',
u_emissionFactor: 'EMISSIONFACTOR',
u_glossiness: 'GLOSSINESS',
u_brdfLUT: 'BRDFLUT',
u_diffuseEnvMap: 'DIFFUSEENVMAP',
Expand Down
12 changes: 11 additions & 1 deletion src/material/semantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,17 @@ const semantic = {
}
},

/**
* EMISSION FACTOR
* @type {Object}
*/
EMISSIONFACTOR: {
get(mesh, material, programInfo) {
return material.emissionFactor.elements;
}
},


/**
* @type {semanticObject}
*/
Expand All @@ -970,7 +981,6 @@ const semantic = {
}
},


/**
* @type {semanticObject}
*/
Expand Down
2 changes: 2 additions & 0 deletions src/shader/chunk/pbr.frag
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#pragma glslify: import('./fixMathCrash.glsl');

uniform vec4 u_baseColor;
uniform vec4 u_emissionFactor;

#ifdef HILO_BASE_COLOR_MAP
uniform HILO_SAMPLER_2D u_baseColorMap;
#endif
Expand Down
8 changes: 6 additions & 2 deletions src/shader/chunk/pbr_main.frag
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vec4 baseColor = u_baseColor;
vec3 emissionColor = u_emissionFactor.rgb;

#ifdef HILO_BASE_COLOR_MAP
#ifdef HILO_GAMMA_CORRECTION
Expand Down Expand Up @@ -185,16 +186,19 @@ color.a = baseColor.a;

#ifdef HILO_EMISSION_MAP
#ifdef HILO_GAMMA_CORRECTION
color.rgb += sRGBToLinear(HILO_TEXTURE_2D(u_emission)).rgb;
emissionColor *= sRGBToLinear(HILO_TEXTURE_2D(u_emission)).rgb;
#else
color.rgb += HILO_TEXTURE_2D(u_emission).rgb;
emissionColor *= HILO_TEXTURE_2D(u_emission).rgb;
#endif
#endif

color.rgb += emissionColor;

#ifdef HILO_HAS_CLEARCOAT
float t = clearcoatFactor * clearCoatFresnel;
color.rgb = color.rgb * (1.0 - t) + clearCoatLayer * t;
#endif
#else
color = baseColor;
color.rgb += emissionColor;
#endif

0 comments on commit de7cf25

Please sign in to comment.