Skip to content

Commit 8448191

Browse files
author
Mike Bond
committed
Starting to refactor glsl shaders for OpenPBR
1 parent cf8143a commit 8448191

File tree

44 files changed

+1453
-812
lines changed

Some content is hidden

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

44 files changed

+1453
-812
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"[css]": {
5151
"editor.defaultFormatter": "esbenp.prettier-vscode"
5252
},
53-
"editor.formatOnSave": true,
53+
"editor.formatOnSave": false,
5454
"jest.runMode": "on-demand",
5555
"typescript.preferences.importModuleSpecifier": "project-relative",
5656
"javascript.preferences.importModuleSpecifier": "project-relative"

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

Lines changed: 106 additions & 106 deletions
Large diffs are not rendered by default.

packages/dev/core/src/Materials/materialHelper.functions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type { IColor3Like } from "core/Maths";
2222
import { MaterialFlags } from "./materialFlags";
2323
import { Texture } from "./Textures/texture";
2424
import type { CubeTexture } from "./Textures/cubeTexture";
25+
import { Color3 } from "core/Maths/math.color";
2526

2627
// Temps
2728
const TempFogColor: IColor3Like = { r: 0, g: 0, b: 0 };

packages/dev/core/src/Rendering/IBLShadows/iblShadowsPluginMaterial.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,16 @@ export class IBLShadowsPluginMaterial extends MaterialPluginBase {
169169
#ifdef REFLECTION
170170
#ifdef COLORED_IBL_SHADOWS
171171
var shadowValue: vec3f = computeIndirectShadow();
172-
finalIrradiance *= shadowValue;
173-
finalRadianceScaled *= mix(vec3f(1.0), shadowValue, roughness);
172+
slab_diffuse *= shadowValue;
173+
slab_glossy *= mix(vec3f(1.0), shadowValue, alphaG);
174174
#else
175175
var shadowValue: vec2f = computeIndirectShadow();
176-
finalIrradiance *= vec3f(shadowValue.x);
177-
finalRadianceScaled *= vec3f(mix(pow(shadowValue.y, 4.0), shadowValue.x, roughness));
176+
slab_diffuse *= vec3f(shadowValue.x);
177+
slab_glossy *= vec3f(mix(pow(shadowValue.y, 4.0), shadowValue.x, alphaG));
178178
#endif
179179
#endif
180180
#else
181-
finalDiffuse *= computeIndirectShadow().x;
181+
slab_diffuse *= computeIndirectShadow().x;
182182
#endif
183183
#endif
184184
`;
@@ -247,16 +247,16 @@ export class IBLShadowsPluginMaterial extends MaterialPluginBase {
247247
#ifdef REFLECTION
248248
#ifdef COLORED_IBL_SHADOWS
249249
vec3 shadowValue = computeIndirectShadow();
250-
finalIrradiance.rgb *= shadowValue.rgb;
251-
finalRadianceScaled *= mix(vec3(1.0), shadowValue.rgb, roughness);
250+
slab_diffuse.rgb *= shadowValue.rgb;
251+
slab_glossy *= mix(vec3(1.0), shadowValue.rgb, alphaG);
252252
#else
253253
vec2 shadowValue = computeIndirectShadow();
254-
finalIrradiance *= shadowValue.x;
255-
finalRadianceScaled *= mix(pow(shadowValue.y, 4.0), shadowValue.x, roughness);
254+
slab_diffuse *= shadowValue.x;
255+
slab_glossy *= mix(pow(shadowValue.y, 4.0), shadowValue.x, alphaG);
256256
#endif
257257
#endif
258258
#else
259-
finalDiffuse *= computeIndirectShadow().x;
259+
slab_diffuse *= computeIndirectShadow().x;
260260
#endif
261261
#endif
262262
`;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ uniform Material {
6060
vec3 vSphericalYZ;
6161
vec3 vSphericalZX;
6262

63-
float baseWeight;
63+
float vBaseWeight;
6464
vec4 vBaseColor;
6565
float vBaseDiffuseRoughness;
6666
vec4 vReflectanceInfo;
6767
vec4 vSpecularColor;
68+
float vCoatWeight;
69+
vec3 vCoatColor;
70+
float vCoatRoughness;
71+
float vCoatIor;
6872
vec3 vEmissionColor;
6973

7074
vec2 vBaseWeightInfos;
@@ -79,6 +83,12 @@ uniform Material {
7983
mat4 specularColorMatrix;
8084
vec2 vBaseMetalRoughInfos;
8185
mat4 baseMetalRoughMatrix;
86+
vec2 vCoatWeightInfos;
87+
mat4 coatWeightMatrix;
88+
vec2 vCoatColorInfos;
89+
mat4 coatColorMatrix;
90+
vec2 vCoatRoughnessInfos;
91+
mat4 coatRoughnessMatrix;
8292
vec2 vGeometryNormalInfos;
8393
mat4 geometryNormalMatrix;
8494
vec2 vGeometryOpacityInfos;
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// This code reads uniforms and samples textures to fill up the base and specular
2+
// layer properties for OpenPBR
3+
4+
// Base Layer Properties
5+
// We don't include base_weight in our initial variables because it is multiplied
6+
// into the base_color in this code snippet.
7+
vec3 base_color = vec3(0.8);
8+
float base_metalness = 0.0;
9+
float base_diffuse_roughness = 0.0;
10+
11+
// Specular Layer Properties
12+
float specular_weight = 1.0;
13+
float specular_roughness = 0.3;
14+
vec3 specular_color = vec3(1.0);
15+
float specular_roughness_anisotropy = 0.0;
16+
float specular_ior = 1.5;
17+
float alpha = 1.0;
18+
19+
// Sample Base Layer properties from textures
20+
#ifdef BASE_WEIGHT
21+
vec4 baseWeightFromTexture = texture2D(baseWeightSampler, vBaseWeightUV + uvOffset);
22+
#endif
23+
24+
#ifdef BASE_COLOR
25+
vec4 baseColorFromTexture = texture2D(baseColorSampler, vBaseColorUV + uvOffset);
26+
#endif
27+
28+
#ifdef METALLIC_ROUGHNESS
29+
vec4 metallicRoughnessFromTexture = texture2D(baseMetalRoughSampler, vBaseMetalRoughUV + uvOffset);
30+
#endif
31+
32+
#ifdef BASE_DIFFUSE_ROUGHNESS
33+
float baseDiffuseRoughnessFromTexture = texture2D(baseDiffuseRoughnessSampler, vBaseDiffuseRoughnessUV + uvOffset).r;
34+
#endif
35+
36+
#ifdef GEOMETRY_OPACITY
37+
vec4 opacityFromTexture = texture2D(opacitySampler, vOpacityUV + uvOffset);
38+
#endif
39+
40+
#ifdef DECAL
41+
vec4 decalFromTexture = texture2D(decalSampler, vDecalUV + uvOffset);
42+
#endif
43+
44+
#ifdef SPECULAR_COLOR
45+
vec4 specularColorFromTexture = texture2D(specularColorSampler, vSpecularColorUV + uvOffset);
46+
#ifdef SPECULAR_COLOR_GAMMA
47+
specularColorFromTexture = toLinearSpace(specularColorFromTexture.rgb);
48+
#endif
49+
#endif
50+
51+
#ifdef SPECULAR_WEIGHT
52+
vec4 specularWeightFromTexture = texture2D(specularWeightSampler, vSpecularWeightUV + uvOffset);
53+
#endif
54+
55+
// Initalize base layer properties from uniforms
56+
base_color = vBaseColor.rgb;
57+
#if defined(VERTEXCOLOR) || defined(INSTANCESCOLOR) && defined(INSTANCES)
58+
base_color *= vColor.rgb;
59+
#endif
60+
#if defined(VERTEXALPHA) || defined(INSTANCESCOLOR) && defined(INSTANCES)
61+
alpha *= vColor.a;
62+
#endif
63+
base_color *= vec3(vBaseWeight);
64+
alpha = vBaseColor.a;
65+
base_metalness = vReflectanceInfo.x;
66+
base_diffuse_roughness = vBaseDiffuseRoughness;
67+
specular_roughness = vReflectanceInfo.y;
68+
specular_color = vSpecularColor.rgb;
69+
specular_weight = vReflectanceInfo.a;
70+
specular_ior = vReflectanceInfo.z;
71+
72+
// Apply texture values to base layer properties
73+
74+
#ifdef BASE_COLOR
75+
#if defined(ALPHAFROMALBEDO) || defined(ALPHATEST)
76+
alpha *= baseColorFromTexture.a;
77+
#endif
78+
79+
#ifdef BASE_COLOR_GAMMA
80+
base_color *= toLinearSpace(baseColorFromTexture.rgb);
81+
#else
82+
base_color *= baseColorFromTexture.rgb;
83+
#endif
84+
85+
base_color *= vBaseColorInfos.y;
86+
#endif
87+
88+
#ifdef BASE_WEIGHT
89+
base_color *= baseWeightFromTexture.r;
90+
#endif
91+
92+
// _____________________________ Alpha Information _______________________________
93+
#ifdef GEOMETRY_OPACITY
94+
alpha *= opacityFromTexture.a;
95+
alpha *= vGeometryOpacityInfos.y;
96+
#endif
97+
98+
#ifdef ALPHATEST
99+
#if DEBUGMODE != 88
100+
if (alpha < ALPHATESTVALUE)
101+
discard;
102+
#endif
103+
104+
#ifndef ALPHABLEND
105+
// Prevent to blend with the canvas.
106+
alpha = 1.0;
107+
#endif
108+
#endif
109+
110+
#ifdef METALLIC_ROUGHNESS
111+
base_metalness *= metallicRoughnessFromTexture.b;
112+
specular_roughness *= metallicRoughnessFromTexture.g;
113+
#endif
114+
115+
#ifdef BASE_DIFFUSE_ROUGHNESS
116+
base_diffuse_roughness *= baseDiffuseRoughnessFromTexture * vBaseDiffuseRoughnessInfos.y;
117+
#endif
118+
119+
#ifdef SPECULAR_COLOR
120+
specular_color *= specularColorFromTexture.rgb;
121+
#endif
122+
123+
#ifdef SPECULAR_WEIGHT
124+
// If loaded from a glTF, the specular_weight is stored in the alpha channel.
125+
// Otherwise, it's expected to just be a greyscale texture.
126+
#ifdef SPECULAR_WEIGHT_USE_ALPHA_ONLY
127+
specular_weight *= specularWeightFromTexture.a;
128+
#else
129+
specular_weight *= specularWeightFromTexture.r;
130+
#endif
131+
#endif
132+
133+
#ifdef DETAIL
134+
float detailRoughness = mix(0.5, detailColor.b, vDetailInfos.w);
135+
float loLerp = mix(0., specular_roughness, detailRoughness * 2.);
136+
float hiLerp = mix(specular_roughness, 1., (detailRoughness - 0.5) * 2.);
137+
specular_roughness = mix(loLerp, hiLerp, step(detailRoughness, 0.5));
138+
#endif

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

Lines changed: 0 additions & 115 deletions
This file was deleted.

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

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)