bg2e-js 2.3.1 → 2.3.2
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.
- package/dist/bg2e-js.js +326 -326
- package/dist/bg2e-js.js.map +1 -1
- package/package.json +56 -56
- package/src/app/AppController.ts +39 -39
- package/src/app/Bg2KeyboardEvent.ts +54 -54
- package/src/app/Bg2MouseEvent.ts +82 -82
- package/src/app/Bg2TouchEvent.ts +18 -18
- package/src/app/Canvas.ts +108 -108
- package/src/app/EventBase.ts +10 -10
- package/src/app/MainLoop.ts +273 -273
- package/src/app/index.ts +24 -24
- package/src/base/Color.ts +134 -134
- package/src/base/Environment.ts +183 -183
- package/src/base/Light.ts +192 -192
- package/src/base/Material.ts +620 -620
- package/src/base/PolyList.ts +365 -365
- package/src/base/Texture.ts +620 -620
- package/src/base/index.ts +81 -81
- package/src/db/Bg2LoaderPlugin.ts +143 -143
- package/src/db/DBPluginApi.ts +48 -48
- package/src/db/Loader.ts +116 -116
- package/src/db/LoaderPlugin.ts +34 -34
- package/src/db/MtlParser.ts +7 -7
- package/src/db/ObjLoaderPlugin.ts +54 -54
- package/src/db/ObjParser.ts +252 -252
- package/src/db/ObjWriterPlugin.ts +18 -18
- package/src/db/VitscnjLoaderPlugin.ts +112 -112
- package/src/db/Writer.ts +52 -52
- package/src/db/WriterPlugin.ts +22 -22
- package/src/db/index.ts +44 -44
- package/src/debug/DebugRenderer.ts +173 -173
- package/src/debug/WebGLTextureViewer.ts +75 -75
- package/src/debug/index.ts +6 -6
- package/src/index.html +11 -11
- package/src/index.ts +33 -33
- package/src/manipulation/SelectionBuffer.ts +82 -82
- package/src/manipulation/SelectionHighlight.ts +85 -85
- package/src/manipulation/SelectionIdAssignVisitor.ts +96 -96
- package/src/manipulation/SelectionManager.ts +166 -166
- package/src/manipulation/SelectionMode.ts +6 -6
- package/src/math/Mat3.ts +259 -259
- package/src/math/Mat4.ts +710 -710
- package/src/math/MatrixStrategy.ts +25 -25
- package/src/math/Quat.ts +65 -65
- package/src/math/Vec.ts +753 -753
- package/src/math/constants.ts +46 -46
- package/src/math/functions.ts +103 -103
- package/src/math/index.ts +74 -74
- package/src/phsics/joint.ts +137 -137
- package/src/primitives/arrow.ts +57 -57
- package/src/primitives/cone.ts +138 -138
- package/src/primitives/cube.ts +60 -60
- package/src/primitives/cylinder.ts +216 -216
- package/src/primitives/index.ts +13 -13
- package/src/primitives/plane.ts +31 -31
- package/src/primitives/sphere.ts +809 -809
- package/src/react/useBg2e.ts +75 -70
- package/src/render/BRDFIntegrationMap.ts +4 -4
- package/src/render/Environment.ts +135 -135
- package/src/render/FrameBuffer.ts +35 -35
- package/src/render/MaterialRenderer.ts +34 -34
- package/src/render/Pipeline.ts +108 -108
- package/src/render/PolyListRenderer.ts +47 -47
- package/src/render/RenderBuffer.ts +197 -197
- package/src/render/RenderQueue.ts +198 -198
- package/src/render/RenderState.ts +116 -116
- package/src/render/Renderer.ts +248 -248
- package/src/render/SceneAppController.ts +247 -247
- package/src/render/SceneRenderer.ts +372 -372
- package/src/render/Shader.ts +32 -32
- package/src/render/ShadowRenderer.ts +176 -176
- package/src/render/SkyCube.ts +105 -105
- package/src/render/SkySphere.ts +117 -117
- package/src/render/TextureMergerRenderer.ts +70 -70
- package/src/render/TextureRenderer.ts +34 -34
- package/src/render/index.ts +67 -67
- package/src/render/webgl/FrameBuffer.ts +9 -9
- package/src/render/webgl/MaterialRenderer.ts +112 -112
- package/src/render/webgl/Pipeline.ts +88 -88
- package/src/render/webgl/PolyListRenderer.ts +260 -260
- package/src/render/webgl/RenderBuffer.ts +226 -226
- package/src/render/webgl/Renderer.ts +262 -262
- package/src/render/webgl/SceneRenderer.ts +67 -67
- package/src/render/webgl/ShaderProgram.ts +424 -424
- package/src/render/webgl/ShadowRenderer.ts +6 -6
- package/src/render/webgl/SkyCube.ts +15 -15
- package/src/render/webgl/SkySphere.ts +15 -15
- package/src/render/webgl/State.ts +152 -152
- package/src/render/webgl/TextureRenderer.ts +167 -167
- package/src/render/webgl/VertexBuffer.ts +137 -137
- package/src/render/webgl/index.ts +35 -35
- package/src/scene/Camera.ts +458 -458
- package/src/scene/Chain.ts +44 -44
- package/src/scene/ChainJoint.ts +58 -58
- package/src/scene/Component.ts +173 -173
- package/src/scene/ComponentMap.ts +106 -106
- package/src/scene/Drawable.ts +154 -154
- package/src/scene/EnvironmentComponent.ts +141 -141
- package/src/scene/FindNodeVisitor.ts +59 -59
- package/src/scene/LightComponent.ts +154 -154
- package/src/scene/MatrixState.ts +46 -46
- package/src/scene/Node.ts +314 -314
- package/src/scene/NodeVisitor.ts +15 -15
- package/src/scene/OrbitCameraController.ts +450 -450
- package/src/scene/SmoothOrbitCameraController.ts +99 -99
- package/src/scene/Transform.ts +73 -73
- package/src/scene/index.ts +60 -60
- package/src/shaders/BasicDiffuseColorShader.ts +111 -111
- package/src/shaders/BasicPBRLightShader.ts +276 -276
- package/src/shaders/DebugRenderShader.ts +97 -97
- package/src/shaders/DepthRenderShader.ts +127 -127
- package/src/shaders/IrradianceMapCubeShader.ts +115 -115
- package/src/shaders/PBRLightIBLShader.ts +486 -486
- package/src/shaders/PickSelectionShader.ts +101 -101
- package/src/shaders/PresentDebugFramebufferShader.ts +118 -118
- package/src/shaders/PresentTextureShader.ts +99 -99
- package/src/shaders/SelectionHighlightShader.ts +127 -127
- package/src/shaders/ShaderFunction.ts +318 -318
- package/src/shaders/SkyCubeShader.ts +93 -93
- package/src/shaders/SkySphereShader.ts +102 -102
- package/src/shaders/SpecularMapCubeShader.ts +164 -164
- package/src/shaders/TextureMergerShader.ts +171 -171
- package/src/shaders/index.ts +36 -36
- package/src/shaders/webgl/color_correction.glsl +47 -47
- package/src/shaders/webgl/constants.glsl +6 -6
- package/src/shaders/webgl/index.ts +70 -70
- package/src/shaders/webgl/normal_map.glsl +9 -9
- package/src/shaders/webgl/pbr.glsl +173 -173
- package/src/shaders/webgl/uniforms.glsl +91 -91
- package/src/shaders/webgl_shader_lib.ts +213 -213
- package/src/tools/BinaryResourceProvider.ts +14 -14
- package/src/tools/ImageResourceProvider.ts +66 -66
- package/src/tools/MaterialModifier.ts +446 -446
- package/src/tools/Resource.ts +203 -203
- package/src/tools/ResourceProvider.ts +69 -69
- package/src/tools/TextResourceProvider.ts +24 -24
- package/src/tools/TextureCache.ts +51 -51
- package/src/tools/TextureResourceDatabase.ts +100 -100
- package/src/tools/UserAgent.ts +362 -362
- package/src/tools/VideoResourceProvider.ts +50 -50
- package/src/tools/WriteStrategy.ts +22 -22
- package/src/tools/base64.ts +11 -11
- package/src/tools/crypto.ts +19 -19
- package/src/tools/endiantess.ts +13 -13
- package/src/tools/image.ts +18 -18
- package/src/tools/index.ts +41 -41
- package/src/tools/processType.ts +39 -39
- package/src/vite-env.d.ts +12 -12
|
@@ -1,213 +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
|
-
}`, []);
|
|
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
|
+
}`, []);
|
|
@@ -1,14 +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
|
-
}
|
|
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
|
+
}
|
|
@@ -1,66 +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
|
-
}
|
|
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
|
+
}
|