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