@plasius/gpu-lighting 0.2.0 → 0.2.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/index.js CHANGED
@@ -176,9 +176,37 @@ var lightingDistanceBands = Object.freeze([
176
176
  ]);
177
177
  var lightingWorkerQueueClass = "lighting";
178
178
  var lightingDebugOwner = "lighting";
179
+ var environmentPresetAmbientScales = Object.freeze({
180
+ "grass-field": 0.78,
181
+ forest: 0.78,
182
+ warehouse: 0.82,
183
+ cavern: 0.78
184
+ });
185
+ var environmentSunlitBaselineByTimeOfDay = Object.freeze({
186
+ dawn: 0.18,
187
+ midday: 0.28,
188
+ dusk: 0.14,
189
+ night: 0.035
190
+ });
191
+ var environmentSunlitBaselineSceneScales = Object.freeze({
192
+ "grass-field": 1,
193
+ forest: 0.78,
194
+ warehouse: 0.62,
195
+ cavern: 0.42,
196
+ harbor: 0.85,
197
+ studio: 0.58
198
+ });
179
199
  function freezeVec4(value) {
180
200
  return Object.freeze([value[0], value[1], value[2], value[3] ?? 1]);
181
201
  }
202
+ function scaleVec4(value, scale) {
203
+ return [
204
+ value[0] * scale,
205
+ value[1] * scale,
206
+ value[2] * scale,
207
+ value[3] ?? 1
208
+ ];
209
+ }
182
210
  function normalizeVector3(value, fallback) {
183
211
  if (!Array.isArray(value) || value.length < 3) {
184
212
  return [...fallback];
@@ -343,6 +371,22 @@ function normalizeEnvironmentPortals(value) {
343
371
  }
344
372
  return Object.freeze(value.map(normalizeEnvironmentPortal));
345
373
  }
374
+ function normalizeEnvironmentMap(value) {
375
+ if (value == null) {
376
+ return null;
377
+ }
378
+ if (!value || typeof value !== "object") {
379
+ throw new Error("environmentMap must be an object when provided.");
380
+ }
381
+ return Object.freeze({
382
+ ...value,
383
+ id: typeof value.id === "string" && value.id.length > 0 ? value.id : "environment-map",
384
+ projection: typeof value.projection === "string" && value.projection.length > 0 ? value.projection : "equirectangular",
385
+ intensity: readPositiveFinite(value.intensity ?? value.radianceScale, 1),
386
+ rotationRadians: readFinite(value.rotationRadians ?? value.rotation, 0),
387
+ ambientStrength: Math.max(0, readFinite(value.ambientStrength, 0.32))
388
+ });
389
+ }
346
390
  function freezeLightSourceSpec(source) {
347
391
  return Object.freeze({
348
392
  ...source,
@@ -352,15 +396,27 @@ function freezeLightSourceSpec(source) {
352
396
  });
353
397
  }
354
398
  function defineEnvironmentPreset(spec) {
399
+ const scene = spec.scene ?? "studio";
400
+ const timeOfDay = spec.timeOfDay ?? "midday";
401
+ const ambientScale = Math.max(
402
+ 0,
403
+ readFinite(spec.ambientScale, environmentPresetAmbientScales[scene] ?? 1)
404
+ );
405
+ const defaultSunlitBaseline = (environmentSunlitBaselineByTimeOfDay[timeOfDay] ?? environmentSunlitBaselineByTimeOfDay.midday) * (environmentSunlitBaselineSceneScales[scene] ?? 0.58);
406
+ const sunlitBaseline = Math.max(
407
+ 0,
408
+ readFinite(spec.sunlitBaseline, defaultSunlitBaseline)
409
+ );
355
410
  return Object.freeze({
356
411
  ...spec,
357
- scene: spec.scene ?? "studio",
358
- timeOfDay: spec.timeOfDay ?? "midday",
412
+ scene,
413
+ timeOfDay,
359
414
  horizonColor: freezeVec4(spec.horizonColor),
360
415
  zenithColor: freezeVec4(spec.zenithColor),
361
416
  sunDirection: Object.freeze(normalizeVector3(spec.sunDirection, [0, 1, 0])),
362
417
  sunColor: freezeVec4(spec.sunColor),
363
- ambientColor: freezeVec4(spec.ambientColor),
418
+ ambientColor: freezeVec4(scaleVec4(spec.ambientColor, ambientScale)),
419
+ sunlitBaseline,
364
420
  environmentLightSources: Object.freeze(
365
421
  (spec.environmentLightSources ?? []).map(freezeLightSourceSpec)
366
422
  )
@@ -888,6 +944,9 @@ function createEnvironmentLightingConfig(options = {}) {
888
944
  const environmentPortals = normalizeEnvironmentPortals(
889
945
  options.environmentPortals ?? options.portals
890
946
  );
947
+ const environmentMap = normalizeEnvironmentMap(
948
+ options.environmentMap ?? options.hdri ?? preset.environmentMap
949
+ );
891
950
  const environmentPortalMode = normalizeEnvironmentPortalMode(
892
951
  options.environmentPortalMode ?? options.portalMode,
893
952
  environmentPortals.length > 0
@@ -911,8 +970,13 @@ function createEnvironmentLightingConfig(options = {}) {
911
970
  ),
912
971
  sunColor: readColor(options.sunColor, preset.sunColor),
913
972
  ambientColor: readColor(options.ambientColor, preset.ambientColor),
973
+ sunlitBaseline: Math.max(
974
+ 0,
975
+ readFinite(options.sunlitBaseline ?? options.daylightBaseline, preset.sunlitBaseline)
976
+ ),
914
977
  environmentPortalMode,
915
- environmentPortals
978
+ environmentPortals,
979
+ environmentMap
916
980
  };
917
981
  const environmentLightSources = normalizeEnvironmentLightSources(
918
982
  options.environmentLightSources ?? options.lightSources,
@@ -940,8 +1004,10 @@ function createEnvironmentLightingConfig(options = {}) {
940
1004
  wavefront: Object.freeze({
941
1005
  environmentColor,
942
1006
  ambientColor: config.ambientColor,
1007
+ sunlitBaseline: config.sunlitBaseline,
943
1008
  environmentPortalMode: config.environmentPortalMode,
944
1009
  environmentPortals: config.environmentPortals,
1010
+ environmentMap: config.environmentMap,
945
1011
  environmentLightSources: config.environmentLightSources,
946
1012
  lightSources: config.environmentLightSources,
947
1013
  dominantLightSource,
@@ -954,8 +1020,10 @@ function createEnvironmentLightingConfig(options = {}) {
954
1020
  intensity: config.environmentIntensity,
955
1021
  mode: config.environmentMode,
956
1022
  exposure: config.exposure,
1023
+ sunlitBaseline: config.sunlitBaseline,
957
1024
  environmentPortalMode: config.environmentPortalMode,
958
1025
  environmentPortalCount: config.environmentPortals.length,
1026
+ environmentMap: config.environmentMap,
959
1027
  environmentLightSources: config.environmentLightSources,
960
1028
  environmentLightSourceCount: config.environmentLightSources.length,
961
1029
  dominantLightSource,
@@ -969,8 +1037,10 @@ function createWavefrontEnvironmentLightingOptions(options = {}) {
969
1037
  return Object.freeze({
970
1038
  environmentColor: config.wavefront.environmentColor,
971
1039
  ambientColor: config.wavefront.ambientColor,
1040
+ sunlitBaseline: config.wavefront.sunlitBaseline,
972
1041
  environmentPortalMode: config.wavefront.environmentPortalMode,
973
1042
  environmentPortals: config.wavefront.environmentPortals,
1043
+ environmentMap: config.wavefront.environmentMap,
974
1044
  environmentLightSources: config.wavefront.environmentLightSources,
975
1045
  lightSources: config.wavefront.environmentLightSources,
976
1046
  dominantLightSource: config.wavefront.dominantLightSource,