@preference-sl/pref-viewer 2.7.0 → 2.8.1

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +50 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preference-sl/pref-viewer",
3
- "version": "2.7.0",
3
+ "version": "2.8.1",
4
4
  "description": "Web Component to preview GLTF models with Babylon.js",
5
5
  "author": "Alex Moreno Palacio <amoreno@preference.es>",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -56,6 +56,8 @@ class PrefViewer extends HTMLElement {
56
56
  this.camera = null;
57
57
  this.hemiLight = null;
58
58
  this.dirLight = null;
59
+ this.cameraLight = null;
60
+ this.shadowGen = null;
59
61
  this._onWindowResize = null;
60
62
 
61
63
  // Model sources
@@ -111,7 +113,8 @@ class PrefViewer extends HTMLElement {
111
113
  }
112
114
 
113
115
  _setupUrlPreprocessing() {
114
- const transformUrl = (url) => url.replace(/^blob:[^/]+\//i, "").replace(/\\/g, "/");
116
+ const transformUrl = (url) =>
117
+ url.replace(/^blob:[^/]+\//i, "").replace(/\\/g, "/");
115
118
  SceneLoader.OnPluginActivatedObservable.add((plugin) => {
116
119
  if (plugin.name === "gltf" || plugin.name === "gltf2") {
117
120
  plugin.preprocessUrl = transformUrl;
@@ -158,23 +161,35 @@ class PrefViewer extends HTMLElement {
158
161
  }
159
162
 
160
163
  _createLights() {
161
- // Ambient hemisphere for general fill
162
- this.hemiLight = new HemisphericLight("hemiLight", new Vector3(0, 1, 0), this.scene);
163
- this.hemiLight.intensity = 0.8; // stronger ambient
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;
164
171
 
165
- // Main “sun” light, but softer
166
- this.dirLight = new DirectionalLight("dirLight", new Vector3(-0.5, -1, -0.5), this.scene);
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
+ );
167
178
  this.dirLight.position = new Vector3(0, 5, 0);
168
- this.dirLight.intensity = 0.3; // much lower than before
179
+ this.dirLight.intensity = 0.3;
169
180
 
170
- // Optional: very light, blurred shadows just for form (darkness: 0 → no shadows, 1 → full)
171
- const shadowGen = new ShadowGenerator(1024, this.dirLight);
172
- shadowGen.useBlurExponentialShadowMap = true; // soften edges
173
- shadowGen.blurKernel = 16;
174
- shadowGen.darkness = 0.2; // practically “skimmed milk” shadows
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;
175
186
 
176
- // Headlight: ensure the camera always shines on what you’re looking at
177
- this.cameraLight = new PointLight("cameraLight", this.camera.position, this.scene);
187
+ // 4) Camera‐attached headlight
188
+ this.cameraLight = new PointLight(
189
+ "cameraLight",
190
+ this.camera.position,
191
+ this.scene
192
+ );
178
193
  this.cameraLight.parent = this.camera;
179
194
  this.cameraLight.intensity = 0.5;
180
195
  }
@@ -205,7 +220,24 @@ class PrefViewer extends HTMLElement {
205
220
  result = await SceneLoader.ImportMeshAsync(null, "", this.modelUrl, this.scene, undefined, extension);
206
221
  }
207
222
 
208
- this.scene.createDefaultCameraOrLight(true, true, true);
223
+ this.scene.createDefaultCamera(true, true, true);
224
+
225
+ // Remove any extra lights that the loader might have added
226
+ this.scene.lights
227
+ .filter(
228
+ (light) =>
229
+ !["hemiLight", "dirLight", "cameraLight"].includes(
230
+ light.name
231
+ )
232
+ )
233
+ .forEach((light) => light.dispose());
234
+
235
+ // Hook up our soft shadows to every mesh
236
+ result.meshes.forEach((mesh) => {
237
+ mesh.receiveShadows = true;
238
+ this.shadowGen.addShadowCaster(mesh, true);
239
+ });
240
+
209
241
  this.dispatchEvent(
210
242
  new CustomEvent("model-loaded", { detail: result, bubbles: true, composed: true })
211
243
  );
@@ -238,7 +270,9 @@ class PrefViewer extends HTMLElement {
238
270
  _disposeEngine() {
239
271
  if (!this.engine) return;
240
272
  this.engine.dispose();
241
- this.engine = this.scene = this.camera = this.hemiLight = this.dirLight = null;
273
+ this.engine = this.scene = this.camera = null;
274
+ this.hemiLight = this.dirLight = this.cameraLight = null;
275
+ this.shadowGen = null;
242
276
  }
243
277
  }
244
278