bg2e-js 2.3.11 → 2.3.13

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.
Files changed (148) hide show
  1. package/dist/bg2e-js.js +356 -326
  2. package/dist/bg2e-js.js.map +1 -1
  3. package/package.json +56 -56
  4. package/src/app/AppController.ts +39 -39
  5. package/src/app/Bg2KeyboardEvent.ts +54 -54
  6. package/src/app/Bg2MouseEvent.ts +82 -82
  7. package/src/app/Bg2TouchEvent.ts +18 -18
  8. package/src/app/Canvas.ts +108 -108
  9. package/src/app/EventBase.ts +10 -10
  10. package/src/app/MainLoop.ts +273 -273
  11. package/src/app/index.ts +24 -24
  12. package/src/base/Color.ts +134 -134
  13. package/src/base/Environment.ts +183 -183
  14. package/src/base/Light.ts +192 -192
  15. package/src/base/Material.ts +620 -620
  16. package/src/base/PolyList.ts +365 -365
  17. package/src/base/Texture.ts +620 -620
  18. package/src/base/index.ts +81 -81
  19. package/src/db/Bg2LoaderPlugin.ts +143 -143
  20. package/src/db/DBPluginApi.ts +48 -48
  21. package/src/db/Loader.ts +116 -116
  22. package/src/db/LoaderPlugin.ts +34 -34
  23. package/src/db/MtlParser.ts +7 -7
  24. package/src/db/ObjLoaderPlugin.ts +54 -54
  25. package/src/db/ObjParser.ts +252 -252
  26. package/src/db/ObjWriterPlugin.ts +18 -18
  27. package/src/db/VitscnjLoaderPlugin.ts +112 -112
  28. package/src/db/Writer.ts +52 -52
  29. package/src/db/WriterPlugin.ts +22 -22
  30. package/src/db/index.ts +44 -44
  31. package/src/debug/DebugRenderer.ts +173 -173
  32. package/src/debug/WebGLTextureViewer.ts +75 -75
  33. package/src/debug/index.ts +6 -6
  34. package/src/index.html +11 -11
  35. package/src/index.ts +33 -33
  36. package/src/manipulation/SelectionBuffer.ts +81 -81
  37. package/src/manipulation/SelectionHighlight.ts +105 -84
  38. package/src/manipulation/SelectionIdAssignVisitor.ts +96 -96
  39. package/src/manipulation/SelectionManager.ts +196 -188
  40. package/src/manipulation/SelectionMode.ts +6 -6
  41. package/src/math/Mat3.ts +259 -259
  42. package/src/math/Mat4.ts +710 -710
  43. package/src/math/MatrixStrategy.ts +25 -25
  44. package/src/math/Quat.ts +65 -65
  45. package/src/math/Vec.ts +753 -753
  46. package/src/math/constants.ts +46 -46
  47. package/src/math/functions.ts +103 -103
  48. package/src/math/index.ts +74 -74
  49. package/src/phsics/joint.ts +137 -137
  50. package/src/primitives/arrow.ts +57 -57
  51. package/src/primitives/cone.ts +138 -138
  52. package/src/primitives/cube.ts +60 -60
  53. package/src/primitives/cylinder.ts +216 -216
  54. package/src/primitives/index.ts +13 -13
  55. package/src/primitives/plane.ts +31 -31
  56. package/src/primitives/sphere.ts +809 -809
  57. package/src/react/useBg2e.ts +69 -69
  58. package/src/render/BRDFIntegrationMap.ts +4 -4
  59. package/src/render/Environment.ts +135 -135
  60. package/src/render/FrameBuffer.ts +35 -35
  61. package/src/render/MaterialRenderer.ts +34 -34
  62. package/src/render/Pipeline.ts +108 -108
  63. package/src/render/PolyListRenderer.ts +47 -47
  64. package/src/render/RenderBuffer.ts +197 -197
  65. package/src/render/RenderQueue.ts +198 -198
  66. package/src/render/RenderState.ts +116 -116
  67. package/src/render/Renderer.ts +248 -248
  68. package/src/render/SceneAppController.ts +250 -250
  69. package/src/render/SceneRenderer.ts +387 -387
  70. package/src/render/Shader.ts +32 -32
  71. package/src/render/ShadowRenderer.ts +176 -176
  72. package/src/render/SkyCube.ts +105 -105
  73. package/src/render/SkySphere.ts +117 -117
  74. package/src/render/TextureMergerRenderer.ts +70 -70
  75. package/src/render/TextureRenderer.ts +34 -34
  76. package/src/render/index.ts +67 -67
  77. package/src/render/webgl/FrameBuffer.ts +9 -9
  78. package/src/render/webgl/MaterialRenderer.ts +112 -112
  79. package/src/render/webgl/Pipeline.ts +88 -88
  80. package/src/render/webgl/PolyListRenderer.ts +260 -260
  81. package/src/render/webgl/RenderBuffer.ts +226 -226
  82. package/src/render/webgl/Renderer.ts +262 -262
  83. package/src/render/webgl/SceneRenderer.ts +67 -67
  84. package/src/render/webgl/ShaderProgram.ts +424 -424
  85. package/src/render/webgl/ShadowRenderer.ts +6 -6
  86. package/src/render/webgl/SkyCube.ts +15 -15
  87. package/src/render/webgl/SkySphere.ts +15 -15
  88. package/src/render/webgl/State.ts +152 -152
  89. package/src/render/webgl/TextureRenderer.ts +167 -167
  90. package/src/render/webgl/VertexBuffer.ts +137 -137
  91. package/src/render/webgl/index.ts +35 -35
  92. package/src/scene/Camera.ts +458 -458
  93. package/src/scene/Chain.ts +44 -44
  94. package/src/scene/ChainJoint.ts +58 -58
  95. package/src/scene/Component.ts +177 -177
  96. package/src/scene/ComponentMap.ts +106 -106
  97. package/src/scene/Drawable.ts +154 -154
  98. package/src/scene/EnvironmentComponent.ts +141 -141
  99. package/src/scene/FindNodeVisitor.ts +59 -59
  100. package/src/scene/LightComponent.ts +154 -154
  101. package/src/scene/MatrixState.ts +46 -46
  102. package/src/scene/Node.ts +328 -328
  103. package/src/scene/NodeVisitor.ts +15 -15
  104. package/src/scene/OrbitCameraController.ts +450 -450
  105. package/src/scene/SmoothOrbitCameraController.ts +99 -99
  106. package/src/scene/Transform.ts +73 -73
  107. package/src/scene/index.ts +60 -60
  108. package/src/shaders/BasicDiffuseColorShader.ts +111 -111
  109. package/src/shaders/BasicPBRLightShader.ts +276 -276
  110. package/src/shaders/DebugRenderShader.ts +97 -97
  111. package/src/shaders/DepthRenderShader.ts +127 -127
  112. package/src/shaders/IrradianceMapCubeShader.ts +115 -115
  113. package/src/shaders/PBRLightIBLShader.ts +486 -486
  114. package/src/shaders/PickSelectionShader.ts +101 -101
  115. package/src/shaders/PresentDebugFramebufferShader.ts +118 -118
  116. package/src/shaders/PresentTextureShader.ts +99 -99
  117. package/src/shaders/SelectionHighlightShader.ts +143 -127
  118. package/src/shaders/ShaderFunction.ts +318 -318
  119. package/src/shaders/SkyCubeShader.ts +93 -93
  120. package/src/shaders/SkySphereShader.ts +102 -102
  121. package/src/shaders/SpecularMapCubeShader.ts +164 -164
  122. package/src/shaders/TextureMergerShader.ts +171 -171
  123. package/src/shaders/index.ts +36 -36
  124. package/src/shaders/webgl/color_correction.glsl +47 -47
  125. package/src/shaders/webgl/constants.glsl +6 -6
  126. package/src/shaders/webgl/index.ts +70 -70
  127. package/src/shaders/webgl/normal_map.glsl +9 -9
  128. package/src/shaders/webgl/pbr.glsl +173 -173
  129. package/src/shaders/webgl/uniforms.glsl +91 -91
  130. package/src/shaders/webgl_shader_lib.ts +213 -213
  131. package/src/tools/BinaryResourceProvider.ts +14 -14
  132. package/src/tools/ImageResourceProvider.ts +66 -66
  133. package/src/tools/MaterialModifier.ts +446 -446
  134. package/src/tools/Resource.ts +203 -203
  135. package/src/tools/ResourceProvider.ts +69 -69
  136. package/src/tools/TextResourceProvider.ts +24 -24
  137. package/src/tools/TextureCache.ts +51 -51
  138. package/src/tools/TextureResourceDatabase.ts +100 -100
  139. package/src/tools/UserAgent.ts +362 -362
  140. package/src/tools/VideoResourceProvider.ts +50 -50
  141. package/src/tools/WriteStrategy.ts +22 -22
  142. package/src/tools/base64.ts +11 -11
  143. package/src/tools/crypto.ts +19 -19
  144. package/src/tools/endiantess.ts +13 -13
  145. package/src/tools/image.ts +18 -18
  146. package/src/tools/index.ts +41 -41
  147. package/src/tools/processType.ts +39 -39
  148. package/src/vite-env.d.ts +12 -12
@@ -1,173 +1,173 @@
1
-
2
- #include "lib/constants.glsl"
3
-
4
- vec3 fresnelSchlick(float cosTheta, vec3 F0)
5
- {
6
- return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
7
- }
8
-
9
- vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness)
10
- {
11
- // Use the Schlick approximation for fresnel with roughness
12
- return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
13
- }
14
-
15
- float distributionGGX(vec3 normal, vec3 halfVector, float roughness)
16
- {
17
- float a = roughness * roughness;
18
- float a2 = a * a;
19
-
20
- float NdotH = max(dot(normal, halfVector), 0.0);
21
- float NdotH2 = NdotH * NdotH;
22
-
23
- float denom = NdotH2 * (a2 - 1.0) + 1.0;
24
-
25
- return a2 / (PI * denom * denom);
26
- }
27
-
28
- float geometrySchlickGGX(float NdotV, float roughness)
29
- {
30
- float r = (roughness + 1.0);
31
- float k = (r * r) / 8.0;
32
-
33
- float num = NdotV;
34
- float denom = NdotV * (1.0 - k) + k;
35
-
36
- return num / denom;
37
- }
38
-
39
- float geometrySmith(vec3 normal, vec3 viewDir, vec3 lightDir, float roughness)
40
- {
41
- float NdotV = max(dot(normal, viewDir), 0.0);
42
- float NdotL = max(dot(normal, lightDir), 0.0);
43
- float ggx2 = geometrySchlickGGX(NdotV, roughness);
44
- float ggx1 = geometrySchlickGGX(NdotL, roughness);
45
- return ggx1 * ggx2;
46
- }
47
-
48
- float calcAttenuation(vec3 lightPosition, vec3 fragPosition)
49
- {
50
- float distance = length(lightPosition - fragPosition);
51
- return 1.0 / (distance * distance);
52
- }
53
-
54
- vec3 calcRadiancePoint( Light light, vec3 viewDir, vec3 fragPos, float metallic, float roughness, vec3 F0, vec3 normal, vec3 albedo, float sheenIntensity, vec3 sheenColor, float ambientOcclussion) {
55
- // calculate per-light radiance
56
- vec3 L = normalize(light.position - fragPos);
57
- vec3 H = normalize(viewDir + L);
58
- float attenuation = calcAttenuation(light.position, fragPos);
59
- vec3 radiance = light.color.rgb * light.intensity * attenuation;
60
-
61
- // cook-torrance brdf
62
- float NDF = distributionGGX(normal, H, roughness);
63
- float G = geometrySmith(normal, viewDir, L, roughness);
64
- vec3 F = fresnelSchlick(max(dot(H, viewDir), 0.0), F0);
65
-
66
- vec3 kS = F;
67
- vec3 kD = vec3(1.0) - kS;
68
- kD *= 1.0 - metallic;
69
-
70
- vec3 numerator = NDF * G * F;
71
- float denominator = 4.0 * max(dot(normal, viewDir), 0.0) * max(dot(normal, L), 0.0) + 0.0001;
72
- vec3 specular = numerator / denominator;
73
-
74
- // add to outgoing radiance Lo
75
- float NdotL = max(dot(normal, L), 0.0);
76
- vec3 base = (kD * albedo / PI + specular) * radiance * NdotL;
77
- vec3 sheen = calcSheen(normal, viewDir, sheenColor, sheenIntensity) * ambientOcclussion;
78
- return base + sheen;
79
- }
80
-
81
- vec3 calcRadianceDirectional(Light light, vec3 viewDir, vec3 fragPos, float metallic, float roughness, vec3 F0, vec3 normal, vec3 albedo, float sheenIntensity, vec3 sheenColor, float ambientOcclussion)
82
- {
83
- vec3 L = normalize(-light.direction);
84
- vec3 H = normalize(viewDir + L);
85
- vec3 radiance = light.color.rgb * light.intensity;
86
-
87
- // cook-torrance brdf
88
- float NDF = distributionGGX(normal, H, roughness);
89
- float G = geometrySmith(normal, viewDir, L, roughness);
90
- vec3 F = fresnelSchlick(max(dot(H, viewDir), 0.0), F0);
91
-
92
- vec3 kS = F;
93
- vec3 kD = vec3(1.0) - kS;
94
- kD *= 1.0 - metallic;
95
-
96
- vec3 numerator = NDF * G * F;
97
- float denominator = 4.0 * max(dot(normal, viewDir), 0.0) * max(dot(normal, L), 0.0) + 0.0001;
98
- vec3 specular = numerator / denominator;
99
-
100
- // add to outgoing radiance Lo
101
- float NdotL = max(dot(normal, L), 0.0);
102
- vec3 base = (kD * albedo / PI + specular) * radiance * NdotL;
103
- vec3 sheen = calcSheen(normal, viewDir, sheenColor, sheenIntensity) * ambientOcclussion;
104
- return base + sheen;
105
- }
106
-
107
- vec3 calcRadiance( Light light, vec3 viewDir, vec3 fragPos, float metallic, float roughness, vec3 F0, vec3 normal, vec3 albedo, float sheenIntensity, vec3 sheenColor, float ambientOcclussion) {
108
- if (light.type == LIGHT_TYPE_POINT)
109
- {
110
- return calcRadiancePoint(light, viewDir, fragPos, metallic, roughness, F0, normal, albedo, sheenIntensity, sheenColor, ambientOcclussion);
111
- }
112
- else if (light.type == LIGHT_TYPE_DIRECTIONAL)
113
- {
114
- return calcRadianceDirectional(light, viewDir, fragPos, metallic, roughness, F0, normal, albedo, sheenIntensity, sheenColor, ambientOcclussion);
115
- }
116
- return vec3(0.0);
117
- }
118
-
119
- vec3 getPrefilteredColor(float roughness,vec3 refVec,samplerCube irrMap,samplerCube specMap,samplerCube envMap) {
120
- vec3 specMap0 = textureCube(envMap, refVec).rgb;
121
- vec3 specMap1 = textureCube(specMap, refVec).rgb;
122
- vec3 specMap2 = textureCube(irrMap, refVec).rgb;
123
-
124
- if (roughness < 0.4) {
125
- return mix(specMap0, specMap1, roughness / 0.4);
126
- }
127
- return mix(specMap1, specMap2, (roughness - 0.4) / 0.6);
128
- }
129
-
130
- vec3 calcAmbientLight( vec3 viewDir, vec3 normal, vec3 F0, vec3 albedo, float metallic, float roughness, samplerCube irradianceMap, samplerCube prefilteredEnvMap, samplerCube envMap, sampler2D brdfLUT, float ambientOcclussion, float sheenIntensity, vec3 sheenColor, vec3 shadowColor, float ambientIntensity) {
131
- vec3 R = reflect(-viewDir, normal);
132
-
133
- vec3 F = fresnelSchlickRoughness(max(dot(normal, viewDir), 0.0), F0, roughness) * max(shadowColor, vec3(0.8));
134
-
135
- vec3 Ks = F;
136
- vec3 Kd = 1.0 - Ks;
137
- Kd *= 1.0 - metallic;
138
-
139
- vec3 irradiance = textureCube(irradianceMap, normal).rgb * ambientIntensity;
140
- vec3 diffuse = irradiance * albedo;
141
-
142
- vec3 prefilteredColor = getPrefilteredColor(roughness, R, irradianceMap, prefilteredEnvMap, envMap);
143
-
144
- vec2 brdfUV = vec2(clamp(max(dot(normal, viewDir), 0.0), 0.01, 0.99), roughness);
145
- vec2 envBRDF = texture2D(brdfLUT, brdfUV).rg;
146
- vec3 specular = prefilteredColor * (F * envBRDF.x + envBRDF.y);
147
-
148
- vec3 base = (Kd * diffuse + specular) * ambientOcclussion;
149
- vec3 sheen = calcSheen(normal, viewDir, sheenColor, sheenIntensity) * ambientOcclussion * shadowColor;
150
- return base + sheen;
151
- }
152
-
153
- // TODO: Extract this function to a shadow map shader function
154
- vec3 getShadowColor(vec4 positionFromLightPov, sampler2D shadowMap, float shadowBias, float shadowStrength) {
155
- // The vertex location rendered from the light source is almost in
156
- // normalized device coordinates (NDC), but the perspective division
157
- // has not been performed yet. We need to divide by w to get the
158
- // vertex location in range [-1, +1]
159
- vec3 shadowCoord = positionFromLightPov.xyz / positionFromLightPov.w;
160
-
161
- // Convert from NDC to texture coordinates
162
- shadowCoord = shadowCoord * 0.5 + 0.5;
163
-
164
- if (shadowCoord.x >= 0.0 && shadowCoord.x <= 1.0 &&
165
- shadowCoord.y >= 0.0 && shadowCoord.y <= 1.0)
166
- {
167
- float shadowDepth = texture2D(shadowMap, shadowCoord.xy).r;
168
- if (shadowCoord.z > shadowDepth + shadowBias) {
169
- return vec3(1.0 - shadowStrength);
170
- }
171
- }
172
- return vec3(1.0);
173
- }
1
+
2
+ #include "lib/constants.glsl"
3
+
4
+ vec3 fresnelSchlick(float cosTheta, vec3 F0)
5
+ {
6
+ return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
7
+ }
8
+
9
+ vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness)
10
+ {
11
+ // Use the Schlick approximation for fresnel with roughness
12
+ return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
13
+ }
14
+
15
+ float distributionGGX(vec3 normal, vec3 halfVector, float roughness)
16
+ {
17
+ float a = roughness * roughness;
18
+ float a2 = a * a;
19
+
20
+ float NdotH = max(dot(normal, halfVector), 0.0);
21
+ float NdotH2 = NdotH * NdotH;
22
+
23
+ float denom = NdotH2 * (a2 - 1.0) + 1.0;
24
+
25
+ return a2 / (PI * denom * denom);
26
+ }
27
+
28
+ float geometrySchlickGGX(float NdotV, float roughness)
29
+ {
30
+ float r = (roughness + 1.0);
31
+ float k = (r * r) / 8.0;
32
+
33
+ float num = NdotV;
34
+ float denom = NdotV * (1.0 - k) + k;
35
+
36
+ return num / denom;
37
+ }
38
+
39
+ float geometrySmith(vec3 normal, vec3 viewDir, vec3 lightDir, float roughness)
40
+ {
41
+ float NdotV = max(dot(normal, viewDir), 0.0);
42
+ float NdotL = max(dot(normal, lightDir), 0.0);
43
+ float ggx2 = geometrySchlickGGX(NdotV, roughness);
44
+ float ggx1 = geometrySchlickGGX(NdotL, roughness);
45
+ return ggx1 * ggx2;
46
+ }
47
+
48
+ float calcAttenuation(vec3 lightPosition, vec3 fragPosition)
49
+ {
50
+ float distance = length(lightPosition - fragPosition);
51
+ return 1.0 / (distance * distance);
52
+ }
53
+
54
+ vec3 calcRadiancePoint( Light light, vec3 viewDir, vec3 fragPos, float metallic, float roughness, vec3 F0, vec3 normal, vec3 albedo, float sheenIntensity, vec3 sheenColor, float ambientOcclussion) {
55
+ // calculate per-light radiance
56
+ vec3 L = normalize(light.position - fragPos);
57
+ vec3 H = normalize(viewDir + L);
58
+ float attenuation = calcAttenuation(light.position, fragPos);
59
+ vec3 radiance = light.color.rgb * light.intensity * attenuation;
60
+
61
+ // cook-torrance brdf
62
+ float NDF = distributionGGX(normal, H, roughness);
63
+ float G = geometrySmith(normal, viewDir, L, roughness);
64
+ vec3 F = fresnelSchlick(max(dot(H, viewDir), 0.0), F0);
65
+
66
+ vec3 kS = F;
67
+ vec3 kD = vec3(1.0) - kS;
68
+ kD *= 1.0 - metallic;
69
+
70
+ vec3 numerator = NDF * G * F;
71
+ float denominator = 4.0 * max(dot(normal, viewDir), 0.0) * max(dot(normal, L), 0.0) + 0.0001;
72
+ vec3 specular = numerator / denominator;
73
+
74
+ // add to outgoing radiance Lo
75
+ float NdotL = max(dot(normal, L), 0.0);
76
+ vec3 base = (kD * albedo / PI + specular) * radiance * NdotL;
77
+ vec3 sheen = calcSheen(normal, viewDir, sheenColor, sheenIntensity) * ambientOcclussion;
78
+ return base + sheen;
79
+ }
80
+
81
+ vec3 calcRadianceDirectional(Light light, vec3 viewDir, vec3 fragPos, float metallic, float roughness, vec3 F0, vec3 normal, vec3 albedo, float sheenIntensity, vec3 sheenColor, float ambientOcclussion)
82
+ {
83
+ vec3 L = normalize(-light.direction);
84
+ vec3 H = normalize(viewDir + L);
85
+ vec3 radiance = light.color.rgb * light.intensity;
86
+
87
+ // cook-torrance brdf
88
+ float NDF = distributionGGX(normal, H, roughness);
89
+ float G = geometrySmith(normal, viewDir, L, roughness);
90
+ vec3 F = fresnelSchlick(max(dot(H, viewDir), 0.0), F0);
91
+
92
+ vec3 kS = F;
93
+ vec3 kD = vec3(1.0) - kS;
94
+ kD *= 1.0 - metallic;
95
+
96
+ vec3 numerator = NDF * G * F;
97
+ float denominator = 4.0 * max(dot(normal, viewDir), 0.0) * max(dot(normal, L), 0.0) + 0.0001;
98
+ vec3 specular = numerator / denominator;
99
+
100
+ // add to outgoing radiance Lo
101
+ float NdotL = max(dot(normal, L), 0.0);
102
+ vec3 base = (kD * albedo / PI + specular) * radiance * NdotL;
103
+ vec3 sheen = calcSheen(normal, viewDir, sheenColor, sheenIntensity) * ambientOcclussion;
104
+ return base + sheen;
105
+ }
106
+
107
+ vec3 calcRadiance( Light light, vec3 viewDir, vec3 fragPos, float metallic, float roughness, vec3 F0, vec3 normal, vec3 albedo, float sheenIntensity, vec3 sheenColor, float ambientOcclussion) {
108
+ if (light.type == LIGHT_TYPE_POINT)
109
+ {
110
+ return calcRadiancePoint(light, viewDir, fragPos, metallic, roughness, F0, normal, albedo, sheenIntensity, sheenColor, ambientOcclussion);
111
+ }
112
+ else if (light.type == LIGHT_TYPE_DIRECTIONAL)
113
+ {
114
+ return calcRadianceDirectional(light, viewDir, fragPos, metallic, roughness, F0, normal, albedo, sheenIntensity, sheenColor, ambientOcclussion);
115
+ }
116
+ return vec3(0.0);
117
+ }
118
+
119
+ vec3 getPrefilteredColor(float roughness,vec3 refVec,samplerCube irrMap,samplerCube specMap,samplerCube envMap) {
120
+ vec3 specMap0 = textureCube(envMap, refVec).rgb;
121
+ vec3 specMap1 = textureCube(specMap, refVec).rgb;
122
+ vec3 specMap2 = textureCube(irrMap, refVec).rgb;
123
+
124
+ if (roughness < 0.4) {
125
+ return mix(specMap0, specMap1, roughness / 0.4);
126
+ }
127
+ return mix(specMap1, specMap2, (roughness - 0.4) / 0.6);
128
+ }
129
+
130
+ vec3 calcAmbientLight( vec3 viewDir, vec3 normal, vec3 F0, vec3 albedo, float metallic, float roughness, samplerCube irradianceMap, samplerCube prefilteredEnvMap, samplerCube envMap, sampler2D brdfLUT, float ambientOcclussion, float sheenIntensity, vec3 sheenColor, vec3 shadowColor, float ambientIntensity) {
131
+ vec3 R = reflect(-viewDir, normal);
132
+
133
+ vec3 F = fresnelSchlickRoughness(max(dot(normal, viewDir), 0.0), F0, roughness) * max(shadowColor, vec3(0.8));
134
+
135
+ vec3 Ks = F;
136
+ vec3 Kd = 1.0 - Ks;
137
+ Kd *= 1.0 - metallic;
138
+
139
+ vec3 irradiance = textureCube(irradianceMap, normal).rgb * ambientIntensity;
140
+ vec3 diffuse = irradiance * albedo;
141
+
142
+ vec3 prefilteredColor = getPrefilteredColor(roughness, R, irradianceMap, prefilteredEnvMap, envMap);
143
+
144
+ vec2 brdfUV = vec2(clamp(max(dot(normal, viewDir), 0.0), 0.01, 0.99), roughness);
145
+ vec2 envBRDF = texture2D(brdfLUT, brdfUV).rg;
146
+ vec3 specular = prefilteredColor * (F * envBRDF.x + envBRDF.y);
147
+
148
+ vec3 base = (Kd * diffuse + specular) * ambientOcclussion;
149
+ vec3 sheen = calcSheen(normal, viewDir, sheenColor, sheenIntensity) * ambientOcclussion * shadowColor;
150
+ return base + sheen;
151
+ }
152
+
153
+ // TODO: Extract this function to a shadow map shader function
154
+ vec3 getShadowColor(vec4 positionFromLightPov, sampler2D shadowMap, float shadowBias, float shadowStrength) {
155
+ // The vertex location rendered from the light source is almost in
156
+ // normalized device coordinates (NDC), but the perspective division
157
+ // has not been performed yet. We need to divide by w to get the
158
+ // vertex location in range [-1, +1]
159
+ vec3 shadowCoord = positionFromLightPov.xyz / positionFromLightPov.w;
160
+
161
+ // Convert from NDC to texture coordinates
162
+ shadowCoord = shadowCoord * 0.5 + 0.5;
163
+
164
+ if (shadowCoord.x >= 0.0 && shadowCoord.x <= 1.0 &&
165
+ shadowCoord.y >= 0.0 && shadowCoord.y <= 1.0)
166
+ {
167
+ float shadowDepth = texture2D(shadowMap, shadowCoord.xy).r;
168
+ if (shadowCoord.z > shadowDepth + shadowBias) {
169
+ return vec3(1.0 - shadowStrength);
170
+ }
171
+ }
172
+ return vec3(1.0);
173
+ }
@@ -1,91 +1,91 @@
1
-
2
- struct PBRMaterialData
3
- {
4
- vec4 albedo;
5
-
6
- vec2 albedoScale;
7
- vec2 normalScale;
8
- vec2 metalnessScale;
9
- vec2 roughnessScale;
10
- vec2 lightEmissionScale;
11
-
12
- float metalness;
13
- float roughness;
14
- float lightEmission;
15
-
16
- int albedoUVSet;
17
- int normalUVSet;
18
- int metalnessUVSet;
19
- int roughnessUVSet;
20
- int lightEmissionUVSet;
21
- int aoUVSet;
22
-
23
- vec4 fresnelTint;
24
- vec4 sheenColor;
25
- float sheenIntensity;
26
- };
27
-
28
- struct Light
29
- {
30
- vec3 position;
31
- float intensity;
32
- vec4 color;
33
- vec3 direction;
34
- int type;
35
- };
36
-
37
- vec4 sampleAlbedo(sampler2D tex, vec2 uv0, vec2 uv1, PBRMaterialData mat, float gamma)
38
- {
39
- vec2 uv = mat.albedoUVSet == 0 ? uv0 : uv1;
40
- vec4 texColor = texture2D(tex, uv * mat.albedoScale);
41
- return vec4(SRGB2Lineal(texColor, gamma).rgb * mat.albedo.rgb, texColor.a * mat.albedo.a);
42
- }
43
-
44
- float sampleMetallic(sampler2D tex, vec2 uv0, vec2 uv1, PBRMaterialData mat)
45
- {
46
- vec2 uv = mat.metalnessUVSet == 0 ? uv0 : uv1;
47
- return texture2D(tex, uv * mat.metalnessScale).r * mat.metalness;
48
- }
49
-
50
- float sampleRoughness(sampler2D tex, vec2 uv0, vec2 uv1, PBRMaterialData mat)
51
- {
52
- const float minRoughness = 0.05; // Minimum roughness value: even a mirror have some roughness
53
- const float maxRoughness = 0.98; // Maximum roughness value: avoid completely diffuse surfaces
54
- vec2 uv = mat.roughnessUVSet == 0 ? uv0 : uv1;
55
- return min(max(texture2D(tex, uv * mat.roughnessScale).g * mat.roughness, minRoughness), maxRoughness);
56
- }
57
-
58
-
59
- float sampleLightEmission(sampler2D tex, vec2 uv0, vec2 uv1, PBRMaterialData mat)
60
- {
61
- vec2 uv = mat.lightEmissionUVSet == 0 ? uv0 : uv1;
62
- return texture2D(tex, uv * mat.lightEmissionScale).b * mat.lightEmission;
63
- }
64
-
65
- vec3 sampleNormal(sampler2D tex, vec2 uv0, vec2 uv1, PBRMaterialData mat, mat3 TBN)
66
- {
67
- vec2 uv = mat.normalUVSet == 0 ? uv0 : uv1;
68
- vec3 normal = texture2D(tex, uv * mat.normalScale).xyz * 2.0 - 1.0;
69
- return normalize(TBN * normal);
70
- }
71
-
72
- float sampleAmbientOcclussion(sampler2D tex, vec2 uv0, vec2 uv1, PBRMaterialData mat)
73
- {
74
- vec2 uv = mat.aoUVSet == 0 ? uv0 : uv1;
75
- return texture2D(tex, uv).a;
76
- }
77
-
78
- vec3 calcF0(vec3 albedo, PBRMaterialData mat)
79
- {
80
- return mix(vec3(0.04), albedo, mat.metalness) * mat.fresnelTint.rgb;
81
- }
82
-
83
- vec3 calcSheen(vec3 normal, vec3 viewDir, vec3 sheenColor, float sheenIntensity)
84
- {
85
- float NdotV = max(dot(normal, viewDir), 0.0);
86
- float facing = 1.0 - NdotV;
87
-
88
- // Adjustable power: controls how concentrated the brightness is
89
- float falloff = pow(facing, 5.0);
90
- return sheenColor * falloff * sheenIntensity;
91
- }
1
+
2
+ struct PBRMaterialData
3
+ {
4
+ vec4 albedo;
5
+
6
+ vec2 albedoScale;
7
+ vec2 normalScale;
8
+ vec2 metalnessScale;
9
+ vec2 roughnessScale;
10
+ vec2 lightEmissionScale;
11
+
12
+ float metalness;
13
+ float roughness;
14
+ float lightEmission;
15
+
16
+ int albedoUVSet;
17
+ int normalUVSet;
18
+ int metalnessUVSet;
19
+ int roughnessUVSet;
20
+ int lightEmissionUVSet;
21
+ int aoUVSet;
22
+
23
+ vec4 fresnelTint;
24
+ vec4 sheenColor;
25
+ float sheenIntensity;
26
+ };
27
+
28
+ struct Light
29
+ {
30
+ vec3 position;
31
+ float intensity;
32
+ vec4 color;
33
+ vec3 direction;
34
+ int type;
35
+ };
36
+
37
+ vec4 sampleAlbedo(sampler2D tex, vec2 uv0, vec2 uv1, PBRMaterialData mat, float gamma)
38
+ {
39
+ vec2 uv = mat.albedoUVSet == 0 ? uv0 : uv1;
40
+ vec4 texColor = texture2D(tex, uv * mat.albedoScale);
41
+ return vec4(SRGB2Lineal(texColor, gamma).rgb * mat.albedo.rgb, texColor.a * mat.albedo.a);
42
+ }
43
+
44
+ float sampleMetallic(sampler2D tex, vec2 uv0, vec2 uv1, PBRMaterialData mat)
45
+ {
46
+ vec2 uv = mat.metalnessUVSet == 0 ? uv0 : uv1;
47
+ return texture2D(tex, uv * mat.metalnessScale).r * mat.metalness;
48
+ }
49
+
50
+ float sampleRoughness(sampler2D tex, vec2 uv0, vec2 uv1, PBRMaterialData mat)
51
+ {
52
+ const float minRoughness = 0.05; // Minimum roughness value: even a mirror have some roughness
53
+ const float maxRoughness = 0.98; // Maximum roughness value: avoid completely diffuse surfaces
54
+ vec2 uv = mat.roughnessUVSet == 0 ? uv0 : uv1;
55
+ return min(max(texture2D(tex, uv * mat.roughnessScale).g * mat.roughness, minRoughness), maxRoughness);
56
+ }
57
+
58
+
59
+ float sampleLightEmission(sampler2D tex, vec2 uv0, vec2 uv1, PBRMaterialData mat)
60
+ {
61
+ vec2 uv = mat.lightEmissionUVSet == 0 ? uv0 : uv1;
62
+ return texture2D(tex, uv * mat.lightEmissionScale).b * mat.lightEmission;
63
+ }
64
+
65
+ vec3 sampleNormal(sampler2D tex, vec2 uv0, vec2 uv1, PBRMaterialData mat, mat3 TBN)
66
+ {
67
+ vec2 uv = mat.normalUVSet == 0 ? uv0 : uv1;
68
+ vec3 normal = texture2D(tex, uv * mat.normalScale).xyz * 2.0 - 1.0;
69
+ return normalize(TBN * normal);
70
+ }
71
+
72
+ float sampleAmbientOcclussion(sampler2D tex, vec2 uv0, vec2 uv1, PBRMaterialData mat)
73
+ {
74
+ vec2 uv = mat.aoUVSet == 0 ? uv0 : uv1;
75
+ return texture2D(tex, uv).a;
76
+ }
77
+
78
+ vec3 calcF0(vec3 albedo, PBRMaterialData mat)
79
+ {
80
+ return mix(vec3(0.04), albedo, mat.metalness) * mat.fresnelTint.rgb;
81
+ }
82
+
83
+ vec3 calcSheen(vec3 normal, vec3 viewDir, vec3 sheenColor, float sheenIntensity)
84
+ {
85
+ float NdotV = max(dot(normal, viewDir), 0.0);
86
+ float facing = 1.0 - NdotV;
87
+
88
+ // Adjustable power: controls how concentrated the brightness is
89
+ float falloff = pow(facing, 5.0);
90
+ return sheenColor * falloff * sheenIntensity;
91
+ }