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