@plasius/gpu-lighting 0.1.18 → 0.1.19

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/src/index.js CHANGED
@@ -136,6 +136,11 @@ export const lightingProfileModeOrder = Object.freeze([
136
136
  "hybrid",
137
137
  "reference",
138
138
  ]);
139
+ export const lightingEnvironmentPresetNames = Object.freeze([
140
+ "moonlit-harbor",
141
+ "product-studio",
142
+ "neutral-studio",
143
+ ]);
139
144
  export const defaultAdaptiveLightingProfilePolicy = Object.freeze({
140
145
  preferredProfile: "reference",
141
146
  minimumFrameRate: 30,
@@ -151,6 +156,153 @@ export const lightingDistanceBands = Object.freeze([
151
156
  export const lightingWorkerQueueClass = "lighting";
152
157
  export const lightingDebugOwner = "lighting";
153
158
 
159
+ function freezeVec4(value) {
160
+ return Object.freeze([value[0], value[1], value[2], value[3] ?? 1]);
161
+ }
162
+
163
+ function normalizeVector3(value, fallback) {
164
+ if (!Array.isArray(value) || value.length < 3) {
165
+ return [...fallback];
166
+ }
167
+ const vector = [
168
+ Number.isFinite(value[0]) ? value[0] : fallback[0],
169
+ Number.isFinite(value[1]) ? value[1] : fallback[1],
170
+ Number.isFinite(value[2]) ? value[2] : fallback[2],
171
+ ];
172
+ const length = Math.hypot(vector[0], vector[1], vector[2]);
173
+ if (!Number.isFinite(length) || length <= 0.000001) {
174
+ return [...fallback];
175
+ }
176
+ return vector.map((component) => component / length);
177
+ }
178
+
179
+ function readColor(value, fallback) {
180
+ if (!Array.isArray(value) || value.length < 3) {
181
+ return freezeVec4(fallback);
182
+ }
183
+ return freezeVec4([
184
+ Number.isFinite(value[0]) ? Math.max(0, value[0]) : fallback[0],
185
+ Number.isFinite(value[1]) ? Math.max(0, value[1]) : fallback[1],
186
+ Number.isFinite(value[2]) ? Math.max(0, value[2]) : fallback[2],
187
+ Number.isFinite(value[3]) ? Math.max(0, Math.min(1, value[3])) : fallback[3] ?? 1,
188
+ ]);
189
+ }
190
+
191
+ function readFinite(value, fallback) {
192
+ return Number.isFinite(value) ? value : fallback;
193
+ }
194
+
195
+ const environmentLightingPresets = Object.freeze({
196
+ "moonlit-harbor": Object.freeze({
197
+ preset: "moonlit-harbor",
198
+ environmentMode: 0,
199
+ environmentIntensity: 0.86,
200
+ exposure: 1,
201
+ horizonColor: freezeVec4([0.33, 0.43, 0.53, 1]),
202
+ zenithColor: freezeVec4([0.035, 0.07, 0.14, 1]),
203
+ sunDirection: Object.freeze(normalizeVector3([0.22, 0.88, 0.42], [0, 1, 0])),
204
+ sunColor: freezeVec4([2.1, 2.25, 2.65, 1]),
205
+ ambientColor: freezeVec4([0.018, 0.023, 0.03, 1]),
206
+ }),
207
+ "product-studio": Object.freeze({
208
+ preset: "product-studio",
209
+ environmentMode: 1,
210
+ environmentIntensity: 1.05,
211
+ exposure: 1,
212
+ horizonColor: freezeVec4([0.52, 0.61, 0.65, 1]),
213
+ zenithColor: freezeVec4([0.18, 0.22, 0.26, 1]),
214
+ sunDirection: Object.freeze(normalizeVector3([0.18, 0.93, 0.24], [0, 1, 0])),
215
+ sunColor: freezeVec4([3.8, 3.55, 2.85, 1]),
216
+ ambientColor: freezeVec4([0.024, 0.027, 0.03, 1]),
217
+ }),
218
+ "neutral-studio": Object.freeze({
219
+ preset: "neutral-studio",
220
+ environmentMode: 2,
221
+ environmentIntensity: 0.95,
222
+ exposure: 1,
223
+ horizonColor: freezeVec4([0.48, 0.53, 0.55, 1]),
224
+ zenithColor: freezeVec4([0.24, 0.26, 0.29, 1]),
225
+ sunDirection: Object.freeze(normalizeVector3([-0.24, 0.86, 0.36], [0, 1, 0])),
226
+ sunColor: freezeVec4([2.4, 2.35, 2.2, 1]),
227
+ ambientColor: freezeVec4([0.028, 0.029, 0.03, 1]),
228
+ }),
229
+ });
230
+
231
+ function resolveEnvironmentPreset(name) {
232
+ const presetName = typeof name === "string" && name.length > 0 ? name : "product-studio";
233
+ const preset = environmentLightingPresets[presetName];
234
+ if (!preset) {
235
+ throw new Error(
236
+ `Unknown lighting environment preset "${presetName}". Expected one of: ${lightingEnvironmentPresetNames.join(", ")}.`
237
+ );
238
+ }
239
+ return preset;
240
+ }
241
+
242
+ function estimateEnvironmentColor(config) {
243
+ const horizonWeight = 0.58;
244
+ const zenithWeight = 1 - horizonWeight;
245
+ const glowWeight = 0.055;
246
+ const intensity = Math.max(config.environmentIntensity, 0.0001);
247
+ return freezeVec4([
248
+ (config.horizonColor[0] * horizonWeight + config.zenithColor[0] * zenithWeight + config.sunColor[0] * glowWeight) * intensity,
249
+ (config.horizonColor[1] * horizonWeight + config.zenithColor[1] * zenithWeight + config.sunColor[1] * glowWeight) * intensity,
250
+ (config.horizonColor[2] * horizonWeight + config.zenithColor[2] * zenithWeight + config.sunColor[2] * glowWeight) * intensity,
251
+ 1,
252
+ ]);
253
+ }
254
+
255
+ export function createEnvironmentLightingConfig(options = {}) {
256
+ const preset = resolveEnvironmentPreset(options.preset ?? options.name);
257
+ const environmentIntensity = Math.max(
258
+ readFinite(options.environmentIntensity ?? options.intensity, preset.environmentIntensity),
259
+ 0.0001
260
+ );
261
+ const config = {
262
+ preset: preset.preset,
263
+ profile: typeof options.profile === "string" ? options.profile : defaultLightingProfile,
264
+ environmentMode: Math.max(0, Math.trunc(readFinite(options.environmentMode, preset.environmentMode))),
265
+ environmentIntensity,
266
+ exposure: Math.max(0.0001, readFinite(options.exposure, preset.exposure)),
267
+ horizonColor: readColor(options.horizonColor, preset.horizonColor),
268
+ zenithColor: readColor(options.zenithColor, preset.zenithColor),
269
+ sunDirection: Object.freeze(
270
+ normalizeVector3(options.sunDirection, preset.sunDirection)
271
+ ),
272
+ sunColor: readColor(options.sunColor, preset.sunColor),
273
+ ambientColor: readColor(options.ambientColor, preset.ambientColor),
274
+ };
275
+ const environmentColor = estimateEnvironmentColor(config);
276
+
277
+ return Object.freeze({
278
+ ...config,
279
+ environmentColor,
280
+ wavefront: Object.freeze({
281
+ environmentColor,
282
+ ambientColor: config.ambientColor,
283
+ environmentLighting: Object.freeze({
284
+ horizonColor: config.horizonColor,
285
+ zenithColor: config.zenithColor,
286
+ sunDirection: Object.freeze([...config.sunDirection]),
287
+ sunColor: config.sunColor,
288
+ intensity: config.environmentIntensity,
289
+ mode: config.environmentMode,
290
+ exposure: config.exposure,
291
+ }),
292
+ }),
293
+ });
294
+ }
295
+
296
+ export function createWavefrontEnvironmentLightingOptions(options = {}) {
297
+ const config = createEnvironmentLightingConfig(options);
298
+ return Object.freeze({
299
+ environmentColor: config.wavefront.environmentColor,
300
+ ambientColor: config.wavefront.ambientColor,
301
+ environmentLighting: config.wavefront.environmentLighting,
302
+ lightingEnvironment: config,
303
+ });
304
+ }
305
+
154
306
  const lightingImportanceLevels = Object.freeze([
155
307
  "low",
156
308
  "medium",