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.
- package/dist/bg2e-js.js +356 -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 +81 -81
- package/src/manipulation/SelectionHighlight.ts +105 -84
- package/src/manipulation/SelectionIdAssignVisitor.ts +96 -96
- package/src/manipulation/SelectionManager.ts +196 -188
- 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 +69 -69
- 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 +250 -250
- package/src/render/SceneRenderer.ts +387 -387
- 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 +177 -177
- 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 +328 -328
- 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 +143 -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,487 +1,487 @@
|
|
|
1
|
-
import Shader from "../render/Shader";
|
|
2
|
-
import ShaderFunction from "./ShaderFunction";
|
|
3
|
-
import ShaderProgram from "../render/webgl/ShaderProgram";
|
|
4
|
-
import Mat4 from "../math/Mat4";
|
|
5
|
-
import Vec from "../math/Vec";
|
|
6
|
-
import Light, {
|
|
7
|
-
LightType
|
|
8
|
-
} from "../base/Light";
|
|
9
|
-
import {
|
|
10
|
-
createNormalTexture,
|
|
11
|
-
normalTexture,
|
|
12
|
-
createBRDFIntegrationTexture
|
|
13
|
-
} from "../tools/TextureResourceDatabase";
|
|
14
|
-
import Environment from "../render/Environment";
|
|
15
|
-
|
|
16
|
-
import {
|
|
17
|
-
getColorCorrectionFunctions,
|
|
18
|
-
getNormalMapFunctions,
|
|
19
|
-
getPBRFunctions,
|
|
20
|
-
getUniformsFunctions,
|
|
21
|
-
replaceConstants
|
|
22
|
-
} from "./webgl";
|
|
23
|
-
import Renderer from "../render/Renderer";
|
|
24
|
-
import WebGLRenderer from "../render/webgl/Renderer";
|
|
25
|
-
import PolyListRenderer from "../render/webgl/PolyListRenderer";
|
|
26
|
-
import MaterialRenderer from "../render/webgl/MaterialRenderer";
|
|
27
|
-
import WebGLTextureRenderer from "../render/webgl/TextureRenderer";
|
|
28
|
-
import Texture from "../base/Texture";
|
|
29
|
-
|
|
30
|
-
function getShaderProgramForLights(this: PBRLightIBLShader, renderer: Renderer, numLights: number): ShaderProgram {
|
|
31
|
-
const r = renderer as WebGLRenderer;
|
|
32
|
-
|
|
33
|
-
if (this._programs[numLights]) {
|
|
34
|
-
return this._programs[numLights];
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const vshader = ShaderFunction.GetShaderCode(`precision mediump float;
|
|
38
|
-
attribute vec3 inPosition;
|
|
39
|
-
attribute vec3 inNormal;
|
|
40
|
-
attribute vec2 inTexCoord;
|
|
41
|
-
attribute vec2 inTexCoord2;
|
|
42
|
-
attribute vec3 inTangent;
|
|
43
|
-
|
|
44
|
-
uniform mat4 uWorld;
|
|
45
|
-
uniform mat4 uView;
|
|
46
|
-
uniform mat4 uViewInverse;
|
|
47
|
-
uniform mat4 uProj;
|
|
48
|
-
uniform mat3 uNormMatrix;
|
|
49
|
-
|
|
50
|
-
varying vec3 fragPos;
|
|
51
|
-
varying vec3 fragViewPos;
|
|
52
|
-
varying vec3 fragNorm;
|
|
53
|
-
varying vec2 fragTexCoord;
|
|
54
|
-
varying vec2 fragTexCoord2;
|
|
55
|
-
varying vec3 fragLightPositions[${numLights}];
|
|
56
|
-
|
|
57
|
-
varying mat3 fragTBN;
|
|
58
|
-
|
|
59
|
-
uniform vec3 uLightPositions[${numLights}];
|
|
60
|
-
uniform mat4 uLightTransforms[${numLights}];
|
|
61
|
-
|
|
62
|
-
// Used to calculate shadow from depth texture
|
|
63
|
-
uniform mat4 uLightPovMvp;
|
|
64
|
-
varying vec4 fragPositionFromLightPov;
|
|
65
|
-
|
|
66
|
-
`,
|
|
67
|
-
[
|
|
68
|
-
new ShaderFunction('void','main','',`{
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
for (int i = 0; i < ${numLights}; ++i)
|
|
72
|
-
{
|
|
73
|
-
fragLightPositions[i] = (uLightTransforms[i] * vec4(uLightPositions[i], 1.0)).xyz;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
fragPos = (uWorld * vec4(inPosition, 1.0)).xyz;
|
|
77
|
-
|
|
78
|
-
fragPositionFromLightPov = uLightPovMvp * vec4(fragPos, 1.0);
|
|
79
|
-
|
|
80
|
-
fragViewPos = (uViewInverse * vec4(0.0,0.0,0.0,1.0)).xyz;
|
|
81
|
-
fragTexCoord = inTexCoord;
|
|
82
|
-
fragTexCoord2 = inTexCoord2;
|
|
83
|
-
fragNorm = normalize(uNormMatrix * inNormal);
|
|
84
|
-
gl_Position = uProj * uView * uWorld * vec4(inPosition,1.0);
|
|
85
|
-
fragTBN = TBNMatrix(uWorld, inNormal, inTangent);
|
|
86
|
-
}`, [
|
|
87
|
-
...getNormalMapFunctions()
|
|
88
|
-
])
|
|
89
|
-
]);
|
|
90
|
-
|
|
91
|
-
const deps = [
|
|
92
|
-
...getColorCorrectionFunctions(),
|
|
93
|
-
...getNormalMapFunctions(),
|
|
94
|
-
...getUniformsFunctions(),
|
|
95
|
-
...getPBRFunctions()
|
|
96
|
-
];
|
|
97
|
-
|
|
98
|
-
const fshader = replaceConstants(ShaderFunction.GetShaderCode(`precision mediump float;
|
|
99
|
-
varying vec3 fragPos;
|
|
100
|
-
varying vec3 fragNorm;
|
|
101
|
-
varying vec2 fragTexCoord;
|
|
102
|
-
varying vec2 fragTexCoord2;
|
|
103
|
-
varying vec3 fragViewPos;
|
|
104
|
-
varying vec3 fragLightPositions[${numLights}];
|
|
105
|
-
|
|
106
|
-
uniform vec3 uCameraPos;
|
|
107
|
-
|
|
108
|
-
uniform sampler2D uAlbedoTexture;
|
|
109
|
-
uniform sampler2D uNormalTexture;
|
|
110
|
-
uniform sampler2D uMetallicRoughnessHeightAOTexture;
|
|
111
|
-
|
|
112
|
-
uniform vec4 uAlbedo;
|
|
113
|
-
uniform float uMetallic;
|
|
114
|
-
uniform float uRoughness;
|
|
115
|
-
uniform float uLightEmission;
|
|
116
|
-
|
|
117
|
-
uniform vec4 uFresnelTint;
|
|
118
|
-
uniform vec4 uSheenColor;
|
|
119
|
-
uniform float uSheenIntensity;
|
|
120
|
-
|
|
121
|
-
uniform vec2 uAlbedoScale;
|
|
122
|
-
uniform vec2 uNormalScale;
|
|
123
|
-
uniform vec2 uMetallicScale;
|
|
124
|
-
uniform vec2 uRoughnessScale;
|
|
125
|
-
uniform vec2 uLightEmissionScale;
|
|
126
|
-
|
|
127
|
-
uniform float uAlphaTresshold;
|
|
128
|
-
|
|
129
|
-
uniform int uLightTypes[${numLights}];
|
|
130
|
-
uniform vec3 uLightDirections[${numLights}];
|
|
131
|
-
uniform vec3 uLightColors[${numLights}];
|
|
132
|
-
uniform float uLightIntensities[${numLights}];
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
uniform samplerCube uEnvMap;
|
|
136
|
-
uniform samplerCube uSpecularMap;
|
|
137
|
-
uniform samplerCube uIrradianceMap;
|
|
138
|
-
uniform sampler2D uBRDFIntegrationMap;
|
|
139
|
-
uniform float uAmbientIntensity;
|
|
140
|
-
|
|
141
|
-
uniform sampler2D uShadowMap;
|
|
142
|
-
uniform float uShadowBias;
|
|
143
|
-
varying vec4 fragPositionFromLightPov;
|
|
144
|
-
uniform float uShadowStrength;
|
|
145
|
-
|
|
146
|
-
uniform int uAlbedoMap;
|
|
147
|
-
uniform int uNormalMap;
|
|
148
|
-
uniform int uAOMap;
|
|
149
|
-
uniform int uMetallicMap;
|
|
150
|
-
uniform int uRoughnessMap;
|
|
151
|
-
uniform int uLightemissionMap;
|
|
152
|
-
|
|
153
|
-
varying mat3 fragTBN;
|
|
154
|
-
|
|
155
|
-
uniform float uBrightness;
|
|
156
|
-
uniform float uContrast;
|
|
157
|
-
`,
|
|
158
|
-
[
|
|
159
|
-
new ShaderFunction('void','main','',`{
|
|
160
|
-
float gamma = 2.2;
|
|
161
|
-
|
|
162
|
-
PBRMaterialData mat;
|
|
163
|
-
mat.albedo = uAlbedo;
|
|
164
|
-
mat.albedoScale = uAlbedoScale;
|
|
165
|
-
mat.normalScale = uNormalScale;
|
|
166
|
-
mat.metalnessScale = uMetallicScale;
|
|
167
|
-
mat.roughnessScale = uRoughnessScale;
|
|
168
|
-
mat.lightEmissionScale = uLightEmissionScale;
|
|
169
|
-
mat.metalness = uMetallic;
|
|
170
|
-
mat.roughness = uRoughness;
|
|
171
|
-
mat.lightEmission = uLightEmission;
|
|
172
|
-
mat.albedoUVSet = uAlbedoMap;
|
|
173
|
-
mat.normalUVSet = uNormalMap;
|
|
174
|
-
mat.metalnessUVSet = uMetallicMap;
|
|
175
|
-
mat.roughnessUVSet = uRoughnessMap;
|
|
176
|
-
mat.lightEmissionUVSet = uLightemissionMap;
|
|
177
|
-
mat.aoUVSet = uAOMap;
|
|
178
|
-
mat.fresnelTint = uFresnelTint;
|
|
179
|
-
mat.sheenColor = uSheenColor;
|
|
180
|
-
mat.sheenIntensity = uSheenIntensity;
|
|
181
|
-
|
|
182
|
-
vec4 albedo = sampleAlbedo(uAlbedoTexture, fragTexCoord, fragTexCoord2, mat, gamma);
|
|
183
|
-
float metallic = sampleMetallic(uMetallicRoughnessHeightAOTexture, fragTexCoord, fragTexCoord2, mat);
|
|
184
|
-
float roughness = sampleRoughness(uMetallicRoughnessHeightAOTexture, fragTexCoord, fragTexCoord2, mat);
|
|
185
|
-
|
|
186
|
-
// TODO: Light emission
|
|
187
|
-
float ambientOcclussion = sampleAmbientOcclussion(uMetallicRoughnessHeightAOTexture, fragTexCoord, fragTexCoord2, mat);
|
|
188
|
-
|
|
189
|
-
vec3 normal = sampleNormal(uNormalTexture, fragTexCoord, fragTexCoord2, mat, fragTBN);
|
|
190
|
-
if (!gl_FrontFacing) {
|
|
191
|
-
normal = -normal;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
vec3 viewDir = normalize(fragViewPos - fragPos);
|
|
195
|
-
vec3 F0 = calcF0(albedo.rgb, mat);
|
|
196
|
-
|
|
197
|
-
if (albedo.a < uAlphaTresshold) {
|
|
198
|
-
discard;
|
|
199
|
-
}
|
|
200
|
-
else {
|
|
201
|
-
vec3 Lo = vec3(0.0);
|
|
202
|
-
for (int i = 0; i < ${numLights}; ++i) {
|
|
203
|
-
Light light;
|
|
204
|
-
light.type = uLightTypes[i];
|
|
205
|
-
light.color = vec4(uLightColors[i], 1.0);
|
|
206
|
-
light.intensity = uLightIntensities[i];
|
|
207
|
-
light.direction = uLightDirections[i];
|
|
208
|
-
light.position = fragLightPositions[i];
|
|
209
|
-
|
|
210
|
-
Lo += calcRadiance(light, viewDir, fragPos, metallic, roughness, F0, normal, albedo.rgb, mat.sheenIntensity, mat.sheenColor.rgb, ambientOcclussion);
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
vec3 shadowColor = getShadowColor(fragPositionFromLightPov, uShadowMap, uShadowBias, uShadowStrength);
|
|
215
|
-
|
|
216
|
-
Lo = Lo * shadowColor;
|
|
217
|
-
|
|
218
|
-
float ambientIntensity = 2.0;
|
|
219
|
-
vec3 ambient = calcAmbientLight(
|
|
220
|
-
viewDir, normal, F0,
|
|
221
|
-
albedo.rgb, metallic, roughness,
|
|
222
|
-
uIrradianceMap, uSpecularMap, uEnvMap,
|
|
223
|
-
uBRDFIntegrationMap, ambientOcclussion,
|
|
224
|
-
mat.sheenIntensity, mat.sheenColor.rgb,
|
|
225
|
-
shadowColor,
|
|
226
|
-
ambientIntensity
|
|
227
|
-
);
|
|
228
|
-
|
|
229
|
-
vec3 color = ambient + Lo;
|
|
230
|
-
|
|
231
|
-
color = color / (color + vec3(1.0));
|
|
232
|
-
gl_FragColor = lineal2SRGB(vec4(color, 1.0), gamma);
|
|
233
|
-
gl_FragColor = brightnessContrast(vec4(color, albedo.a), uBrightness, uContrast);
|
|
234
|
-
}
|
|
235
|
-
}`, deps)
|
|
236
|
-
]));
|
|
237
|
-
|
|
238
|
-
this._programs[numLights] = ShaderProgram.Create(r.gl, "PBRLightIBL", vshader, fshader);
|
|
239
|
-
return this._programs[numLights];
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
export default class PBRLightIBLShader extends Shader {
|
|
243
|
-
protected _programs: { [key: number]: ShaderProgram };
|
|
244
|
-
protected _lights: Light[];
|
|
245
|
-
protected _lightTransforms: Mat4[];
|
|
246
|
-
protected _lightTypes: LightType[] = [];
|
|
247
|
-
protected _lightPositions: Vec[] = [];
|
|
248
|
-
protected _lightDirections: Vec[] = [];
|
|
249
|
-
protected _lightColors: Vec[] = [];
|
|
250
|
-
protected _lightIntensities: number[] = [];
|
|
251
|
-
protected _brightness: number;
|
|
252
|
-
protected _contrast: number;
|
|
253
|
-
|
|
254
|
-
protected _cameraPosition: Vec;
|
|
255
|
-
protected _brdfIntegrationTexture: Texture | null = null;
|
|
256
|
-
protected _environment: Environment | null = null;
|
|
257
|
-
protected _program: ShaderProgram | null = null;
|
|
258
|
-
|
|
259
|
-
constructor(renderer: Renderer) {
|
|
260
|
-
super(renderer);
|
|
261
|
-
|
|
262
|
-
this._lights = [];
|
|
263
|
-
this._lightTransforms = [];
|
|
264
|
-
|
|
265
|
-
this._brightness = 0.2;
|
|
266
|
-
this._contrast = 1.1;
|
|
267
|
-
|
|
268
|
-
this._cameraPosition = new Vec(0,0,0);
|
|
269
|
-
|
|
270
|
-
if (renderer.typeId !== "WebGL") {
|
|
271
|
-
throw Error("PresentTextureShader is only compatible with WebGL renderer");
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
this._programs = {};
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
async load() {
|
|
278
|
-
await createNormalTexture(this.renderer);
|
|
279
|
-
|
|
280
|
-
this._brdfIntegrationTexture = await createBRDFIntegrationTexture(this.renderer);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
get brightness() {
|
|
284
|
-
return this._brightness;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
get contrast() {
|
|
288
|
-
return this._contrast;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
set brightness(b) {
|
|
292
|
-
this._brightness = b;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
set contrast(c) {
|
|
296
|
-
this._contrast = c;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
set cameraPosition(p: Vec) {
|
|
300
|
-
this._cameraPosition = p;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
set environment(env: Environment | null) {
|
|
304
|
-
this._environment = env;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
get environmentMap() {
|
|
308
|
-
return this._environment ? this._environment.environmentMap : null;
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
get specularMap() {
|
|
312
|
-
return this._environment ? this._environment.specularMap : null;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
get irradianceMap() {
|
|
316
|
-
return this._environment ? this._environment.irradianceMap : null;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
set lights(l) {
|
|
320
|
-
if (!l.length) {
|
|
321
|
-
throw new Error("BasicPBRLightShader: Invalid light array set.");
|
|
322
|
-
}
|
|
323
|
-
this._lights = l;
|
|
324
|
-
this._lightTypes = [];
|
|
325
|
-
this._lightPositions = [];
|
|
326
|
-
this._lightDirections = [];
|
|
327
|
-
this._lightColors = [];
|
|
328
|
-
this._lightIntensities = [];
|
|
329
|
-
this._lights.forEach((light,i) => {
|
|
330
|
-
this._lightTypes.push(light.type);
|
|
331
|
-
const trx = this._lightTransforms[i];
|
|
332
|
-
const rot = trx && Mat4.GetRotation(trx);
|
|
333
|
-
const position = new Vec(light.position);
|
|
334
|
-
const direction = new Vec(light.direction);
|
|
335
|
-
this._lightPositions.push(trx ? trx.multVector(position).xyz : position);
|
|
336
|
-
this._lightDirections.push(rot ? rot.multVector(direction).xyz.normalize() : direction);
|
|
337
|
-
this._lightColors.push(new Vec(light.color));
|
|
338
|
-
this._lightIntensities.push(light.intensity);
|
|
339
|
-
});
|
|
340
|
-
this._program = getShaderProgramForLights.apply(this, [this.renderer, this._lights.length]);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
get lights() {
|
|
344
|
-
return this._lights;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
set lightTransforms(lt) {
|
|
348
|
-
this._lightTransforms = lt;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
get lightTransforms() {
|
|
352
|
-
return this._lightTransforms;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
updateLight(light: Light, index: number) {
|
|
356
|
-
if (index >= this._lights.length) {
|
|
357
|
-
throw new Error("BasicPBRLightShader: Invalid light index updating light data");
|
|
358
|
-
}
|
|
359
|
-
this._lightTypes[index] = light.type;
|
|
360
|
-
this._lightPositions[index] = new Vec(light.position);
|
|
361
|
-
this._lightDirections[index] = new Vec(light.direction);
|
|
362
|
-
this._lightColors[index] = new Vec(light.color);
|
|
363
|
-
this._lightIntensities[index] = light.intensity;
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
updateLights(lights: Light[]) {
|
|
367
|
-
if (this._lights.length !== lights.length) {
|
|
368
|
-
this.lights = lights;
|
|
369
|
-
}
|
|
370
|
-
else {
|
|
371
|
-
lights.forEach((l,i) => this.updateLight(l,i));
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
setup(
|
|
376
|
-
plistRenderer: PolyListRenderer,
|
|
377
|
-
materialRenderer: MaterialRenderer,
|
|
378
|
-
modelMatrix: Mat4,
|
|
379
|
-
viewMatrix: Mat4,
|
|
380
|
-
projectionMatrix: Mat4
|
|
381
|
-
) {
|
|
382
|
-
const rend = this.renderer as WebGLRenderer;
|
|
383
|
-
if (!this._program) {
|
|
384
|
-
throw new Error("BasicPBRLightShader: lights array not specified");
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
const material = materialRenderer.material;
|
|
388
|
-
materialRenderer.mergeTextures();
|
|
389
|
-
rend.state.shaderProgram = this._program;
|
|
390
|
-
|
|
391
|
-
const normMatrix = Mat4.GetNormalMatrix(modelMatrix);
|
|
392
|
-
this._program.bindMatrix('uNormMatrix', normMatrix);
|
|
393
|
-
this._program.bindMatrix('uWorld', modelMatrix);
|
|
394
|
-
this._program.bindMatrix('uView', viewMatrix);
|
|
395
|
-
this._program.bindMatrix('uViewInverse', Mat4.GetInverted(viewMatrix));
|
|
396
|
-
this._program.bindMatrix('uProj', projectionMatrix);
|
|
397
|
-
|
|
398
|
-
if (!this._cameraPosition) {
|
|
399
|
-
console.warn("Serious performance warning: camera position not set in BasicPBRLightShader. Extracting the camera position from the view matrix involves inverting the matrix.");
|
|
400
|
-
this._program.bindVector('uCameraPos',Mat4.GetPosition(Mat4.GetInverted(viewMatrix)));
|
|
401
|
-
}
|
|
402
|
-
else {
|
|
403
|
-
this._program.bindVector('uCameraPos',this._cameraPosition);
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
materialRenderer.bindTexture(this._program, 'albedoTexture', 'uAlbedoTexture', 0);
|
|
407
|
-
materialRenderer.bindTexture(this._program, 'normalTexture', 'uNormalTexture', 1, normalTexture(this.renderer));
|
|
408
|
-
materialRenderer.bindMetalnessRoughnessHeightAOTexture(this._program, 'uMetallicRoughnessHeightAOTexture', 2);
|
|
409
|
-
this._program.uniform1f('uAlphaTresshold', material.alphaCutoff);
|
|
410
|
-
|
|
411
|
-
materialRenderer.bindColor(this._program, 'albedo', 'uAlbedo');
|
|
412
|
-
materialRenderer.bindValue(this._program, 'metalness', 'uMetallic');
|
|
413
|
-
materialRenderer.bindValue(this._program, 'roughness', 'uRoughness');
|
|
414
|
-
materialRenderer.bindValue(this._program, 'lightEmission', 'uLightEmission', 0);
|
|
415
|
-
|
|
416
|
-
this._program.uniform1i('uAlbedoMap', material.albedoUV);
|
|
417
|
-
this._program.uniform1i('uNormalMap', material.normalUV);
|
|
418
|
-
this._program.uniform1i('uAOMap', material.ambientOcclussionUV);
|
|
419
|
-
this._program.uniform1i('uMetallicMap', material.metalnessChannel);
|
|
420
|
-
this._program.uniform1i('uRoughnessMap', material.roughnessChannel);
|
|
421
|
-
this._program.uniform1i('uLightemissionMap', material.lightEmissionChannel);
|
|
422
|
-
|
|
423
|
-
this._program.bindVector("uAlbedoScale", material.albedoScale);
|
|
424
|
-
this._program.bindVector("uNormalScale", material.normalScale);
|
|
425
|
-
this._program.bindVector("uMetallicScale", material.metalnessScale);
|
|
426
|
-
this._program.bindVector("uRoughnessScale", material.roughnessScale);
|
|
427
|
-
this._program.bindVector("uLightEmissionScale", material.lightEmissionScale);
|
|
428
|
-
this._program.bindVector("uFresnelTint", material.fresnelTint);
|
|
429
|
-
this._program.bindVector("uSheenColor", material.sheenColor);
|
|
430
|
-
this._program.uniform1f("uSheenIntensity", material.sheenIntensity);
|
|
431
|
-
|
|
432
|
-
if (!this.irradianceMap || !this.specularMap || !this.environmentMap || !this._brdfIntegrationTexture) {
|
|
433
|
-
throw new Error("PBRLightIBLShader: Environment maps not set.");
|
|
434
|
-
}
|
|
435
|
-
const irMap = this.renderer.factory.texture(this.irradianceMap) as WebGLTextureRenderer;
|
|
436
|
-
const specMap = this.renderer.factory.texture(this.specularMap) as WebGLTextureRenderer;
|
|
437
|
-
const envMap = this.renderer.factory.texture(this.environmentMap) as WebGLTextureRenderer;
|
|
438
|
-
const brdfLut = this.renderer.factory.texture(this._brdfIntegrationTexture) as WebGLTextureRenderer;
|
|
439
|
-
|
|
440
|
-
this._program.bindTexture("uIrradianceMap", irMap, 3);
|
|
441
|
-
this._program.bindTexture("uSpecularMap", specMap, 4);
|
|
442
|
-
this._program.bindTexture("uEnvMap", envMap, 5);
|
|
443
|
-
this._program.bindTexture("uBRDFIntegrationMap", brdfLut, 6);
|
|
444
|
-
|
|
445
|
-
this._program.uniform1f("uBrightness", this._brightness);
|
|
446
|
-
this._program.uniform1f("uContrast", this._contrast);
|
|
447
|
-
|
|
448
|
-
// TODO: Get the ambient intensity from environment
|
|
449
|
-
this._program.uniform1f("uAmbientIntensity", 1.0);
|
|
450
|
-
|
|
451
|
-
let shadowLight: Light | null = this._lights.find(light => light.depthTexture) || null;
|
|
452
|
-
this._lights.forEach((light,i) => {
|
|
453
|
-
if (light.depthTexture) {
|
|
454
|
-
shadowLight = light;
|
|
455
|
-
}
|
|
456
|
-
this._program?.uniform1i(`uLightTypes[${i}]`, this._lightTypes[i]);
|
|
457
|
-
this._program?.bindVector(`uLightPositions[${i}]`, this._lightPositions[i]);
|
|
458
|
-
this._program?.bindVector(`uLightDirections[${i}]`, this._lightDirections[i]);
|
|
459
|
-
this._program?.bindVector(`uLightColors[${i}]`, this._lightColors[i].rgb);
|
|
460
|
-
this._program?.uniform1f(`uLightIntensities[${i}]`, this._lightIntensities[i]);
|
|
461
|
-
this._program?.bindMatrix(`uLightTransforms[${i}]`, this._lightTransforms[i]);
|
|
462
|
-
});
|
|
463
|
-
|
|
464
|
-
if (shadowLight?.depthTexture) {
|
|
465
|
-
const depthTex = this.renderer.factory.texture(shadowLight.depthTexture) as WebGLTextureRenderer;
|
|
466
|
-
this._program.bindTexture("uShadowMap", depthTex, 7);
|
|
467
|
-
const projectionViewMatrix = Mat4.Mult(shadowLight.projection, shadowLight.viewMatrix || Mat4.MakeIdentity());
|
|
468
|
-
this._program.bindMatrix("uLightPovMvp", projectionViewMatrix);
|
|
469
|
-
this._program.uniform1f("uShadowBias", shadowLight.shadowBias * 30);
|
|
470
|
-
this._program.uniform1f("uShadowStrength", shadowLight.shadowStrength);
|
|
471
|
-
}
|
|
472
|
-
|
|
473
|
-
this._program.bindAttribs(plistRenderer, {
|
|
474
|
-
position: "inPosition",
|
|
475
|
-
normal: "inNormal",
|
|
476
|
-
tex0: "inTexCoord",
|
|
477
|
-
tex1: "inTexCoord2",
|
|
478
|
-
tangent: "inTangent"
|
|
479
|
-
});
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
destroy() {
|
|
483
|
-
if (this._program) {
|
|
484
|
-
ShaderProgram.Delete(this._program);
|
|
485
|
-
}
|
|
486
|
-
}
|
|
1
|
+
import Shader from "../render/Shader";
|
|
2
|
+
import ShaderFunction from "./ShaderFunction";
|
|
3
|
+
import ShaderProgram from "../render/webgl/ShaderProgram";
|
|
4
|
+
import Mat4 from "../math/Mat4";
|
|
5
|
+
import Vec from "../math/Vec";
|
|
6
|
+
import Light, {
|
|
7
|
+
LightType
|
|
8
|
+
} from "../base/Light";
|
|
9
|
+
import {
|
|
10
|
+
createNormalTexture,
|
|
11
|
+
normalTexture,
|
|
12
|
+
createBRDFIntegrationTexture
|
|
13
|
+
} from "../tools/TextureResourceDatabase";
|
|
14
|
+
import Environment from "../render/Environment";
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
getColorCorrectionFunctions,
|
|
18
|
+
getNormalMapFunctions,
|
|
19
|
+
getPBRFunctions,
|
|
20
|
+
getUniformsFunctions,
|
|
21
|
+
replaceConstants
|
|
22
|
+
} from "./webgl";
|
|
23
|
+
import Renderer from "../render/Renderer";
|
|
24
|
+
import WebGLRenderer from "../render/webgl/Renderer";
|
|
25
|
+
import PolyListRenderer from "../render/webgl/PolyListRenderer";
|
|
26
|
+
import MaterialRenderer from "../render/webgl/MaterialRenderer";
|
|
27
|
+
import WebGLTextureRenderer from "../render/webgl/TextureRenderer";
|
|
28
|
+
import Texture from "../base/Texture";
|
|
29
|
+
|
|
30
|
+
function getShaderProgramForLights(this: PBRLightIBLShader, renderer: Renderer, numLights: number): ShaderProgram {
|
|
31
|
+
const r = renderer as WebGLRenderer;
|
|
32
|
+
|
|
33
|
+
if (this._programs[numLights]) {
|
|
34
|
+
return this._programs[numLights];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const vshader = ShaderFunction.GetShaderCode(`precision mediump float;
|
|
38
|
+
attribute vec3 inPosition;
|
|
39
|
+
attribute vec3 inNormal;
|
|
40
|
+
attribute vec2 inTexCoord;
|
|
41
|
+
attribute vec2 inTexCoord2;
|
|
42
|
+
attribute vec3 inTangent;
|
|
43
|
+
|
|
44
|
+
uniform mat4 uWorld;
|
|
45
|
+
uniform mat4 uView;
|
|
46
|
+
uniform mat4 uViewInverse;
|
|
47
|
+
uniform mat4 uProj;
|
|
48
|
+
uniform mat3 uNormMatrix;
|
|
49
|
+
|
|
50
|
+
varying vec3 fragPos;
|
|
51
|
+
varying vec3 fragViewPos;
|
|
52
|
+
varying vec3 fragNorm;
|
|
53
|
+
varying vec2 fragTexCoord;
|
|
54
|
+
varying vec2 fragTexCoord2;
|
|
55
|
+
varying vec3 fragLightPositions[${numLights}];
|
|
56
|
+
|
|
57
|
+
varying mat3 fragTBN;
|
|
58
|
+
|
|
59
|
+
uniform vec3 uLightPositions[${numLights}];
|
|
60
|
+
uniform mat4 uLightTransforms[${numLights}];
|
|
61
|
+
|
|
62
|
+
// Used to calculate shadow from depth texture
|
|
63
|
+
uniform mat4 uLightPovMvp;
|
|
64
|
+
varying vec4 fragPositionFromLightPov;
|
|
65
|
+
|
|
66
|
+
`,
|
|
67
|
+
[
|
|
68
|
+
new ShaderFunction('void','main','',`{
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
for (int i = 0; i < ${numLights}; ++i)
|
|
72
|
+
{
|
|
73
|
+
fragLightPositions[i] = (uLightTransforms[i] * vec4(uLightPositions[i], 1.0)).xyz;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
fragPos = (uWorld * vec4(inPosition, 1.0)).xyz;
|
|
77
|
+
|
|
78
|
+
fragPositionFromLightPov = uLightPovMvp * vec4(fragPos, 1.0);
|
|
79
|
+
|
|
80
|
+
fragViewPos = (uViewInverse * vec4(0.0,0.0,0.0,1.0)).xyz;
|
|
81
|
+
fragTexCoord = inTexCoord;
|
|
82
|
+
fragTexCoord2 = inTexCoord2;
|
|
83
|
+
fragNorm = normalize(uNormMatrix * inNormal);
|
|
84
|
+
gl_Position = uProj * uView * uWorld * vec4(inPosition,1.0);
|
|
85
|
+
fragTBN = TBNMatrix(uWorld, inNormal, inTangent);
|
|
86
|
+
}`, [
|
|
87
|
+
...getNormalMapFunctions()
|
|
88
|
+
])
|
|
89
|
+
]);
|
|
90
|
+
|
|
91
|
+
const deps = [
|
|
92
|
+
...getColorCorrectionFunctions(),
|
|
93
|
+
...getNormalMapFunctions(),
|
|
94
|
+
...getUniformsFunctions(),
|
|
95
|
+
...getPBRFunctions()
|
|
96
|
+
];
|
|
97
|
+
|
|
98
|
+
const fshader = replaceConstants(ShaderFunction.GetShaderCode(`precision mediump float;
|
|
99
|
+
varying vec3 fragPos;
|
|
100
|
+
varying vec3 fragNorm;
|
|
101
|
+
varying vec2 fragTexCoord;
|
|
102
|
+
varying vec2 fragTexCoord2;
|
|
103
|
+
varying vec3 fragViewPos;
|
|
104
|
+
varying vec3 fragLightPositions[${numLights}];
|
|
105
|
+
|
|
106
|
+
uniform vec3 uCameraPos;
|
|
107
|
+
|
|
108
|
+
uniform sampler2D uAlbedoTexture;
|
|
109
|
+
uniform sampler2D uNormalTexture;
|
|
110
|
+
uniform sampler2D uMetallicRoughnessHeightAOTexture;
|
|
111
|
+
|
|
112
|
+
uniform vec4 uAlbedo;
|
|
113
|
+
uniform float uMetallic;
|
|
114
|
+
uniform float uRoughness;
|
|
115
|
+
uniform float uLightEmission;
|
|
116
|
+
|
|
117
|
+
uniform vec4 uFresnelTint;
|
|
118
|
+
uniform vec4 uSheenColor;
|
|
119
|
+
uniform float uSheenIntensity;
|
|
120
|
+
|
|
121
|
+
uniform vec2 uAlbedoScale;
|
|
122
|
+
uniform vec2 uNormalScale;
|
|
123
|
+
uniform vec2 uMetallicScale;
|
|
124
|
+
uniform vec2 uRoughnessScale;
|
|
125
|
+
uniform vec2 uLightEmissionScale;
|
|
126
|
+
|
|
127
|
+
uniform float uAlphaTresshold;
|
|
128
|
+
|
|
129
|
+
uniform int uLightTypes[${numLights}];
|
|
130
|
+
uniform vec3 uLightDirections[${numLights}];
|
|
131
|
+
uniform vec3 uLightColors[${numLights}];
|
|
132
|
+
uniform float uLightIntensities[${numLights}];
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
uniform samplerCube uEnvMap;
|
|
136
|
+
uniform samplerCube uSpecularMap;
|
|
137
|
+
uniform samplerCube uIrradianceMap;
|
|
138
|
+
uniform sampler2D uBRDFIntegrationMap;
|
|
139
|
+
uniform float uAmbientIntensity;
|
|
140
|
+
|
|
141
|
+
uniform sampler2D uShadowMap;
|
|
142
|
+
uniform float uShadowBias;
|
|
143
|
+
varying vec4 fragPositionFromLightPov;
|
|
144
|
+
uniform float uShadowStrength;
|
|
145
|
+
|
|
146
|
+
uniform int uAlbedoMap;
|
|
147
|
+
uniform int uNormalMap;
|
|
148
|
+
uniform int uAOMap;
|
|
149
|
+
uniform int uMetallicMap;
|
|
150
|
+
uniform int uRoughnessMap;
|
|
151
|
+
uniform int uLightemissionMap;
|
|
152
|
+
|
|
153
|
+
varying mat3 fragTBN;
|
|
154
|
+
|
|
155
|
+
uniform float uBrightness;
|
|
156
|
+
uniform float uContrast;
|
|
157
|
+
`,
|
|
158
|
+
[
|
|
159
|
+
new ShaderFunction('void','main','',`{
|
|
160
|
+
float gamma = 2.2;
|
|
161
|
+
|
|
162
|
+
PBRMaterialData mat;
|
|
163
|
+
mat.albedo = uAlbedo;
|
|
164
|
+
mat.albedoScale = uAlbedoScale;
|
|
165
|
+
mat.normalScale = uNormalScale;
|
|
166
|
+
mat.metalnessScale = uMetallicScale;
|
|
167
|
+
mat.roughnessScale = uRoughnessScale;
|
|
168
|
+
mat.lightEmissionScale = uLightEmissionScale;
|
|
169
|
+
mat.metalness = uMetallic;
|
|
170
|
+
mat.roughness = uRoughness;
|
|
171
|
+
mat.lightEmission = uLightEmission;
|
|
172
|
+
mat.albedoUVSet = uAlbedoMap;
|
|
173
|
+
mat.normalUVSet = uNormalMap;
|
|
174
|
+
mat.metalnessUVSet = uMetallicMap;
|
|
175
|
+
mat.roughnessUVSet = uRoughnessMap;
|
|
176
|
+
mat.lightEmissionUVSet = uLightemissionMap;
|
|
177
|
+
mat.aoUVSet = uAOMap;
|
|
178
|
+
mat.fresnelTint = uFresnelTint;
|
|
179
|
+
mat.sheenColor = uSheenColor;
|
|
180
|
+
mat.sheenIntensity = uSheenIntensity;
|
|
181
|
+
|
|
182
|
+
vec4 albedo = sampleAlbedo(uAlbedoTexture, fragTexCoord, fragTexCoord2, mat, gamma);
|
|
183
|
+
float metallic = sampleMetallic(uMetallicRoughnessHeightAOTexture, fragTexCoord, fragTexCoord2, mat);
|
|
184
|
+
float roughness = sampleRoughness(uMetallicRoughnessHeightAOTexture, fragTexCoord, fragTexCoord2, mat);
|
|
185
|
+
|
|
186
|
+
// TODO: Light emission
|
|
187
|
+
float ambientOcclussion = sampleAmbientOcclussion(uMetallicRoughnessHeightAOTexture, fragTexCoord, fragTexCoord2, mat);
|
|
188
|
+
|
|
189
|
+
vec3 normal = sampleNormal(uNormalTexture, fragTexCoord, fragTexCoord2, mat, fragTBN);
|
|
190
|
+
if (!gl_FrontFacing) {
|
|
191
|
+
normal = -normal;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
vec3 viewDir = normalize(fragViewPos - fragPos);
|
|
195
|
+
vec3 F0 = calcF0(albedo.rgb, mat);
|
|
196
|
+
|
|
197
|
+
if (albedo.a < uAlphaTresshold) {
|
|
198
|
+
discard;
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
vec3 Lo = vec3(0.0);
|
|
202
|
+
for (int i = 0; i < ${numLights}; ++i) {
|
|
203
|
+
Light light;
|
|
204
|
+
light.type = uLightTypes[i];
|
|
205
|
+
light.color = vec4(uLightColors[i], 1.0);
|
|
206
|
+
light.intensity = uLightIntensities[i];
|
|
207
|
+
light.direction = uLightDirections[i];
|
|
208
|
+
light.position = fragLightPositions[i];
|
|
209
|
+
|
|
210
|
+
Lo += calcRadiance(light, viewDir, fragPos, metallic, roughness, F0, normal, albedo.rgb, mat.sheenIntensity, mat.sheenColor.rgb, ambientOcclussion);
|
|
211
|
+
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
vec3 shadowColor = getShadowColor(fragPositionFromLightPov, uShadowMap, uShadowBias, uShadowStrength);
|
|
215
|
+
|
|
216
|
+
Lo = Lo * shadowColor;
|
|
217
|
+
|
|
218
|
+
float ambientIntensity = 2.0;
|
|
219
|
+
vec3 ambient = calcAmbientLight(
|
|
220
|
+
viewDir, normal, F0,
|
|
221
|
+
albedo.rgb, metallic, roughness,
|
|
222
|
+
uIrradianceMap, uSpecularMap, uEnvMap,
|
|
223
|
+
uBRDFIntegrationMap, ambientOcclussion,
|
|
224
|
+
mat.sheenIntensity, mat.sheenColor.rgb,
|
|
225
|
+
shadowColor,
|
|
226
|
+
ambientIntensity
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
vec3 color = ambient + Lo;
|
|
230
|
+
|
|
231
|
+
color = color / (color + vec3(1.0));
|
|
232
|
+
gl_FragColor = lineal2SRGB(vec4(color, 1.0), gamma);
|
|
233
|
+
gl_FragColor = brightnessContrast(vec4(color, albedo.a), uBrightness, uContrast);
|
|
234
|
+
}
|
|
235
|
+
}`, deps)
|
|
236
|
+
]));
|
|
237
|
+
|
|
238
|
+
this._programs[numLights] = ShaderProgram.Create(r.gl, "PBRLightIBL", vshader, fshader);
|
|
239
|
+
return this._programs[numLights];
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export default class PBRLightIBLShader extends Shader {
|
|
243
|
+
protected _programs: { [key: number]: ShaderProgram };
|
|
244
|
+
protected _lights: Light[];
|
|
245
|
+
protected _lightTransforms: Mat4[];
|
|
246
|
+
protected _lightTypes: LightType[] = [];
|
|
247
|
+
protected _lightPositions: Vec[] = [];
|
|
248
|
+
protected _lightDirections: Vec[] = [];
|
|
249
|
+
protected _lightColors: Vec[] = [];
|
|
250
|
+
protected _lightIntensities: number[] = [];
|
|
251
|
+
protected _brightness: number;
|
|
252
|
+
protected _contrast: number;
|
|
253
|
+
|
|
254
|
+
protected _cameraPosition: Vec;
|
|
255
|
+
protected _brdfIntegrationTexture: Texture | null = null;
|
|
256
|
+
protected _environment: Environment | null = null;
|
|
257
|
+
protected _program: ShaderProgram | null = null;
|
|
258
|
+
|
|
259
|
+
constructor(renderer: Renderer) {
|
|
260
|
+
super(renderer);
|
|
261
|
+
|
|
262
|
+
this._lights = [];
|
|
263
|
+
this._lightTransforms = [];
|
|
264
|
+
|
|
265
|
+
this._brightness = 0.2;
|
|
266
|
+
this._contrast = 1.1;
|
|
267
|
+
|
|
268
|
+
this._cameraPosition = new Vec(0,0,0);
|
|
269
|
+
|
|
270
|
+
if (renderer.typeId !== "WebGL") {
|
|
271
|
+
throw Error("PresentTextureShader is only compatible with WebGL renderer");
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
this._programs = {};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async load() {
|
|
278
|
+
await createNormalTexture(this.renderer);
|
|
279
|
+
|
|
280
|
+
this._brdfIntegrationTexture = await createBRDFIntegrationTexture(this.renderer);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
get brightness() {
|
|
284
|
+
return this._brightness;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
get contrast() {
|
|
288
|
+
return this._contrast;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
set brightness(b) {
|
|
292
|
+
this._brightness = b;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
set contrast(c) {
|
|
296
|
+
this._contrast = c;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
set cameraPosition(p: Vec) {
|
|
300
|
+
this._cameraPosition = p;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
set environment(env: Environment | null) {
|
|
304
|
+
this._environment = env;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
get environmentMap() {
|
|
308
|
+
return this._environment ? this._environment.environmentMap : null;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
get specularMap() {
|
|
312
|
+
return this._environment ? this._environment.specularMap : null;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
get irradianceMap() {
|
|
316
|
+
return this._environment ? this._environment.irradianceMap : null;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
set lights(l) {
|
|
320
|
+
if (!l.length) {
|
|
321
|
+
throw new Error("BasicPBRLightShader: Invalid light array set.");
|
|
322
|
+
}
|
|
323
|
+
this._lights = l;
|
|
324
|
+
this._lightTypes = [];
|
|
325
|
+
this._lightPositions = [];
|
|
326
|
+
this._lightDirections = [];
|
|
327
|
+
this._lightColors = [];
|
|
328
|
+
this._lightIntensities = [];
|
|
329
|
+
this._lights.forEach((light,i) => {
|
|
330
|
+
this._lightTypes.push(light.type);
|
|
331
|
+
const trx = this._lightTransforms[i];
|
|
332
|
+
const rot = trx && Mat4.GetRotation(trx);
|
|
333
|
+
const position = new Vec(light.position);
|
|
334
|
+
const direction = new Vec(light.direction);
|
|
335
|
+
this._lightPositions.push(trx ? trx.multVector(position).xyz : position);
|
|
336
|
+
this._lightDirections.push(rot ? rot.multVector(direction).xyz.normalize() : direction);
|
|
337
|
+
this._lightColors.push(new Vec(light.color));
|
|
338
|
+
this._lightIntensities.push(light.intensity);
|
|
339
|
+
});
|
|
340
|
+
this._program = getShaderProgramForLights.apply(this, [this.renderer, this._lights.length]);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
get lights() {
|
|
344
|
+
return this._lights;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
set lightTransforms(lt) {
|
|
348
|
+
this._lightTransforms = lt;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
get lightTransforms() {
|
|
352
|
+
return this._lightTransforms;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
updateLight(light: Light, index: number) {
|
|
356
|
+
if (index >= this._lights.length) {
|
|
357
|
+
throw new Error("BasicPBRLightShader: Invalid light index updating light data");
|
|
358
|
+
}
|
|
359
|
+
this._lightTypes[index] = light.type;
|
|
360
|
+
this._lightPositions[index] = new Vec(light.position);
|
|
361
|
+
this._lightDirections[index] = new Vec(light.direction);
|
|
362
|
+
this._lightColors[index] = new Vec(light.color);
|
|
363
|
+
this._lightIntensities[index] = light.intensity;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
updateLights(lights: Light[]) {
|
|
367
|
+
if (this._lights.length !== lights.length) {
|
|
368
|
+
this.lights = lights;
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
lights.forEach((l,i) => this.updateLight(l,i));
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
setup(
|
|
376
|
+
plistRenderer: PolyListRenderer,
|
|
377
|
+
materialRenderer: MaterialRenderer,
|
|
378
|
+
modelMatrix: Mat4,
|
|
379
|
+
viewMatrix: Mat4,
|
|
380
|
+
projectionMatrix: Mat4
|
|
381
|
+
) {
|
|
382
|
+
const rend = this.renderer as WebGLRenderer;
|
|
383
|
+
if (!this._program) {
|
|
384
|
+
throw new Error("BasicPBRLightShader: lights array not specified");
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const material = materialRenderer.material;
|
|
388
|
+
materialRenderer.mergeTextures();
|
|
389
|
+
rend.state.shaderProgram = this._program;
|
|
390
|
+
|
|
391
|
+
const normMatrix = Mat4.GetNormalMatrix(modelMatrix);
|
|
392
|
+
this._program.bindMatrix('uNormMatrix', normMatrix);
|
|
393
|
+
this._program.bindMatrix('uWorld', modelMatrix);
|
|
394
|
+
this._program.bindMatrix('uView', viewMatrix);
|
|
395
|
+
this._program.bindMatrix('uViewInverse', Mat4.GetInverted(viewMatrix));
|
|
396
|
+
this._program.bindMatrix('uProj', projectionMatrix);
|
|
397
|
+
|
|
398
|
+
if (!this._cameraPosition) {
|
|
399
|
+
console.warn("Serious performance warning: camera position not set in BasicPBRLightShader. Extracting the camera position from the view matrix involves inverting the matrix.");
|
|
400
|
+
this._program.bindVector('uCameraPos',Mat4.GetPosition(Mat4.GetInverted(viewMatrix)));
|
|
401
|
+
}
|
|
402
|
+
else {
|
|
403
|
+
this._program.bindVector('uCameraPos',this._cameraPosition);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
materialRenderer.bindTexture(this._program, 'albedoTexture', 'uAlbedoTexture', 0);
|
|
407
|
+
materialRenderer.bindTexture(this._program, 'normalTexture', 'uNormalTexture', 1, normalTexture(this.renderer));
|
|
408
|
+
materialRenderer.bindMetalnessRoughnessHeightAOTexture(this._program, 'uMetallicRoughnessHeightAOTexture', 2);
|
|
409
|
+
this._program.uniform1f('uAlphaTresshold', material.alphaCutoff);
|
|
410
|
+
|
|
411
|
+
materialRenderer.bindColor(this._program, 'albedo', 'uAlbedo');
|
|
412
|
+
materialRenderer.bindValue(this._program, 'metalness', 'uMetallic');
|
|
413
|
+
materialRenderer.bindValue(this._program, 'roughness', 'uRoughness');
|
|
414
|
+
materialRenderer.bindValue(this._program, 'lightEmission', 'uLightEmission', 0);
|
|
415
|
+
|
|
416
|
+
this._program.uniform1i('uAlbedoMap', material.albedoUV);
|
|
417
|
+
this._program.uniform1i('uNormalMap', material.normalUV);
|
|
418
|
+
this._program.uniform1i('uAOMap', material.ambientOcclussionUV);
|
|
419
|
+
this._program.uniform1i('uMetallicMap', material.metalnessChannel);
|
|
420
|
+
this._program.uniform1i('uRoughnessMap', material.roughnessChannel);
|
|
421
|
+
this._program.uniform1i('uLightemissionMap', material.lightEmissionChannel);
|
|
422
|
+
|
|
423
|
+
this._program.bindVector("uAlbedoScale", material.albedoScale);
|
|
424
|
+
this._program.bindVector("uNormalScale", material.normalScale);
|
|
425
|
+
this._program.bindVector("uMetallicScale", material.metalnessScale);
|
|
426
|
+
this._program.bindVector("uRoughnessScale", material.roughnessScale);
|
|
427
|
+
this._program.bindVector("uLightEmissionScale", material.lightEmissionScale);
|
|
428
|
+
this._program.bindVector("uFresnelTint", material.fresnelTint);
|
|
429
|
+
this._program.bindVector("uSheenColor", material.sheenColor);
|
|
430
|
+
this._program.uniform1f("uSheenIntensity", material.sheenIntensity);
|
|
431
|
+
|
|
432
|
+
if (!this.irradianceMap || !this.specularMap || !this.environmentMap || !this._brdfIntegrationTexture) {
|
|
433
|
+
throw new Error("PBRLightIBLShader: Environment maps not set.");
|
|
434
|
+
}
|
|
435
|
+
const irMap = this.renderer.factory.texture(this.irradianceMap) as WebGLTextureRenderer;
|
|
436
|
+
const specMap = this.renderer.factory.texture(this.specularMap) as WebGLTextureRenderer;
|
|
437
|
+
const envMap = this.renderer.factory.texture(this.environmentMap) as WebGLTextureRenderer;
|
|
438
|
+
const brdfLut = this.renderer.factory.texture(this._brdfIntegrationTexture) as WebGLTextureRenderer;
|
|
439
|
+
|
|
440
|
+
this._program.bindTexture("uIrradianceMap", irMap, 3);
|
|
441
|
+
this._program.bindTexture("uSpecularMap", specMap, 4);
|
|
442
|
+
this._program.bindTexture("uEnvMap", envMap, 5);
|
|
443
|
+
this._program.bindTexture("uBRDFIntegrationMap", brdfLut, 6);
|
|
444
|
+
|
|
445
|
+
this._program.uniform1f("uBrightness", this._brightness);
|
|
446
|
+
this._program.uniform1f("uContrast", this._contrast);
|
|
447
|
+
|
|
448
|
+
// TODO: Get the ambient intensity from environment
|
|
449
|
+
this._program.uniform1f("uAmbientIntensity", 1.0);
|
|
450
|
+
|
|
451
|
+
let shadowLight: Light | null = this._lights.find(light => light.depthTexture) || null;
|
|
452
|
+
this._lights.forEach((light,i) => {
|
|
453
|
+
if (light.depthTexture) {
|
|
454
|
+
shadowLight = light;
|
|
455
|
+
}
|
|
456
|
+
this._program?.uniform1i(`uLightTypes[${i}]`, this._lightTypes[i]);
|
|
457
|
+
this._program?.bindVector(`uLightPositions[${i}]`, this._lightPositions[i]);
|
|
458
|
+
this._program?.bindVector(`uLightDirections[${i}]`, this._lightDirections[i]);
|
|
459
|
+
this._program?.bindVector(`uLightColors[${i}]`, this._lightColors[i].rgb);
|
|
460
|
+
this._program?.uniform1f(`uLightIntensities[${i}]`, this._lightIntensities[i]);
|
|
461
|
+
this._program?.bindMatrix(`uLightTransforms[${i}]`, this._lightTransforms[i]);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
if (shadowLight?.depthTexture) {
|
|
465
|
+
const depthTex = this.renderer.factory.texture(shadowLight.depthTexture) as WebGLTextureRenderer;
|
|
466
|
+
this._program.bindTexture("uShadowMap", depthTex, 7);
|
|
467
|
+
const projectionViewMatrix = Mat4.Mult(shadowLight.projection, shadowLight.viewMatrix || Mat4.MakeIdentity());
|
|
468
|
+
this._program.bindMatrix("uLightPovMvp", projectionViewMatrix);
|
|
469
|
+
this._program.uniform1f("uShadowBias", shadowLight.shadowBias * 30);
|
|
470
|
+
this._program.uniform1f("uShadowStrength", shadowLight.shadowStrength);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
this._program.bindAttribs(plistRenderer, {
|
|
474
|
+
position: "inPosition",
|
|
475
|
+
normal: "inNormal",
|
|
476
|
+
tex0: "inTexCoord",
|
|
477
|
+
tex1: "inTexCoord2",
|
|
478
|
+
tangent: "inTangent"
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
destroy() {
|
|
483
|
+
if (this._program) {
|
|
484
|
+
ShaderProgram.Delete(this._program);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
487
|
}
|