basilisk-engine 0.1.38__py3-none-any.whl → 0.1.39__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of basilisk-engine might be problematic. Click here for more details.

Files changed (97) hide show
  1. basilisk/__init__.py +26 -26
  2. basilisk/audio/sound.py +27 -27
  3. basilisk/bsk_assets/cube.obj +48 -48
  4. basilisk/collisions/broad/broad_aabb.py +102 -102
  5. basilisk/collisions/broad/broad_bvh.py +137 -137
  6. basilisk/collisions/collider.py +95 -95
  7. basilisk/collisions/collider_handler.py +226 -226
  8. basilisk/collisions/narrow/contact_manifold.py +95 -95
  9. basilisk/collisions/narrow/dataclasses.py +34 -34
  10. basilisk/collisions/narrow/deprecated.py +46 -46
  11. basilisk/collisions/narrow/epa.py +91 -91
  12. basilisk/collisions/narrow/gjk.py +66 -66
  13. basilisk/collisions/narrow/graham_scan.py +24 -24
  14. basilisk/collisions/narrow/helper.py +29 -29
  15. basilisk/collisions/narrow/line_intersections.py +106 -106
  16. basilisk/collisions/narrow/sutherland_hodgman.py +75 -75
  17. basilisk/config.py +53 -53
  18. basilisk/draw/draw.py +100 -100
  19. basilisk/draw/draw_handler.py +178 -178
  20. basilisk/draw/font_renderer.py +28 -28
  21. basilisk/engine.py +165 -165
  22. basilisk/generic/abstract_bvh.py +15 -15
  23. basilisk/generic/abstract_custom.py +133 -133
  24. basilisk/generic/collisions.py +70 -70
  25. basilisk/generic/input_validation.py +82 -82
  26. basilisk/generic/math.py +18 -7
  27. basilisk/generic/matrices.py +35 -35
  28. basilisk/generic/meshes.py +72 -72
  29. basilisk/generic/quat.py +142 -142
  30. basilisk/generic/quat_methods.py +7 -7
  31. basilisk/generic/raycast_result.py +26 -26
  32. basilisk/generic/vec3.py +143 -143
  33. basilisk/input/__init__.py +0 -0
  34. basilisk/input/mouse.py +62 -0
  35. basilisk/input/path.py +14 -0
  36. basilisk/input_output/IO_handler.py +91 -91
  37. basilisk/input_output/clock.py +49 -49
  38. basilisk/input_output/keys.py +43 -43
  39. basilisk/input_output/mouse.py +90 -90
  40. basilisk/input_output/path.py +14 -14
  41. basilisk/mesh/cube.py +33 -33
  42. basilisk/mesh/mesh.py +233 -233
  43. basilisk/mesh/mesh_from_data.py +150 -150
  44. basilisk/mesh/model.py +271 -271
  45. basilisk/mesh/narrow_aabb.py +89 -89
  46. basilisk/mesh/narrow_bvh.py +91 -91
  47. basilisk/mesh/narrow_primative.py +23 -23
  48. basilisk/nodes/helper.py +28 -28
  49. basilisk/nodes/node.py +709 -704
  50. basilisk/nodes/node_handler.py +97 -97
  51. basilisk/particles/particle_handler.py +64 -64
  52. basilisk/particles/particle_renderer.py +93 -93
  53. basilisk/physics/impulse.py +112 -112
  54. basilisk/physics/physics_body.py +43 -43
  55. basilisk/physics/physics_engine.py +35 -35
  56. basilisk/render/batch.py +103 -103
  57. basilisk/render/bloom.py +107 -107
  58. basilisk/render/camera.py +260 -260
  59. basilisk/render/chunk.py +108 -108
  60. basilisk/render/chunk_handler.py +167 -167
  61. basilisk/render/frame.py +110 -110
  62. basilisk/render/framebuffer.py +202 -202
  63. basilisk/render/image.py +120 -120
  64. basilisk/render/image_handler.py +120 -120
  65. basilisk/render/light.py +96 -96
  66. basilisk/render/light_handler.py +58 -58
  67. basilisk/render/material.py +232 -232
  68. basilisk/render/material_handler.py +133 -133
  69. basilisk/render/post_process.py +146 -146
  70. basilisk/render/shader.py +134 -134
  71. basilisk/render/shader_handler.py +85 -85
  72. basilisk/render/sky.py +120 -120
  73. basilisk/scene.py +290 -290
  74. basilisk/shaders/batch.frag +289 -289
  75. basilisk/shaders/batch.vert +117 -117
  76. basilisk/shaders/bloom_downsample.frag +42 -42
  77. basilisk/shaders/bloom_frame.frag +24 -24
  78. basilisk/shaders/bloom_upsample.frag +33 -33
  79. basilisk/shaders/crt.frag +31 -31
  80. basilisk/shaders/draw.frag +25 -25
  81. basilisk/shaders/draw.vert +25 -25
  82. basilisk/shaders/filter.frag +22 -22
  83. basilisk/shaders/frame.frag +12 -12
  84. basilisk/shaders/frame.vert +13 -13
  85. basilisk/shaders/geometry.frag +10 -10
  86. basilisk/shaders/geometry.vert +41 -41
  87. basilisk/shaders/normal.frag +62 -62
  88. basilisk/shaders/normal.vert +96 -96
  89. basilisk/shaders/particle.frag +76 -76
  90. basilisk/shaders/particle.vert +86 -86
  91. basilisk/shaders/sky.frag +23 -23
  92. basilisk/shaders/sky.vert +13 -13
  93. {basilisk_engine-0.1.38.dist-info → basilisk_engine-0.1.39.dist-info}/METADATA +89 -89
  94. basilisk_engine-0.1.39.dist-info/RECORD +114 -0
  95. {basilisk_engine-0.1.38.dist-info → basilisk_engine-0.1.39.dist-info}/WHEEL +1 -1
  96. basilisk_engine-0.1.38.dist-info/RECORD +0 -111
  97. {basilisk_engine-0.1.38.dist-info → basilisk_engine-0.1.39.dist-info}/top_level.txt +0 -0
@@ -1,290 +1,290 @@
1
- #version 330 core
2
-
3
- layout (location = 0) out vec4 fragColor;
4
- layout (location = 1) out vec4 bloomColor;
5
-
6
- // Structs needed for the shader
7
- struct textArray {
8
- sampler2DArray array;
9
- };
10
-
11
- struct DirectionalLight {
12
- vec3 direction;
13
- float intensity;
14
- vec3 color;
15
- float ambient;
16
- };
17
-
18
- // Material struct sent to fragment shader
19
- struct Material {
20
- vec3 color;
21
- vec3 emissiveColor;
22
- float roughness;
23
- float subsurface;
24
- float sheen;
25
- float sheenTint;
26
- float anisotropic;
27
- float specular;
28
- float metallicness;
29
- float specularTint;
30
- float clearcoat;
31
- float clearcoatGloss;
32
-
33
- int hasAlbedoMap;
34
- vec2 albedoMap;
35
- int hasNormalMap;
36
- vec2 normalMap;
37
- int hasRoughnessMap;
38
- vec2 roughnessMap;
39
- int hasAoMap;
40
- vec2 aoMap;
41
- };
42
-
43
- struct LightResult {
44
- vec3 diffuse;
45
- vec3 specular;
46
- vec3 clearcoat;
47
- };
48
-
49
- in vec2 uv;
50
- in vec3 position;
51
- in mat3 TBN;
52
-
53
- // Material attributes
54
- flat in Material mtl;
55
-
56
- // Uniforms
57
- uniform vec3 cameraPosition;
58
- const int maxDirLights = 5;
59
- uniform DirectionalLight dirLights[maxDirLights];
60
- uniform int numDirLights;
61
- uniform textArray textureArrays[5];
62
- uniform samplerCube skyboxTexture;
63
-
64
-
65
- float luminance(vec3 color) {
66
- return dot(color, vec3(0.299, 0.587, 0.114));
67
- }
68
-
69
- float sqr(float x) {
70
- return x * x;
71
- }
72
-
73
- float SchlickFresnel(float x) {
74
- x = clamp(1.0 - x, 0.0, 1.0);
75
- float x2 = x * x;
76
- return x2 * x2 * x;
77
- }
78
-
79
- float GTR1(float ndoth, float a) {
80
- float a2 = a * a;
81
- float t = 1.0 + (a2 - 1.0) * ndoth * ndoth;
82
- return (a2 - 1.0) / (3.1415 * log(a2) * t);
83
- }
84
-
85
- float AnisotropicGTR2(float ndoth, float hdotx, float hdoty, float ax, float ay) {
86
- return 1 / (3.1415 * ax * ay * sqr(sqr(hdotx / ax) + sqr(hdoty / ay) + sqr(ndoth)));
87
- }
88
-
89
- float SmithGGX(float alpha, float ndotl, float ndotv) {
90
- float a = ndotv * sqrt(alpha + ndotl * (ndotl - alpha * ndotl));
91
- float b = ndotl * sqrt(alpha + ndotv * (ndotv - alpha * ndotv));
92
-
93
- return 0.5 / (a + b);
94
- }
95
-
96
- float AnisotropicSmithGGX(float ndots, float sdotx, float sdoty, float ax, float ay) {
97
- return 1 / (ndots + sqrt(pow(sdotx * ax, 2) + pow(sdoty * ay, 2) + pow(ndots, 2)));
98
- }
99
-
100
- // Diffuse model as outlined by Burley: https://media.disneyanimation.com/uploads/production/publication_asset/48/asset/s2012_pbs_disney_brdf_notes_v3.pdf
101
- // Much help from Acerola's video on the topic: https://www.youtube.com/watch?v=KkOkx0FiHDA&t=570s
102
- LightResult PrincipledDiffuse(DirectionalLight light, Material mtl, vec3 albedo, float roughness, vec3 N, vec3 V, vec3 X, vec3 Y) {
103
-
104
- LightResult result;
105
-
106
- vec3 L = normalize(-light.direction); // light direction
107
- vec3 H = normalize(L + V); // half vector
108
-
109
- // Commonly used values
110
- float cos_theta_l = clamp(dot(N, L), 0.0, 1.0);
111
- float cos_theta_V = clamp(dot(N, V), 0.0, 1.0);
112
- float cos_theta_D = clamp(dot(L, H), 0.0, 1.0); // Also equal to dot(V, H) by symetry
113
-
114
- float ndoth = dot(N, H);
115
- float hdotx = dot(H, X);
116
- float hdoty = dot(H, Y);
117
- float ldotx = dot(L, X);
118
- float ldoty = dot(L, Y);
119
- float vdotx = dot(V, X);
120
- float vdoty = dot(V, Y);
121
-
122
- // Color Values
123
- vec3 surfaceColor = albedo;
124
- float Cdlum = luminance(surfaceColor);
125
-
126
- vec3 Ctint = Cdlum > 0.0 ? surfaceColor / Cdlum : vec3(1.0, 1.0, 1.0);
127
- vec3 Cspec0 = mix(mtl.specular * 0.08 * mix(vec3(1.0, 1.0, 1.0), Ctint, mtl.specularTint), surfaceColor, mtl.metallicness);
128
- vec3 Csheen = mix(vec3(1.0, 1.0, 1.0), Ctint, mtl.sheenTint);
129
-
130
- // Diffuse
131
- float FL = SchlickFresnel(cos_theta_l);
132
- float FV = SchlickFresnel(cos_theta_V);
133
- float Fss90 = cos_theta_D * cos_theta_D * roughness;
134
- float Fd90 = 0.5 + 2.0 * Fss90;
135
-
136
- float Fd = mix(1.0, Fd90, FL) * mix(1.0, Fd90, FV);
137
-
138
- // Subsurface
139
- float Fss = mix(1.0, Fss90, FL) * mix(1.0, Fss90, FV);
140
- float ss = 1.25 * (Fss * ((1 / (cos_theta_l + cos_theta_V)) - 0.5) + 0.5);
141
-
142
- // Specular
143
- float alpha = roughness * roughness;
144
- float aspect = sqrt(1.0 - 0.9 * mtl.anisotropic);
145
- float alpha_x = max(0.001, alpha / aspect);
146
- float alpha_y = max(0.001, alpha * aspect);
147
-
148
-
149
- // Anisotropic Microfacet Normal Distribution
150
- float Ds = AnisotropicGTR2(ndoth, hdotx, hdoty, alpha_x, alpha_y);
151
-
152
- // Geometric Attenuation
153
- float GalphaSquared = pow(0.5 + roughness * 0.5, 2);
154
- float GalphaX = max(0.001, GalphaSquared / aspect);
155
- float GalphaY = max(0.001, GalphaSquared * aspect);
156
- float G = AnisotropicSmithGGX(cos_theta_l, ldotx, ldoty, GalphaX, GalphaY);
157
- G = sqrt(G);
158
- G *= AnisotropicSmithGGX(cos_theta_V, vdotx, vdoty, GalphaX, GalphaY);
159
-
160
- // Fresnel Reflectance
161
- float FH = SchlickFresnel(cos_theta_D);
162
- vec3 F = mix(Cspec0, vec3(1.0, 1.0, 1.0), FH);
163
-
164
- // Sheen lobe
165
- vec3 Fsheen = FH * mtl.sheen * Csheen;
166
-
167
- // Clearcoat
168
- float Dr = GTR1(ndoth, mix(0.1, 0.001, mtl.clearcoatGloss)); // Normalized Isotropic GTR Gamma == 1
169
- float Fr = mix(0.04, 1.0, FH);
170
- float Gr = SmithGGX(cos_theta_l, cos_theta_V, 0.25);
171
-
172
- // Result for all lobes
173
-
174
- result.diffuse = max(vec3(0.0), (1 / 3.1415) * albedo * (mix(Fd, ss, mtl.subsurface) + Fsheen) * (1.0 - mtl.metallicness) * cos_theta_l);
175
- result.specular = max(vec3(0.0), vec3(Ds * F * G) * cos_theta_l);
176
- result.clearcoat = max(vec3(0.0), vec3(0.25 * mtl.clearcoat * Gr * Fr * Dr) * cos_theta_l);
177
-
178
- // Combine lobes and multiply weakening factor and light attributes
179
- return result;
180
- }
181
-
182
- vec3 getAlbedo(Material mtl, vec2 uv, float gamma) {
183
- vec3 albedo = mtl.color;
184
- if (bool(mtl.hasAlbedoMap)){
185
- albedo *= pow(texture(textureArrays[int(round(mtl.albedoMap.x))].array, vec3(uv, round(mtl.albedoMap.y))).rgb, vec3(gamma));
186
- }
187
- return albedo;
188
- }
189
-
190
- vec3 getNormal(Material mtl, mat3 TBN){
191
- // Isolate the normal vector from the TBN basis
192
- vec3 normal = TBN[2];
193
- // Apply normal map if the material has one
194
- if (bool(mtl.hasNormalMap)) {
195
- normal = texture(textureArrays[int(round(mtl.normalMap.x))].array, vec3(uv, round(mtl.normalMap.y))).rgb * 2.0 - 1.0;
196
- normal = normalize(TBN * normal);
197
- }
198
- // Return vector
199
- return normal;
200
- }
201
-
202
- float getAo(Material mtl, vec2 uv) {
203
- float ao;
204
- if (bool(mtl.hasAoMap)){
205
- ao = texture(textureArrays[int(round(mtl.aoMap.x))].array, vec3(uv, round(mtl.aoMap.y))).a;
206
- }
207
- else {
208
- ao = 1.0;
209
- }
210
- return ao;
211
- }
212
-
213
- float getRoughness(Material mtl, vec2 uv) {
214
- float roughness;
215
- if (bool(mtl.hasRoughnessMap)){
216
- roughness = texture(textureArrays[int(round(mtl.roughnessMap.x))].array, vec3(uv, round(mtl.roughnessMap.y))).a;
217
- }
218
- else {
219
- roughness = mtl.roughness;
220
- }
221
- return roughness;
222
- }
223
-
224
- void main() {
225
- float gamma = 2.2;
226
- vec3 viewDir = vec3(normalize(cameraPosition - position));
227
-
228
- // Get lighting vectors
229
- vec3 albedo = getAlbedo(mtl, uv, gamma);
230
- vec3 normal = getNormal(mtl, TBN);
231
- float ao = getAo(mtl, uv);
232
- float roughness = getRoughness(mtl, uv);
233
- vec3 tangent = TBN[0];
234
- vec3 bitangent = TBN[1];
235
-
236
- // Orthogonalize the tangent and bitangent according to the mapped normal vector
237
- tangent = tangent - dot(normal, tangent) * normal;
238
- bitangent = bitangent - dot(normal, bitangent) * normal - dot(tangent, bitangent) * tangent;
239
-
240
- // Lighting variables
241
- vec3 N = normalize(normal); // normal
242
- vec3 V = normalize(cameraPosition - position); // view vector
243
- vec3 X = normalize(tangent); // Tangent Vector
244
- vec3 Y = normalize(bitangent); // Bitangent Vector
245
-
246
- // Indirect lighting
247
- vec3 ambient_sky = texture(skyboxTexture, N).rgb;
248
- vec3 reflect_sky = texture(skyboxTexture, reflect(-V, N)).rgb;
249
-
250
- LightResult lightResult;
251
- lightResult.diffuse = vec3(0.0);
252
- lightResult.specular = vec3(0.0);
253
- lightResult.clearcoat = vec3(0.0);
254
-
255
- // Add result from each directional light in the scene
256
- for (int i = 0; i < numDirLights; i++) {
257
- // Caculate the light for the directional light
258
- LightResult dirLightResult = PrincipledDiffuse(dirLights[i], mtl, albedo, roughness, N, V, X, Y);
259
- vec3 lightFactor = dirLights[i].intensity * dirLights[i].color;
260
- // Add each lobe
261
- lightResult.diffuse += dirLightResult.diffuse * lightFactor;
262
- lightResult.specular += dirLightResult.specular * lightFactor;
263
- lightResult.clearcoat += dirLightResult.clearcoat * lightFactor;
264
- }
265
-
266
- lightResult.specular = min(vec3(1.0), lightResult.specular);
267
- lightResult.specular *= mix(vec3(1.0), reflect_sky, mtl.metallicness) * luminance(reflect_sky);
268
- lightResult.diffuse *= mix(vec3(1.0), ambient_sky, 0.25);
269
- lightResult.diffuse *= ao;
270
-
271
- vec3 finalColor = albedo * 0.3 * mix(vec3(1.0), reflect_sky, mtl.metallicness);
272
- finalColor += (lightResult.diffuse + lightResult.specular + lightResult.clearcoat);
273
-
274
- // light_result *= mix(vec3(1.0), texture(skyboxTexture, reflect(-V, N)).rgb, mtl.metallicness);
275
- // Gamma correction
276
- // finalColor = pow(finalColor, vec3(1.0/gamma));
277
-
278
- // Output fragment color
279
- float brightness = dot(fragColor.rgb, vec3(0.2126, 0.7152, 0.0722)) + dot(mtl.emissiveColor, vec3(1));
280
- fragColor = vec4(finalColor + mtl.emissiveColor, 1.0);
281
-
282
- // Filter out bright pixels for bloom
283
- // bloomColor = vec4(0.0, 0.0, 0.0, 1.0);
284
- if (brightness > 0.5) {
285
- bloomColor = vec4(fragColor.rgb, 1.0);
286
- }
287
- else{
288
- bloomColor = vec4(0.0, 0.0, 0.0, 1.0);
289
- }
1
+ #version 330 core
2
+
3
+ layout (location = 0) out vec4 fragColor;
4
+ layout (location = 1) out vec4 bloomColor;
5
+
6
+ // Structs needed for the shader
7
+ struct textArray {
8
+ sampler2DArray array;
9
+ };
10
+
11
+ struct DirectionalLight {
12
+ vec3 direction;
13
+ float intensity;
14
+ vec3 color;
15
+ float ambient;
16
+ };
17
+
18
+ // Material struct sent to fragment shader
19
+ struct Material {
20
+ vec3 color;
21
+ vec3 emissiveColor;
22
+ float roughness;
23
+ float subsurface;
24
+ float sheen;
25
+ float sheenTint;
26
+ float anisotropic;
27
+ float specular;
28
+ float metallicness;
29
+ float specularTint;
30
+ float clearcoat;
31
+ float clearcoatGloss;
32
+
33
+ int hasAlbedoMap;
34
+ vec2 albedoMap;
35
+ int hasNormalMap;
36
+ vec2 normalMap;
37
+ int hasRoughnessMap;
38
+ vec2 roughnessMap;
39
+ int hasAoMap;
40
+ vec2 aoMap;
41
+ };
42
+
43
+ struct LightResult {
44
+ vec3 diffuse;
45
+ vec3 specular;
46
+ vec3 clearcoat;
47
+ };
48
+
49
+ in vec2 uv;
50
+ in vec3 position;
51
+ in mat3 TBN;
52
+
53
+ // Material attributes
54
+ flat in Material mtl;
55
+
56
+ // Uniforms
57
+ uniform vec3 cameraPosition;
58
+ const int maxDirLights = 5;
59
+ uniform DirectionalLight dirLights[maxDirLights];
60
+ uniform int numDirLights;
61
+ uniform textArray textureArrays[5];
62
+ uniform samplerCube skyboxTexture;
63
+
64
+
65
+ float luminance(vec3 color) {
66
+ return dot(color, vec3(0.299, 0.587, 0.114));
67
+ }
68
+
69
+ float sqr(float x) {
70
+ return x * x;
71
+ }
72
+
73
+ float SchlickFresnel(float x) {
74
+ x = clamp(1.0 - x, 0.0, 1.0);
75
+ float x2 = x * x;
76
+ return x2 * x2 * x;
77
+ }
78
+
79
+ float GTR1(float ndoth, float a) {
80
+ float a2 = a * a;
81
+ float t = 1.0 + (a2 - 1.0) * ndoth * ndoth;
82
+ return (a2 - 1.0) / (3.1415 * log(a2) * t);
83
+ }
84
+
85
+ float AnisotropicGTR2(float ndoth, float hdotx, float hdoty, float ax, float ay) {
86
+ return 1 / (3.1415 * ax * ay * sqr(sqr(hdotx / ax) + sqr(hdoty / ay) + sqr(ndoth)));
87
+ }
88
+
89
+ float SmithGGX(float alpha, float ndotl, float ndotv) {
90
+ float a = ndotv * sqrt(alpha + ndotl * (ndotl - alpha * ndotl));
91
+ float b = ndotl * sqrt(alpha + ndotv * (ndotv - alpha * ndotv));
92
+
93
+ return 0.5 / (a + b);
94
+ }
95
+
96
+ float AnisotropicSmithGGX(float ndots, float sdotx, float sdoty, float ax, float ay) {
97
+ return 1 / (ndots + sqrt(pow(sdotx * ax, 2) + pow(sdoty * ay, 2) + pow(ndots, 2)));
98
+ }
99
+
100
+ // Diffuse model as outlined by Burley: https://media.disneyanimation.com/uploads/production/publication_asset/48/asset/s2012_pbs_disney_brdf_notes_v3.pdf
101
+ // Much help from Acerola's video on the topic: https://www.youtube.com/watch?v=KkOkx0FiHDA&t=570s
102
+ LightResult PrincipledDiffuse(DirectionalLight light, Material mtl, vec3 albedo, float roughness, vec3 N, vec3 V, vec3 X, vec3 Y) {
103
+
104
+ LightResult result;
105
+
106
+ vec3 L = normalize(-light.direction); // light direction
107
+ vec3 H = normalize(L + V); // half vector
108
+
109
+ // Commonly used values
110
+ float cos_theta_l = clamp(dot(N, L), 0.0, 1.0);
111
+ float cos_theta_V = clamp(dot(N, V), 0.0, 1.0);
112
+ float cos_theta_D = clamp(dot(L, H), 0.0, 1.0); // Also equal to dot(V, H) by symetry
113
+
114
+ float ndoth = dot(N, H);
115
+ float hdotx = dot(H, X);
116
+ float hdoty = dot(H, Y);
117
+ float ldotx = dot(L, X);
118
+ float ldoty = dot(L, Y);
119
+ float vdotx = dot(V, X);
120
+ float vdoty = dot(V, Y);
121
+
122
+ // Color Values
123
+ vec3 surfaceColor = albedo;
124
+ float Cdlum = luminance(surfaceColor);
125
+
126
+ vec3 Ctint = Cdlum > 0.0 ? surfaceColor / Cdlum : vec3(1.0, 1.0, 1.0);
127
+ vec3 Cspec0 = mix(mtl.specular * 0.08 * mix(vec3(1.0, 1.0, 1.0), Ctint, mtl.specularTint), surfaceColor, mtl.metallicness);
128
+ vec3 Csheen = mix(vec3(1.0, 1.0, 1.0), Ctint, mtl.sheenTint);
129
+
130
+ // Diffuse
131
+ float FL = SchlickFresnel(cos_theta_l);
132
+ float FV = SchlickFresnel(cos_theta_V);
133
+ float Fss90 = cos_theta_D * cos_theta_D * roughness;
134
+ float Fd90 = 0.5 + 2.0 * Fss90;
135
+
136
+ float Fd = mix(1.0, Fd90, FL) * mix(1.0, Fd90, FV);
137
+
138
+ // Subsurface
139
+ float Fss = mix(1.0, Fss90, FL) * mix(1.0, Fss90, FV);
140
+ float ss = 1.25 * (Fss * ((1 / (cos_theta_l + cos_theta_V)) - 0.5) + 0.5);
141
+
142
+ // Specular
143
+ float alpha = roughness * roughness;
144
+ float aspect = sqrt(1.0 - 0.9 * mtl.anisotropic);
145
+ float alpha_x = max(0.001, alpha / aspect);
146
+ float alpha_y = max(0.001, alpha * aspect);
147
+
148
+
149
+ // Anisotropic Microfacet Normal Distribution
150
+ float Ds = AnisotropicGTR2(ndoth, hdotx, hdoty, alpha_x, alpha_y);
151
+
152
+ // Geometric Attenuation
153
+ float GalphaSquared = pow(0.5 + roughness * 0.5, 2);
154
+ float GalphaX = max(0.001, GalphaSquared / aspect);
155
+ float GalphaY = max(0.001, GalphaSquared * aspect);
156
+ float G = AnisotropicSmithGGX(cos_theta_l, ldotx, ldoty, GalphaX, GalphaY);
157
+ G = sqrt(G);
158
+ G *= AnisotropicSmithGGX(cos_theta_V, vdotx, vdoty, GalphaX, GalphaY);
159
+
160
+ // Fresnel Reflectance
161
+ float FH = SchlickFresnel(cos_theta_D);
162
+ vec3 F = mix(Cspec0, vec3(1.0, 1.0, 1.0), FH);
163
+
164
+ // Sheen lobe
165
+ vec3 Fsheen = FH * mtl.sheen * Csheen;
166
+
167
+ // Clearcoat
168
+ float Dr = GTR1(ndoth, mix(0.1, 0.001, mtl.clearcoatGloss)); // Normalized Isotropic GTR Gamma == 1
169
+ float Fr = mix(0.04, 1.0, FH);
170
+ float Gr = SmithGGX(cos_theta_l, cos_theta_V, 0.25);
171
+
172
+ // Result for all lobes
173
+
174
+ result.diffuse = max(vec3(0.0), (1 / 3.1415) * albedo * (mix(Fd, ss, mtl.subsurface) + Fsheen) * (1.0 - mtl.metallicness) * cos_theta_l);
175
+ result.specular = max(vec3(0.0), vec3(Ds * F * G) * cos_theta_l);
176
+ result.clearcoat = max(vec3(0.0), vec3(0.25 * mtl.clearcoat * Gr * Fr * Dr) * cos_theta_l);
177
+
178
+ // Combine lobes and multiply weakening factor and light attributes
179
+ return result;
180
+ }
181
+
182
+ vec3 getAlbedo(Material mtl, vec2 uv, float gamma) {
183
+ vec3 albedo = mtl.color;
184
+ if (bool(mtl.hasAlbedoMap)){
185
+ albedo *= pow(texture(textureArrays[int(round(mtl.albedoMap.x))].array, vec3(uv, round(mtl.albedoMap.y))).rgb, vec3(gamma));
186
+ }
187
+ return albedo;
188
+ }
189
+
190
+ vec3 getNormal(Material mtl, mat3 TBN){
191
+ // Isolate the normal vector from the TBN basis
192
+ vec3 normal = TBN[2];
193
+ // Apply normal map if the material has one
194
+ if (bool(mtl.hasNormalMap)) {
195
+ normal = texture(textureArrays[int(round(mtl.normalMap.x))].array, vec3(uv, round(mtl.normalMap.y))).rgb * 2.0 - 1.0;
196
+ normal = normalize(TBN * normal);
197
+ }
198
+ // Return vector
199
+ return normal;
200
+ }
201
+
202
+ float getAo(Material mtl, vec2 uv) {
203
+ float ao;
204
+ if (bool(mtl.hasAoMap)){
205
+ ao = texture(textureArrays[int(round(mtl.aoMap.x))].array, vec3(uv, round(mtl.aoMap.y))).a;
206
+ }
207
+ else {
208
+ ao = 1.0;
209
+ }
210
+ return ao;
211
+ }
212
+
213
+ float getRoughness(Material mtl, vec2 uv) {
214
+ float roughness;
215
+ if (bool(mtl.hasRoughnessMap)){
216
+ roughness = texture(textureArrays[int(round(mtl.roughnessMap.x))].array, vec3(uv, round(mtl.roughnessMap.y))).a;
217
+ }
218
+ else {
219
+ roughness = mtl.roughness;
220
+ }
221
+ return roughness;
222
+ }
223
+
224
+ void main() {
225
+ float gamma = 2.2;
226
+ vec3 viewDir = vec3(normalize(cameraPosition - position));
227
+
228
+ // Get lighting vectors
229
+ vec3 albedo = getAlbedo(mtl, uv, gamma);
230
+ vec3 normal = getNormal(mtl, TBN);
231
+ float ao = getAo(mtl, uv);
232
+ float roughness = getRoughness(mtl, uv);
233
+ vec3 tangent = TBN[0];
234
+ vec3 bitangent = TBN[1];
235
+
236
+ // Orthogonalize the tangent and bitangent according to the mapped normal vector
237
+ tangent = tangent - dot(normal, tangent) * normal;
238
+ bitangent = bitangent - dot(normal, bitangent) * normal - dot(tangent, bitangent) * tangent;
239
+
240
+ // Lighting variables
241
+ vec3 N = normalize(normal); // normal
242
+ vec3 V = normalize(cameraPosition - position); // view vector
243
+ vec3 X = normalize(tangent); // Tangent Vector
244
+ vec3 Y = normalize(bitangent); // Bitangent Vector
245
+
246
+ // Indirect lighting
247
+ vec3 ambient_sky = texture(skyboxTexture, N).rgb;
248
+ vec3 reflect_sky = texture(skyboxTexture, reflect(-V, N)).rgb;
249
+
250
+ LightResult lightResult;
251
+ lightResult.diffuse = vec3(0.0);
252
+ lightResult.specular = vec3(0.0);
253
+ lightResult.clearcoat = vec3(0.0);
254
+
255
+ // Add result from each directional light in the scene
256
+ for (int i = 0; i < numDirLights; i++) {
257
+ // Caculate the light for the directional light
258
+ LightResult dirLightResult = PrincipledDiffuse(dirLights[i], mtl, albedo, roughness, N, V, X, Y);
259
+ vec3 lightFactor = dirLights[i].intensity * dirLights[i].color;
260
+ // Add each lobe
261
+ lightResult.diffuse += dirLightResult.diffuse * lightFactor;
262
+ lightResult.specular += dirLightResult.specular * lightFactor;
263
+ lightResult.clearcoat += dirLightResult.clearcoat * lightFactor;
264
+ }
265
+
266
+ lightResult.specular = min(vec3(1.0), lightResult.specular);
267
+ lightResult.specular *= mix(vec3(1.0), reflect_sky, mtl.metallicness) * luminance(reflect_sky);
268
+ lightResult.diffuse *= mix(vec3(1.0), ambient_sky, 0.25);
269
+ lightResult.diffuse *= ao;
270
+
271
+ vec3 finalColor = albedo * 0.3 * mix(vec3(1.0), reflect_sky, mtl.metallicness);
272
+ finalColor += (lightResult.diffuse + lightResult.specular + lightResult.clearcoat);
273
+
274
+ // light_result *= mix(vec3(1.0), texture(skyboxTexture, reflect(-V, N)).rgb, mtl.metallicness);
275
+ // Gamma correction
276
+ // finalColor = pow(finalColor, vec3(1.0/gamma));
277
+
278
+ // Output fragment color
279
+ float brightness = dot(fragColor.rgb, vec3(0.2126, 0.7152, 0.0722)) + dot(mtl.emissiveColor, vec3(1));
280
+ fragColor = vec4(finalColor + mtl.emissiveColor, 1.0);
281
+
282
+ // Filter out bright pixels for bloom
283
+ // bloomColor = vec4(0.0, 0.0, 0.0, 1.0);
284
+ if (brightness > 0.5) {
285
+ bloomColor = vec4(fragColor.rgb, 1.0);
286
+ }
287
+ else{
288
+ bloomColor = vec4(0.0, 0.0, 0.0, 1.0);
289
+ }
290
290
  }