@preference-sl/pref-viewer 2.6.0 → 2.8.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.
- package/package.json +1 -1
- package/src/index.js +57 -9
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -37,7 +37,9 @@ import {
|
|
|
37
37
|
SceneLoader,
|
|
38
38
|
Color4,
|
|
39
39
|
HemisphericLight,
|
|
40
|
-
DirectionalLight
|
|
40
|
+
DirectionalLight,
|
|
41
|
+
PointLight,
|
|
42
|
+
ShadowGenerator
|
|
41
43
|
} from "@babylonjs/core";
|
|
42
44
|
import "@babylonjs/loaders";
|
|
43
45
|
|
|
@@ -54,6 +56,8 @@ class PrefViewer extends HTMLElement {
|
|
|
54
56
|
this.camera = null;
|
|
55
57
|
this.hemiLight = null;
|
|
56
58
|
this.dirLight = null;
|
|
59
|
+
this.cameraLight = null;
|
|
60
|
+
this.shadowGen = null;
|
|
57
61
|
this._onWindowResize = null;
|
|
58
62
|
|
|
59
63
|
// Model sources
|
|
@@ -109,7 +113,8 @@ class PrefViewer extends HTMLElement {
|
|
|
109
113
|
}
|
|
110
114
|
|
|
111
115
|
_setupUrlPreprocessing() {
|
|
112
|
-
const transformUrl = (url) =>
|
|
116
|
+
const transformUrl = (url) =>
|
|
117
|
+
url.replace(/^blob:[^/]+\//i, "").replace(/\\/g, "/");
|
|
113
118
|
SceneLoader.OnPluginActivatedObservable.add((plugin) => {
|
|
114
119
|
if (plugin.name === "gltf" || plugin.name === "gltf2") {
|
|
115
120
|
plugin.preprocessUrl = transformUrl;
|
|
@@ -156,11 +161,37 @@ class PrefViewer extends HTMLElement {
|
|
|
156
161
|
}
|
|
157
162
|
|
|
158
163
|
_createLights() {
|
|
159
|
-
|
|
160
|
-
this.hemiLight
|
|
161
|
-
|
|
164
|
+
// 1) Stronger ambient fill
|
|
165
|
+
this.hemiLight = new HemisphericLight(
|
|
166
|
+
"hemiLight",
|
|
167
|
+
new Vector3(0, 1, 0),
|
|
168
|
+
this.scene
|
|
169
|
+
);
|
|
170
|
+
this.hemiLight.intensity = 0.8;
|
|
171
|
+
|
|
172
|
+
// 2) Soft directional “sun” for form
|
|
173
|
+
this.dirLight = new DirectionalLight(
|
|
174
|
+
"dirLight",
|
|
175
|
+
new Vector3(-0.5, -1, -0.5),
|
|
176
|
+
this.scene
|
|
177
|
+
);
|
|
162
178
|
this.dirLight.position = new Vector3(0, 5, 0);
|
|
163
|
-
this.dirLight.intensity = 0.
|
|
179
|
+
this.dirLight.intensity = 0.3;
|
|
180
|
+
|
|
181
|
+
// 3) Soft blurred shadows
|
|
182
|
+
this.shadowGen = new ShadowGenerator(1024, this.dirLight);
|
|
183
|
+
this.shadowGen.useBlurExponentialShadowMap = true;
|
|
184
|
+
this.shadowGen.blurKernel = 16;
|
|
185
|
+
this.shadowGen.darkness = 0.2;
|
|
186
|
+
|
|
187
|
+
// 4) Camera‐attached headlight
|
|
188
|
+
this.cameraLight = new PointLight(
|
|
189
|
+
"cameraLight",
|
|
190
|
+
this.camera.position,
|
|
191
|
+
this.scene
|
|
192
|
+
);
|
|
193
|
+
this.cameraLight.parent = this.camera;
|
|
194
|
+
this.cameraLight.intensity = 0.5;
|
|
164
195
|
}
|
|
165
196
|
|
|
166
197
|
_setupInteraction() {
|
|
@@ -189,7 +220,22 @@ class PrefViewer extends HTMLElement {
|
|
|
189
220
|
result = await SceneLoader.ImportMeshAsync(null, "", this.modelUrl, this.scene, undefined, extension);
|
|
190
221
|
}
|
|
191
222
|
|
|
192
|
-
|
|
223
|
+
// Remove any extra lights that the loader might have added
|
|
224
|
+
this.scene.lights
|
|
225
|
+
.filter(
|
|
226
|
+
(light) =>
|
|
227
|
+
!["hemiLight", "dirLight", "cameraLight"].includes(
|
|
228
|
+
light.name
|
|
229
|
+
)
|
|
230
|
+
)
|
|
231
|
+
.forEach((light) => light.dispose());
|
|
232
|
+
|
|
233
|
+
// Hook up our soft shadows to every mesh
|
|
234
|
+
result.meshes.forEach((mesh) => {
|
|
235
|
+
mesh.receiveShadows = true;
|
|
236
|
+
this.shadowGen.addShadowCaster(mesh, true);
|
|
237
|
+
});
|
|
238
|
+
|
|
193
239
|
this.dispatchEvent(
|
|
194
240
|
new CustomEvent("model-loaded", { detail: result, bubbles: true, composed: true })
|
|
195
241
|
);
|
|
@@ -211,7 +257,7 @@ class PrefViewer extends HTMLElement {
|
|
|
211
257
|
const raw = payload || base64;
|
|
212
258
|
const decoded = atob(raw);
|
|
213
259
|
let isJson = false;
|
|
214
|
-
try { JSON.parse(decoded); isJson = true; } catch {}
|
|
260
|
+
try { JSON.parse(decoded); isJson = true; } catch { }
|
|
215
261
|
const extension = isJson ? ".gltf" : ".glb";
|
|
216
262
|
const type = isJson ? "model/gltf+json" : "model/gltf-binary";
|
|
217
263
|
const array = Uint8Array.from(decoded, (c) => c.charCodeAt(0));
|
|
@@ -222,7 +268,9 @@ class PrefViewer extends HTMLElement {
|
|
|
222
268
|
_disposeEngine() {
|
|
223
269
|
if (!this.engine) return;
|
|
224
270
|
this.engine.dispose();
|
|
225
|
-
this.engine = this.scene = this.camera =
|
|
271
|
+
this.engine = this.scene = this.camera = null;
|
|
272
|
+
this.hemiLight = this.dirLight = this.cameraLight = null;
|
|
273
|
+
this.shadowGen = null;
|
|
226
274
|
}
|
|
227
275
|
}
|
|
228
276
|
|