@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plasius/gpu-lighting",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Advanced lighting WGSL modules and planning profiles for @plasius/gpu-worker.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
package/src/index.js CHANGED
@@ -191,10 +191,42 @@ export const lightingDistanceBands = Object.freeze([
191
191
  export const lightingWorkerQueueClass = "lighting";
192
192
  export const lightingDebugOwner = "lighting";
193
193
 
194
+ const environmentPresetAmbientScales = Object.freeze({
195
+ "grass-field": 0.78,
196
+ forest: 0.78,
197
+ warehouse: 0.82,
198
+ cavern: 0.78,
199
+ });
200
+
201
+ const environmentSunlitBaselineByTimeOfDay = Object.freeze({
202
+ dawn: 0.18,
203
+ midday: 0.28,
204
+ dusk: 0.14,
205
+ night: 0.035,
206
+ });
207
+
208
+ const environmentSunlitBaselineSceneScales = Object.freeze({
209
+ "grass-field": 1,
210
+ forest: 0.78,
211
+ warehouse: 0.62,
212
+ cavern: 0.42,
213
+ harbor: 0.85,
214
+ studio: 0.58,
215
+ });
216
+
194
217
  function freezeVec4(value) {
195
218
  return Object.freeze([value[0], value[1], value[2], value[3] ?? 1]);
196
219
  }
197
220
 
221
+ function scaleVec4(value, scale) {
222
+ return [
223
+ value[0] * scale,
224
+ value[1] * scale,
225
+ value[2] * scale,
226
+ value[3] ?? 1,
227
+ ];
228
+ }
229
+
198
230
  function normalizeVector3(value, fallback) {
199
231
  if (!Array.isArray(value) || value.length < 3) {
200
232
  return [...fallback];
@@ -377,6 +409,27 @@ function normalizeEnvironmentPortals(value) {
377
409
  return Object.freeze(value.map(normalizeEnvironmentPortal));
378
410
  }
379
411
 
412
+ function normalizeEnvironmentMap(value) {
413
+ if (value == null) {
414
+ return null;
415
+ }
416
+ if (!value || typeof value !== "object") {
417
+ throw new Error("environmentMap must be an object when provided.");
418
+ }
419
+ return Object.freeze({
420
+ ...value,
421
+ id: typeof value.id === "string" && value.id.length > 0
422
+ ? value.id
423
+ : "environment-map",
424
+ projection: typeof value.projection === "string" && value.projection.length > 0
425
+ ? value.projection
426
+ : "equirectangular",
427
+ intensity: readPositiveFinite(value.intensity ?? value.radianceScale, 1),
428
+ rotationRadians: readFinite(value.rotationRadians ?? value.rotation, 0),
429
+ ambientStrength: Math.max(0, readFinite(value.ambientStrength, 0.32)),
430
+ });
431
+ }
432
+
380
433
  function freezeLightSourceSpec(source) {
381
434
  return Object.freeze({
382
435
  ...source,
@@ -391,15 +444,30 @@ function freezeLightSourceSpec(source) {
391
444
  }
392
445
 
393
446
  function defineEnvironmentPreset(spec) {
447
+ const scene = spec.scene ?? "studio";
448
+ const timeOfDay = spec.timeOfDay ?? "midday";
449
+ const ambientScale = Math.max(
450
+ 0,
451
+ readFinite(spec.ambientScale, environmentPresetAmbientScales[scene] ?? 1)
452
+ );
453
+ const defaultSunlitBaseline =
454
+ (environmentSunlitBaselineByTimeOfDay[timeOfDay] ??
455
+ environmentSunlitBaselineByTimeOfDay.midday) *
456
+ (environmentSunlitBaselineSceneScales[scene] ?? 0.58);
457
+ const sunlitBaseline = Math.max(
458
+ 0,
459
+ readFinite(spec.sunlitBaseline, defaultSunlitBaseline)
460
+ );
394
461
  return Object.freeze({
395
462
  ...spec,
396
- scene: spec.scene ?? "studio",
397
- timeOfDay: spec.timeOfDay ?? "midday",
463
+ scene,
464
+ timeOfDay,
398
465
  horizonColor: freezeVec4(spec.horizonColor),
399
466
  zenithColor: freezeVec4(spec.zenithColor),
400
467
  sunDirection: Object.freeze(normalizeVector3(spec.sunDirection, [0, 1, 0])),
401
468
  sunColor: freezeVec4(spec.sunColor),
402
- ambientColor: freezeVec4(spec.ambientColor),
469
+ ambientColor: freezeVec4(scaleVec4(spec.ambientColor, ambientScale)),
470
+ sunlitBaseline,
403
471
  environmentLightSources: Object.freeze(
404
472
  (spec.environmentLightSources ?? []).map(freezeLightSourceSpec)
405
473
  ),
@@ -952,6 +1020,9 @@ export function createEnvironmentLightingConfig(options = {}) {
952
1020
  const environmentPortals = normalizeEnvironmentPortals(
953
1021
  options.environmentPortals ?? options.portals
954
1022
  );
1023
+ const environmentMap = normalizeEnvironmentMap(
1024
+ options.environmentMap ?? options.hdri ?? preset.environmentMap
1025
+ );
955
1026
  const environmentPortalMode = normalizeEnvironmentPortalMode(
956
1027
  options.environmentPortalMode ?? options.portalMode,
957
1028
  environmentPortals.length > 0
@@ -975,8 +1046,13 @@ export function createEnvironmentLightingConfig(options = {}) {
975
1046
  ),
976
1047
  sunColor: readColor(options.sunColor, preset.sunColor),
977
1048
  ambientColor: readColor(options.ambientColor, preset.ambientColor),
1049
+ sunlitBaseline: Math.max(
1050
+ 0,
1051
+ readFinite(options.sunlitBaseline ?? options.daylightBaseline, preset.sunlitBaseline)
1052
+ ),
978
1053
  environmentPortalMode,
979
1054
  environmentPortals,
1055
+ environmentMap,
980
1056
  };
981
1057
  const environmentLightSources = normalizeEnvironmentLightSources(
982
1058
  options.environmentLightSources ?? options.lightSources,
@@ -1005,8 +1081,10 @@ export function createEnvironmentLightingConfig(options = {}) {
1005
1081
  wavefront: Object.freeze({
1006
1082
  environmentColor,
1007
1083
  ambientColor: config.ambientColor,
1084
+ sunlitBaseline: config.sunlitBaseline,
1008
1085
  environmentPortalMode: config.environmentPortalMode,
1009
1086
  environmentPortals: config.environmentPortals,
1087
+ environmentMap: config.environmentMap,
1010
1088
  environmentLightSources: config.environmentLightSources,
1011
1089
  lightSources: config.environmentLightSources,
1012
1090
  dominantLightSource,
@@ -1019,8 +1097,10 @@ export function createEnvironmentLightingConfig(options = {}) {
1019
1097
  intensity: config.environmentIntensity,
1020
1098
  mode: config.environmentMode,
1021
1099
  exposure: config.exposure,
1100
+ sunlitBaseline: config.sunlitBaseline,
1022
1101
  environmentPortalMode: config.environmentPortalMode,
1023
1102
  environmentPortalCount: config.environmentPortals.length,
1103
+ environmentMap: config.environmentMap,
1024
1104
  environmentLightSources: config.environmentLightSources,
1025
1105
  environmentLightSourceCount: config.environmentLightSources.length,
1026
1106
  dominantLightSource,
@@ -1035,8 +1115,10 @@ export function createWavefrontEnvironmentLightingOptions(options = {}) {
1035
1115
  return Object.freeze({
1036
1116
  environmentColor: config.wavefront.environmentColor,
1037
1117
  ambientColor: config.wavefront.ambientColor,
1118
+ sunlitBaseline: config.wavefront.sunlitBaseline,
1038
1119
  environmentPortalMode: config.wavefront.environmentPortalMode,
1039
1120
  environmentPortals: config.wavefront.environmentPortals,
1121
+ environmentMap: config.wavefront.environmentMap,
1040
1122
  environmentLightSources: config.wavefront.environmentLightSources,
1041
1123
  lightSources: config.wavefront.environmentLightSources,
1042
1124
  dominantLightSource: config.wavefront.dominantLightSource,