bg2e-js 2.1.2 → 2.2.0

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 (147) hide show
  1. package/dist/bg2e-js.js +7031 -6989
  2. package/dist/bg2e-js.js.map +1 -1
  3. package/package.json +20 -2
  4. package/src/app/AppController.ts +39 -0
  5. package/src/app/Bg2KeyboardEvent.ts +54 -0
  6. package/src/app/Bg2MouseEvent.ts +82 -0
  7. package/src/app/Bg2TouchEvent.ts +18 -0
  8. package/src/app/Canvas.ts +108 -0
  9. package/src/app/EventBase.ts +10 -0
  10. package/src/app/MainLoop.ts +273 -0
  11. package/src/app/index.ts +25 -0
  12. package/src/base/Color.ts +134 -0
  13. package/src/base/Environment.ts +183 -0
  14. package/src/base/Light.ts +192 -0
  15. package/src/base/Material.ts +616 -0
  16. package/src/base/PolyList.ts +365 -0
  17. package/src/base/Texture.ts +620 -0
  18. package/src/base/index.ts +81 -0
  19. package/src/db/Bg2LoaderPlugin.ts +129 -0
  20. package/src/db/DBPluginApi.ts +48 -0
  21. package/src/db/Loader.ts +116 -0
  22. package/src/db/LoaderPlugin.ts +34 -0
  23. package/src/db/MtlParser.ts +7 -0
  24. package/src/db/ObjLoaderPlugin.ts +55 -0
  25. package/src/db/ObjParser.ts +252 -0
  26. package/src/db/ObjWriterPlugin.ts +19 -0
  27. package/src/db/VitscnjLoaderPlugin.ts +100 -0
  28. package/src/db/Writer.ts +52 -0
  29. package/src/db/WriterPlugin.ts +22 -0
  30. package/src/db/index.ts +44 -0
  31. package/src/debug/DebugRenderer.ts +173 -0
  32. package/src/debug/WebGLTextureViewer.ts +75 -0
  33. package/src/debug/index.ts +7 -0
  34. package/src/index.html +11 -0
  35. package/src/index.ts +33 -0
  36. package/src/manipulation/SelectionBuffer.ts +82 -0
  37. package/src/manipulation/SelectionHighlight.ts +85 -0
  38. package/src/manipulation/SelectionIdAssignVisitor.ts +97 -0
  39. package/src/manipulation/SelectionManager.ts +166 -0
  40. package/src/manipulation/SelectionMode.ts +6 -0
  41. package/src/math/Mat3.ts +259 -0
  42. package/src/math/Mat4.ts +706 -0
  43. package/src/math/MatrixStrategy.ts +25 -0
  44. package/src/math/Quat.ts +65 -0
  45. package/src/math/Vec.ts +753 -0
  46. package/src/math/constants.ts +47 -0
  47. package/src/math/functions.ts +103 -0
  48. package/src/math/index.ts +74 -0
  49. package/src/phsics/joint.ts +137 -0
  50. package/src/primitives/arrow.ts +58 -0
  51. package/src/primitives/cone.ts +138 -0
  52. package/src/primitives/cube.ts +60 -0
  53. package/src/primitives/cylinder.ts +216 -0
  54. package/src/primitives/index.ts +13 -0
  55. package/src/primitives/plane.ts +31 -0
  56. package/src/primitives/sphere.ts +809 -0
  57. package/src/render/BRDFIntegrationMap.ts +4 -0
  58. package/src/render/Environment.ts +136 -0
  59. package/src/render/FrameBuffer.ts +35 -0
  60. package/src/render/MaterialRenderer.ts +34 -0
  61. package/src/render/Pipeline.ts +109 -0
  62. package/src/render/PolyListRenderer.ts +47 -0
  63. package/src/render/RenderBuffer.ts +197 -0
  64. package/src/render/RenderQueue.ts +199 -0
  65. package/src/render/RenderState.ts +116 -0
  66. package/src/render/Renderer.ts +248 -0
  67. package/src/render/SceneAppController.ts +238 -0
  68. package/src/render/SceneRenderer.ts +373 -0
  69. package/src/render/Shader.ts +32 -0
  70. package/src/render/ShadowRenderer.ts +176 -0
  71. package/src/render/SkyCube.ts +106 -0
  72. package/src/render/SkySphere.ts +118 -0
  73. package/src/render/TextureMergerRenderer.ts +70 -0
  74. package/src/render/TextureRenderer.ts +34 -0
  75. package/src/render/index.ts +67 -0
  76. package/src/render/webgl/FrameBuffer.ts +10 -0
  77. package/src/render/webgl/MaterialRenderer.ts +113 -0
  78. package/src/render/webgl/Pipeline.ts +89 -0
  79. package/src/render/webgl/PolyListRenderer.ts +260 -0
  80. package/src/render/webgl/RenderBuffer.ts +227 -0
  81. package/src/render/webgl/Renderer.ts +262 -0
  82. package/src/render/webgl/SceneRenderer.ts +68 -0
  83. package/src/render/webgl/ShaderProgram.ts +424 -0
  84. package/src/render/webgl/ShadowRenderer.ts +6 -0
  85. package/src/render/webgl/SkyCube.ts +16 -0
  86. package/src/render/webgl/SkySphere.ts +16 -0
  87. package/src/render/webgl/State.ts +152 -0
  88. package/src/render/webgl/TextureRenderer.ts +167 -0
  89. package/src/render/webgl/VertexBuffer.ts +137 -0
  90. package/src/render/webgl/index.ts +35 -0
  91. package/src/scene/Camera.ts +458 -0
  92. package/src/scene/Chain.ts +44 -0
  93. package/src/scene/ChainJoint.ts +58 -0
  94. package/src/scene/Component.ts +173 -0
  95. package/src/scene/ComponentMap.ts +107 -0
  96. package/src/scene/Drawable.ts +154 -0
  97. package/src/scene/EnvironmentComponent.ts +142 -0
  98. package/src/scene/FindNodeVisitor.ts +60 -0
  99. package/src/scene/LightComponent.ts +155 -0
  100. package/src/scene/MatrixState.ts +46 -0
  101. package/src/scene/Node.ts +314 -0
  102. package/src/scene/NodeVisitor.ts +15 -0
  103. package/src/scene/OrbitCameraController.ts +450 -0
  104. package/src/scene/SmoothOrbitCameraController.ts +99 -0
  105. package/src/scene/Transform.ts +73 -0
  106. package/src/scene/index.ts +57 -0
  107. package/src/shaders/BasicDiffuseColorShader.ts +111 -0
  108. package/src/shaders/BasicPBRLightShader.ts +277 -0
  109. package/src/shaders/DebugRenderShader.ts +98 -0
  110. package/src/shaders/DepthRenderShader.ts +91 -0
  111. package/src/shaders/IrradianceMapCubeShader.ts +116 -0
  112. package/src/shaders/PBRLightIBLShader.ts +487 -0
  113. package/src/shaders/PickSelectionShader.ts +101 -0
  114. package/src/shaders/PresentDebugFramebufferShader.ts +118 -0
  115. package/src/shaders/PresentTextureShader.ts +99 -0
  116. package/src/shaders/SelectionHighlightShader.ts +127 -0
  117. package/src/shaders/ShaderFunction.ts +318 -0
  118. package/src/shaders/SkyCubeShader.ts +94 -0
  119. package/src/shaders/SkySphereShader.ts +102 -0
  120. package/src/shaders/SpecularMapCubeShader.ts +165 -0
  121. package/src/shaders/TextureMergerShader.ts +171 -0
  122. package/src/shaders/index.ts +37 -0
  123. package/src/shaders/webgl/color_correction.glsl +47 -0
  124. package/src/shaders/webgl/constants.glsl +6 -0
  125. package/src/shaders/webgl/index.ts +70 -0
  126. package/src/shaders/webgl/normal_map.glsl +9 -0
  127. package/src/shaders/webgl/pbr.glsl +173 -0
  128. package/src/shaders/webgl/uniforms.glsl +91 -0
  129. package/src/shaders/webgl_shader_lib.ts +213 -0
  130. package/src/tools/BinaryResourceProvider.ts +14 -0
  131. package/src/tools/ImageResourceProvider.ts +66 -0
  132. package/src/tools/MaterialModifier.ts +276 -0
  133. package/src/tools/Resource.ts +203 -0
  134. package/src/tools/ResourceProvider.ts +69 -0
  135. package/src/tools/TextResourceProvider.ts +24 -0
  136. package/src/tools/TextureCache.ts +52 -0
  137. package/src/tools/TextureResourceDatabase.ts +100 -0
  138. package/src/tools/UserAgent.ts +362 -0
  139. package/src/tools/VideoResourceProvider.ts +50 -0
  140. package/src/tools/WriteStrategy.ts +22 -0
  141. package/src/tools/base64.ts +11 -0
  142. package/src/tools/crypto.ts +19 -0
  143. package/src/tools/endiantess.ts +13 -0
  144. package/src/tools/image.ts +18 -0
  145. package/src/tools/index.ts +41 -0
  146. package/src/tools/processType.ts +38 -0
  147. package/src/vite-env.d.ts +12 -0
@@ -0,0 +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
+ }
@@ -0,0 +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).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
+ }
@@ -0,0 +1,213 @@
1
+ // TODO: Deprecated. Use webgl/index.ts instead.
2
+
3
+ import ShaderFunction from "./ShaderFunction";
4
+
5
+ export const fresnelSchlick = new ShaderFunction('vec3', 'fresnelSchlick', 'float cosTheta, vec3 F0', `{
6
+ return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
7
+ }`);
8
+
9
+ export const fresnelSchlickRoughness = new ShaderFunction('vec3', 'fresnelSchlickRoughness', 'float cosTheta, vec3 F0, float roughness', `{
10
+ return F0 + (max(vec3(1.0 - roughness), F0) - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
11
+ }`);
12
+
13
+ export const distributionGGX = new ShaderFunction('float', 'distributionGGX', 'vec3 N, vec3 H, float roughness', `{
14
+ float a = roughness * roughness;
15
+ float a2 = a * a;
16
+ float NdotH = max(dot(N, H), 0.0);
17
+ float NdotH2 = NdotH * NdotH;
18
+
19
+ float num = a2;
20
+ float denom = NdotH2 * (a2 - 1.0) + 1.0;
21
+ denom = ${Math.PI} * denom * denom;
22
+
23
+ return num / denom;
24
+ }`);
25
+
26
+ export const geometrySchlickGGX = new ShaderFunction('float', 'geometrySchlickGGX', 'float NdotV, float roughness', `{
27
+ float r = (roughness + 1.0);
28
+ float k = (r * r) / 8.0;
29
+
30
+ float num = NdotV;
31
+ float denom = NdotV * (1.0 - k) + k;
32
+
33
+ return num / denom;
34
+ }`)
35
+
36
+ export const geometrySmith = new ShaderFunction('float', 'geometrySmith', 'vec3 N, vec3 V, vec3 L, float roughness', `{
37
+ float NdotV = max(dot(N,V), 0.0);
38
+ float NdotL = max(dot(N,L), 0.0);
39
+ float ggx2 = geometrySchlickGGX(NdotV, roughness);
40
+ float ggx1 = geometrySchlickGGX(NdotL, roughness);
41
+
42
+ return ggx1 * ggx2;
43
+ }`, [geometrySchlickGGX]);
44
+
45
+ export const pbrPointLight = new ShaderFunction('vec3', 'pbrPointLight',
46
+ 'vec3 lightPos, vec3 lightColor, vec3 fragPos, vec3 fragNorm, vec3 viewPos, vec3 albedo, float roughness, float metallic, vec3 fresnel',
47
+ `{
48
+ vec3 F0 = vec3(0.04);
49
+ F0 = mix(F0, albedo, metallic);
50
+
51
+ vec3 L = normalize(lightPos - fragPos);
52
+ vec3 H = normalize(viewPos + L);
53
+
54
+ float distance = length(lightPos - fragPos);
55
+ float attenuation = 1.0 / (distance * distance);
56
+ vec3 radiance = lightColor * attenuation;
57
+
58
+ vec3 F = fresnelSchlick(max(dot(H, viewPos), 0.0), F0) * fresnel;
59
+
60
+ float NDF = distributionGGX(fragNorm, H, roughness);
61
+ float G = geometrySmith(fragNorm, viewPos, L, roughness);
62
+
63
+ vec3 numerator = NDF * G * F;
64
+ float denom = 4.0 * max(dot(fragNorm,viewPos), 0.0) * max(dot(fragNorm,L), 0.0) + 0.0001;
65
+ vec3 specular = numerator / denom;
66
+
67
+ vec3 kS = F;
68
+ vec3 kD = vec3(1.0) - kS;
69
+
70
+ kD *= 1.0 - metallic;
71
+
72
+ float NdotL = max(dot(fragNorm,L), 0.0);
73
+ return (kD * albedo / ${Math.PI} + specular) * radiance * NdotL;
74
+ }`, [fresnelSchlick, distributionGGX, geometrySmith]);
75
+
76
+ export const pbrDirectionalLight = new ShaderFunction('vec3', 'pbrDirectionalLight',
77
+ 'vec3 lightDir, vec3 lightColor, vec3 fragPos, vec3 fragNorm, vec3 viewPos, vec3 albedo, float roughness, float metallic, vec3 fresnel, vec3 shadowColor',
78
+ `{
79
+ vec3 F0 = vec3(0.04);
80
+ F0 = mix(F0, albedo, metallic);
81
+
82
+ vec3 L = normalize(lightDir);
83
+ vec3 H = normalize(viewPos + L);
84
+
85
+ vec3 F = fresnelSchlickRoughness(max(dot(H, viewPos), 0.0), F0, roughness);
86
+
87
+ float NDF = distributionGGX(fragNorm, H, roughness);
88
+ float G = geometrySmith(fragNorm, viewPos, L, roughness);
89
+
90
+ vec3 numerator = NDF * G * F;
91
+ float denom = 4.0 * max(dot(fragNorm,viewPos), 0.4) * max(dot(fragNorm,L), 0.4);
92
+ vec3 specular = numerator / max(denom, 0.0001);
93
+
94
+ vec3 kS = F;
95
+ vec3 kD = vec3(1.0) - kS;
96
+
97
+ kD *= 1.0 - metallic;
98
+
99
+ float NdotL = max(dot(fragNorm,L), 0.0);
100
+ return (kD * albedo / ${Math.PI} + specular * fresnel * shadowColor) * lightColor * shadowColor * NdotL;
101
+ }`, [fresnelSchlick, distributionGGX, geometrySmith, fresnelSchlickRoughness]);
102
+
103
+ export const getPrefilteredColor = new ShaderFunction('vec3', 'getPrefilteredColor', 'float roughness, vec3 refVec, samplerCube irrMap, samplerCube specMap, samplerCube envMap',
104
+ `{
105
+ vec3 specMap0 = textureCube(envMap, refVec).rgb;
106
+ vec3 specMap1 = textureCube(specMap, refVec).rgb;
107
+ vec3 specMap2 = textureCube(irrMap, refVec).rgb;
108
+
109
+ if (roughness<0.7) {
110
+ return mix(specMap0, specMap1, (log(roughness) + 5.0) / 5.0);
111
+ }
112
+ else {
113
+ return mix(specMap1, specMap2, roughness);
114
+ }
115
+ }`);
116
+
117
+ export const pbrAmbientLight = new ShaderFunction('vec3', 'pbrAmbientLight', 'vec3 fragPos, vec3 N, vec3 V, vec3 albedo, float metallic, float roughness, samplerCube irradianceMap, samplerCube specularMap, samplerCube envMap, sampler2D brdfMap, vec3 fresnel, vec3 shadowColor',
118
+ `{
119
+ vec3 F0 = vec3(0.04);
120
+ F0 = mix(F0, albedo, metallic);
121
+ vec3 kS = fresnelSchlickRoughness(max(dot(N,V), 0.0), F0, roughness) * fresnel;
122
+ vec3 kD = 1.0 - kS;
123
+ vec3 irradiance = textureCube(irradianceMap, N).rgb;
124
+ vec3 diffuse = irradiance * albedo;
125
+
126
+ vec3 R = reflect(-V, N);
127
+ vec3 prefilteredColor = getPrefilteredColor(roughness, R, irradianceMap, specularMap, envMap);
128
+ float NdotV = min(max(dot(N,V), 0.0), 0.95);
129
+ vec2 brdfUV = vec2(NdotV, clamp(roughness, 0.01, 0.94));
130
+ vec2 envBRDF = texture2D(brdfMap, brdfUV).xy;
131
+ vec3 indirectSpecular = prefilteredColor * (kS * envBRDF.x + envBRDF.y);
132
+
133
+ return kD * diffuse + indirectSpecular;
134
+ }`, [fresnelSchlickRoughness, getPrefilteredColor]);
135
+
136
+ export const applyConvolution = new ShaderFunction('vec4', 'applyConvolution', 'sampler2D texture, vec2 texCoord, vec2 texSize, float[9] convMatrix, float radius',
137
+ `
138
+ {
139
+ vec2 onePixel = vec2(1.0,1.0) / texSize * radius;
140
+ vec4 colorSum =
141
+ texture2D(texture, texCoord + onePixel * vec2(-1, -1)) * convMatrix[0] +
142
+ texture2D(texture, texCoord + onePixel * vec2( 0, -1)) * convMatrix[1] +
143
+ texture2D(texture, texCoord + onePixel * vec2( 1, -1)) * convMatrix[2] +
144
+ texture2D(texture, texCoord + onePixel * vec2(-1, 0)) * convMatrix[3] +
145
+ texture2D(texture, texCoord + onePixel * vec2( 0, 0)) * convMatrix[4] +
146
+ texture2D(texture, texCoord + onePixel * vec2( 1, 0)) * convMatrix[5] +
147
+ texture2D(texture, texCoord + onePixel * vec2(-1, 1)) * convMatrix[6] +
148
+ texture2D(texture, texCoord + onePixel * vec2( 0, 1)) * convMatrix[7] +
149
+ texture2D(texture, texCoord + onePixel * vec2( 1, 1)) * convMatrix[8];
150
+ float kernelWeight =
151
+ convMatrix[0] +
152
+ convMatrix[1] +
153
+ convMatrix[2] +
154
+ convMatrix[3] +
155
+ convMatrix[4] +
156
+ convMatrix[5] +
157
+ convMatrix[6] +
158
+ convMatrix[7] +
159
+ convMatrix[8];
160
+ if (kernelWeight <= 0.0) {
161
+ kernelWeight = 1.0;
162
+ }
163
+ return vec4((colorSum / kernelWeight).rgb, 1.0);
164
+ }
165
+ `, []);
166
+
167
+ export const getShadowColor = new ShaderFunction('vec3', 'getShadowColor', 'vec4 positionFromLightPov, sampler2D shadowMap, float shadowBias, float shadowStrength',
168
+ `
169
+ {
170
+ // The vertex location rendered from the light source is almost in
171
+ // normalized device coordinates (NDC), but the perspective division
172
+ // has not been performed yet. We need to divide by w to get the
173
+ // vertex location in range [-1, +1]
174
+ vec3 shadowCoord = positionFromLightPov.xyz / positionFromLightPov.w;
175
+
176
+ // Convert from NDC to texture coordinates
177
+ shadowCoord = shadowCoord * 0.5 + 0.5;
178
+
179
+ if (shadowCoord.x >= 0.0 && shadowCoord.x <= 1.0 &&
180
+ shadowCoord.y >= 0.0 && shadowCoord.y <= 1.0)
181
+ {
182
+ float shadowDepth = texture2D(shadowMap, shadowCoord.xy).r;
183
+ if (shadowCoord.z > shadowDepth + shadowBias) {
184
+ return vec3(1.0 - shadowStrength);
185
+ }
186
+ }
187
+ return vec3(1.0);
188
+ }
189
+ `, []);
190
+
191
+ export const lineal2SRGB = new ShaderFunction('vec4', 'lineal2SRGB', 'vec4 color, float gamma', `{
192
+ color.rgb = color.rgb / (color.rgb + vec3(1.0));
193
+ return pow(color, vec4(1.0 / gamma));
194
+ }
195
+ `, []);
196
+
197
+ export const SRGB2Lineal = new ShaderFunction('vec4', 'SRGB2Lineal', 'vec4 color, float gamma', `{
198
+ return pow(color, vec4(gamma));
199
+ }
200
+ `, []);
201
+
202
+ export const brightnessContrast = new ShaderFunction('vec4', 'brightnessContrast', 'vec4 color, float brightness, float contrast', `{
203
+ mat4 brightnessMat = mat4(1, 0, 0, 0,
204
+ 0, 1, 0, 0,
205
+ 0, 0, 1, 0,
206
+ brightness, brightness, brightness, 1);
207
+ float t = (1.0 - contrast) / 2.0;
208
+ mat4 contrastMat = mat4(contrast, 0, 0, 0,
209
+ 0, contrast, 0, 0,
210
+ 0, 0, contrast, 0,
211
+ t, t, t, 1);
212
+ return contrastMat * brightnessMat * color;
213
+ }`, []);
@@ -0,0 +1,14 @@
1
+ import ResourceProvider from "./ResourceProvider";
2
+
3
+ export default class BinaryResourceProvider extends ResourceProvider {
4
+ async load(url: string): Promise<ArrayBuffer> {
5
+ const response = await fetch(url);
6
+ if (response.ok) {
7
+ const binaryData = await response.arrayBuffer();
8
+ return binaryData;
9
+ }
10
+ else {
11
+ throw new Error(`Resource not found at '${ url }'`);
12
+ }
13
+ }
14
+ }
@@ -0,0 +1,66 @@
1
+ import ResourceProvider from "./ResourceProvider";
2
+ import { generateUUID } from "./crypto";
3
+
4
+ // In this array we keep a reference to the images being loaded to
5
+ // prevent them from being deleted from memory
6
+ const g_preventImageDump: HTMLImageElement[] = [];
7
+
8
+ const beginLoadImage = (img: HTMLImageElement): void => {
9
+ if (g_preventImageDump.indexOf(img) === -1) {
10
+ g_preventImageDump.push(img);
11
+ }
12
+ };
13
+
14
+ const endLoadImage = (img: HTMLImageElement): void => {
15
+ const i = g_preventImageDump.indexOf(img);
16
+ if (i !== -1) {
17
+ g_preventImageDump.splice(i, 1);
18
+ }
19
+ }
20
+
21
+ const loadImage = (url: string, preventCache: boolean = false): Promise<HTMLImageElement> => {
22
+ return new Promise((resolve, reject) => {
23
+ const img = new Image();
24
+ beginLoadImage(img);
25
+ img.crossOrigin = "";
26
+ img.addEventListener("load", evt => {
27
+ endLoadImage(evt.target as HTMLImageElement);
28
+ resolve(evt.target as HTMLImageElement);
29
+ });
30
+ img.addEventListener("error", evt => {
31
+ endLoadImage(evt.target as HTMLImageElement);
32
+ reject(new Error(`Error loading image '${ url }'.`));
33
+ });
34
+ img.addEventListener("abort", evt => {
35
+ endLoadImage(evt.target as HTMLImageElement);
36
+ reject(new Error(`Image load aborted '${ url }'.`));
37
+ });
38
+ img.src = url + (preventCache ? `?${generateUUID()}` : "");
39
+ })
40
+ }
41
+
42
+ export default class ImageResourceProvider extends ResourceProvider {
43
+ async load(url: string): Promise<HTMLImageElement> {
44
+ const img = await loadImage(url, false);
45
+ return img;
46
+ }
47
+
48
+ async write(url: string, img: HTMLImageElement | string): Promise<void> {
49
+ let data: Uint8Array | null = null;
50
+ if (img instanceof Image) {
51
+ // TODO: convert to Uint8Array data
52
+ throw new Error("ImageResourceProvider.write(): write from HTMLImageElement not implemented yet.");
53
+ }
54
+ else if (typeof(img) === "string" && /base64/i.test(img)) {
55
+ // Convert base64 image into Uint8Array data
56
+ throw new Error("ImageResourceProvider.write(): write from base64 string not implemented yet.");
57
+ }
58
+ else if (typeof(img) === "string") {
59
+ // Path: copy image
60
+ throw new Error("ImageResourceProvider.write(): copy image from path not implemented yet.");
61
+ }
62
+ else {
63
+ throw new Error("Unsupported image type specified for write");
64
+ }
65
+ }
66
+ }