Skip to content

Commit c3d174f

Browse files
author
Mike Bond
committed
Add geometry_normal to OpenPBR material
1 parent d4d6183 commit c3d174f

File tree

45 files changed

+1176
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1176
-197
lines changed

packages/dev/core/src/Materials/PBR/openPbrMaterial.ts

Lines changed: 20 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ export class OpenPBRMaterialDefines extends ImageProcessingDefinesMixin(OpenPBRM
245245

246246
public NORMAL = false;
247247
public TANGENT = false;
248-
public BUMP = false;
249-
public BUMPDIRECTUV = 0;
250248
public OBJECTSPACE_NORMALMAP = false;
251249
public PARALLAX = false;
252250
public PARALLAX_RHS = false;
@@ -564,6 +562,15 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
564562
// eslint-disable-next-line @typescript-eslint/no-unused-vars
565563
private _baseMetalRoughTexture: Sampler = new Sampler("base_metalness_specular_roughness", "baseMetalRough", "METALLIC_ROUGHNESS");
566564

565+
/**
566+
* Defines the normal of the material's geometry.
567+
* See OpenPBR's specs for geometry_normal
568+
*/
569+
public geometryNormalTexture: BaseTexture;
570+
@addAccessorsForMaterialProperty("_markAllSubMeshesAsTexturesDirty", "geometryNormalTexture")
571+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
572+
private _geometryNormalTexture: Sampler = new Sampler("geometry_normal", "geometryNormal", "GEOMETRY_NORMAL");
573+
567574
/**
568575
* Defines the opacity of the material's geometry.
569576
* See OpenPBR's specs for geometry_opacity
@@ -637,13 +644,6 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
637644
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
638645
public environmentIntensity: number = 1.0;
639646

640-
/**
641-
* Debug Control allowing disabling the bump map on this material.
642-
*/
643-
@serialize()
644-
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
645-
public disableBumpMap: boolean = false;
646-
647647
/**
648648
* Stores the reflection values in a texture.
649649
*/
@@ -658,13 +658,6 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
658658
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
659659
public useSpecularWeightFromTextureAlpha = false;
660660

661-
/**
662-
* Stores surface normal data used to displace a mesh in a texture.
663-
*/
664-
@serializeAsTexture()
665-
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
666-
public bumpTexture: Nullable<BaseTexture>;
667-
668661
/**
669662
* Stores the pre-calculated light information of a mesh in a texture.
670663
*/
@@ -791,14 +784,14 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
791784
public useObjectSpaceNormalMap = false;
792785

793786
/**
794-
* Allows using the bump map in parallax mode.
787+
* Allows using the normal map in parallax mode.
795788
*/
796789
@serialize()
797790
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
798791
public useParallax = false;
799792

800793
/**
801-
* Allows using the bump map in parallax occlusion mode.
794+
* Allows using the normal map in parallax occlusion mode.
802795
*/
803796
@serialize()
804797
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
@@ -819,7 +812,7 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
819812
public disableLighting = false;
820813

821814
/**
822-
* Force the shader to compute irradiance in the fragment shader in order to take bump in account.
815+
* Force the shader to compute irradiance in the fragment shader in order to take normal mapping into account.
823816
*/
824817
@serialize()
825818
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
@@ -890,7 +883,7 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
890883
/**
891884
* Enables specular anti aliasing in the PBR shader.
892885
* It will both interacts on the Geometry for analytical and IBL lighting.
893-
* It also prefilter the roughness map based on the bump values.
886+
* It also prefilter the roughness map based on the normalmap values.
894887
*/
895888
@serialize()
896889
@expandToProperty("_markAllSubMeshesAsTexturesDirty")
@@ -975,12 +968,6 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
975968
*/
976969
private _lightingInfos: Vector4 = new Vector4(this._directIntensity, this._emissiveIntensity, this._environmentIntensity, 1.0);
977970

978-
/**
979-
* Debug Control allowing disabling the bump map on this material.
980-
* @internal
981-
*/
982-
public _disableBumpMap: boolean = false;
983-
984971
/**
985972
* Stores the reflection values in a texture.
986973
* @internal
@@ -994,12 +981,6 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
994981
*/
995982
public _useSpecularWeightFromTextureAlpha = false;
996983

997-
/**
998-
* Stores surface normal data used to displace a mesh in a texture.
999-
* @internal
1000-
*/
1001-
public _bumpTexture: Nullable<BaseTexture> = null;
1002-
1003984
/**
1004985
* Stores the pre-calculated light information of a mesh in a texture.
1005986
* @internal
@@ -1103,13 +1084,13 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
11031084
public _useObjectSpaceNormalMap = false;
11041085

11051086
/**
1106-
* Allows using the bump map in parallax mode.
1087+
* Allows using the normal map in parallax mode.
11071088
* @internal
11081089
*/
11091090
public _useParallax = false;
11101091

11111092
/**
1112-
* Allows using the bump map in parallax occlusion mode.
1093+
* Allows using the normal map in parallax occlusion mode.
11131094
* @internal
11141095
*/
11151096
public _useParallaxOcclusion = false;
@@ -1179,7 +1160,7 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
11791160
public _environmentBRDFTexture: Nullable<BaseTexture> = null;
11801161

11811162
/**
1182-
* Force the shader to compute irradiance in the fragment shader in order to take bump in account.
1163+
* Force the shader to compute irradiance in the fragment shader in order to take normal mapping into account.
11831164
* @internal
11841165
*/
11851166
public _forceIrradianceInFragment = false;
@@ -1224,7 +1205,7 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
12241205
/**
12251206
* Enables specular anti aliasing in the PBR shader.
12261207
* It will both interacts on the Geometry for analytical and IBL lighting.
1227-
* It also prefilter the roughness map based on the bump values.
1208+
* It also prefilter the roughness map based on the normalmap values.
12281209
* @internal
12291210
*/
12301211
public _enableSpecularAntiAliasing = false;
@@ -1369,6 +1350,7 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
13691350
this._specularRoughness;
13701351
this._specularIor;
13711352
this._baseMetalRoughTexture;
1353+
this._geometryNormalTexture;
13721354
this._geometryOpacity;
13731355
this._geometryOpacityTexture;
13741356
this._emissionColor;
@@ -1621,13 +1603,6 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
16211603
}
16221604
}
16231605

1624-
if (engine.getCaps().standardDerivatives && this._bumpTexture && MaterialFlags.BumpTextureEnabled && !this._disableBumpMap) {
1625-
// Bump texture cannot be not blocking.
1626-
if (!this._bumpTexture.isReady()) {
1627-
return false;
1628-
}
1629-
}
1630-
16311606
if (this._environmentBRDFTexture && MaterialFlags.ReflectionTextureEnabled) {
16321607
// This is blocking.
16331608
if (!this._environmentBRDFTexture.isReady()) {
@@ -1717,9 +1692,7 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
17171692
// Order is important !
17181693
const ubo = this._uniformBuffer;
17191694
ubo.addUniform("vLightmapInfos", 2);
1720-
ubo.addUniform("vBumpInfos", 3);
17211695
ubo.addUniform("lightmapMatrix", 16);
1722-
ubo.addUniform("bumpMatrix", 16);
17231696
ubo.addUniform("vTangentSpaceParams", 2);
17241697
ubo.addUniform("vLightingIntensity", 4);
17251698

@@ -1821,10 +1794,7 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
18211794
BindTextureMatrix(this._lightmapTexture, ubo, "lightmap");
18221795
}
18231796

1824-
if (this._bumpTexture && engine.getCaps().standardDerivatives && MaterialFlags.BumpTextureEnabled && !this._disableBumpMap) {
1825-
ubo.updateFloat3("vBumpInfos", this._bumpTexture.coordinatesIndex, this._bumpTexture.level, this._parallaxScaleBias);
1826-
BindTextureMatrix(this._bumpTexture, ubo, "bump");
1827-
1797+
if (this.geometryNormalTexture) {
18281798
if (scene._mirroredCameraPosition) {
18291799
ubo.updateFloat2("vTangentSpaceParams", this._invertNormalMapX ? 1.0 : -1.0, this._invertNormalMapY ? 1.0 : -1.0);
18301800
} else {
@@ -1886,10 +1856,6 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
18861856
if (this._lightmapTexture && MaterialFlags.LightmapTextureEnabled) {
18871857
ubo.setTexture("lightmapSampler", this._lightmapTexture);
18881858
}
1889-
1890-
if (this._bumpTexture && engine.getCaps().standardDerivatives && MaterialFlags.BumpTextureEnabled && !this._disableBumpMap) {
1891-
ubo.setTexture("bumpSampler", this._bumpTexture);
1892-
}
18931859
}
18941860

18951861
// OIT with depth peeling
@@ -1963,10 +1929,6 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
19631929
results.push(this._reflectionTexture);
19641930
}
19651931

1966-
if (this._bumpTexture && this._bumpTexture.animations && this._bumpTexture.animations.length > 0) {
1967-
results.push(this._bumpTexture);
1968-
}
1969-
19701932
if (this._lightmapTexture && this._lightmapTexture.animations && this._lightmapTexture.animations.length > 0) {
19711933
results.push(this._lightmapTexture);
19721934
}
@@ -1993,10 +1955,6 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
19931955
activeTextures.push(this._reflectionTexture);
19941956
}
19951957

1996-
if (this._bumpTexture) {
1997-
activeTextures.push(this._bumpTexture);
1998-
}
1999-
20001958
if (this._lightmapTexture) {
20011959
activeTextures.push(this._lightmapTexture);
20021960
}
@@ -2026,10 +1984,6 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
20261984
return true;
20271985
}
20281986

2029-
if (this._bumpTexture === texture) {
2030-
return true;
2031-
}
2032-
20331987
if (this._lightmapTexture === texture) {
20341988
return true;
20351989
}
@@ -2066,7 +2020,6 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
20662020
}
20672021

20682022
this._reflectionTexture?.dispose();
2069-
this._bumpTexture?.dispose();
20702023
this._lightmapTexture?.dispose();
20712024
}
20722025

@@ -2148,10 +2101,6 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
21482101
fallbacks.addFallback(fallbackRank++, "TANGENT");
21492102
}
21502103

2151-
if (defines.BUMP) {
2152-
fallbacks.addFallback(fallbackRank++, "BUMP");
2153-
}
2154-
21552104
fallbackRank = HandleFallbacksForShadows(defines, fallbacks, this._maxSimultaneousLights, fallbackRank++);
21562105

21572106
if (defines.SPECULARTERM) {
@@ -2224,11 +2173,9 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
22242173
"vFogInfos",
22252174
"vFogColor",
22262175
"pointSize",
2227-
"vBumpInfos",
22282176
"vLightmapInfos",
22292177
"mBones",
22302178
"normalMatrix",
2231-
"bumpMatrix",
22322179
"lightmapMatrix",
22332180
"vLightingIntensity",
22342181
"logarithmicDepthConstant",
@@ -2245,13 +2192,9 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
22452192
}
22462193

22472194
const samplers = [
2248-
"reflectivitySampler",
2249-
"bumpSampler",
22502195
"lightmapSampler",
22512196
"environmentBrdfSampler",
22522197
"boneSampler",
2253-
"metallicReflectanceSampler",
2254-
"reflectanceSampler",
22552198
"morphTargets",
22562199
"oitDepthSampler",
22572200
"oitFrontColorSampler",
@@ -2378,13 +2321,6 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
23782321
defines["MAINUV" + i] = false;
23792322
}
23802323
if (scene.texturesEnabled) {
2381-
defines.AMBIENTDIRECTUV = 0;
2382-
defines.OPACITYDIRECTUV = 0;
2383-
defines.EMISSIVEDIRECTUV = 0;
2384-
defines.REFLECTIVITYDIRECTUV = 0;
2385-
defines.METALLIC_REFLECTANCEDIRECTUV = 0;
2386-
defines.REFLECTANCEDIRECTUV = 0;
2387-
defines.BUMPDIRECTUV = 0;
23882324
defines.LIGHTMAPDIRECTUV = 0;
23892325

23902326
if (engine.getCaps().textureLOD) {
@@ -2428,20 +2364,16 @@ export class OpenPBRMaterial extends OpenPBRMaterialBase {
24282364
defines.SPECULAR_WEIGHT_USE_ALPHA_ONLY = this._useSpecularWeightFromTextureAlpha;
24292365
}
24302366

2431-
if (engine.getCaps().standardDerivatives && this._bumpTexture && MaterialFlags.BumpTextureEnabled && !this._disableBumpMap) {
2432-
PrepareDefinesForMergedUV(this._bumpTexture, defines, "BUMP");
2433-
2367+
if (this.geometryNormalTexture) {
24342368
if (this._useParallax && this.baseColorTexture && MaterialFlags.DiffuseTextureEnabled) {
24352369
defines.PARALLAX = true;
24362370
defines.PARALLAX_RHS = scene.useRightHandedSystem;
24372371
defines.PARALLAXOCCLUSION = !!this._useParallaxOcclusion;
24382372
} else {
24392373
defines.PARALLAX = false;
24402374
}
2441-
24422375
defines.OBJECTSPACE_NORMALMAP = this._useObjectSpaceNormalMap;
24432376
} else {
2444-
defines.BUMP = false;
24452377
defines.PARALLAX = false;
24462378
defines.PARALLAX_RHS = false;
24472379
defines.PARALLAXOCCLUSION = false;

packages/dev/core/src/Rendering/geometryBufferRenderer.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,8 @@ export class GeometryBufferRenderer {
568568
}
569569

570570
// Normal map texture
571-
if ((material.bumpTexture || material.normalTexture) && MaterialFlags.BumpTextureEnabled) {
572-
const texture = material.bumpTexture || material.normalTexture;
571+
if ((material.bumpTexture || material.normalTexture || material.geometryNormalTexture) && MaterialFlags.BumpTextureEnabled) {
572+
const texture = material.bumpTexture || material.normalTexture || material.geometryNormalTexture;
573573
defines.push("#define BUMP");
574574
defines.push(`#define BUMP_UV${texture.coordinatesIndex + 1}`);
575575
needUv = true;
@@ -1110,8 +1110,12 @@ export class GeometryBufferRenderer {
11101110
}
11111111

11121112
// Bump
1113-
if ((material.bumpTexture || material.normalTexture) && scene.getEngine().getCaps().standardDerivatives && MaterialFlags.BumpTextureEnabled) {
1114-
const texture = material.bumpTexture || material.normalTexture;
1113+
if (
1114+
(material.bumpTexture || material.normalTexture || material.geometryNormalTexture) &&
1115+
scene.getEngine().getCaps().standardDerivatives &&
1116+
MaterialFlags.BumpTextureEnabled
1117+
) {
1118+
const texture = material.bumpTexture || material.normalTexture || material.geometryNormalTexture;
11151119
effect.setFloat3("vBumpInfos", texture.coordinatesIndex, 1.0 / texture.level, material.parallaxScaleBias);
11161120
effect.setMatrix("bumpMatrix", texture.getTextureMatrix());
11171121
effect.setTexture("bumpSampler", texture);

packages/dev/core/src/Shaders/ShadersInclude/openPbrUboDeclaration.fx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ layout(std140, column_major) uniform;
2424

2525
uniform Material {
2626
vec2 vLightmapInfos;
27-
vec3 vBumpInfos;
28-
2927
mat4 lightmapMatrix;
30-
mat4 bumpMatrix;
3128
vec2 vTangentSpaceParams;
3229
vec4 vLightingIntensity;
3330
float pointSize;
@@ -84,6 +81,8 @@ uniform Material {
8481
mat4 specularColorMatrix;
8582
vec2 vBaseMetalRoughInfos;
8683
mat4 baseMetalRoughMatrix;
84+
vec2 vGeometryNormalInfos;
85+
mat4 geometryNormalMatrix;
8786
vec2 vGeometryOpacityInfos;
8887
mat4 geometryOpacityMatrix;
8988
vec2 vEmissionInfos;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
struct ambientOcclusionOutParams
2+
{
3+
vec3 ambientOcclusionColor;
4+
#if DEBUGMODE > 0 && defined(AMBIENT_OCCLUSION)
5+
vec3 ambientOcclusionColorMap;
6+
#endif
7+
};
8+
9+
ambientOcclusionOutParams ambientOcclusionBlock(
10+
#ifdef AMBIENT_OCCLUSION
11+
in vec3 ambientOcclusionFromTexture,
12+
in vec2 ambientOcclusionInfos
13+
#endif
14+
)
15+
{
16+
ambientOcclusionOutParams outParams;
17+
vec3 ambientOcclusionColor = vec3(1., 1., 1.);
18+
19+
#ifdef AMBIENT_OCCLUSION
20+
ambientOcclusionColor = vec3(ambientOcclusionFromTexture.r * ambientOcclusionInfos.y);
21+
22+
#if DEBUGMODE > 0
23+
outParams.ambientOcclusionColorMap = ambientOcclusionFromTexture;
24+
#endif
25+
#endif
26+
27+
outParams.ambientOcclusionColor = ambientOcclusionColor;
28+
29+
return outParams;
30+
}

0 commit comments

Comments
 (0)