roamwright 0.57.0 → 0.58.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "roamwright",
3
- "version": "0.57.0",
3
+ "version": "0.58.0",
4
4
  "description": "A procedural worldbuilding engine built on top of Three.js. Generate landscapes with rivers and changing weather, animate crowds, and explore on foot or in flight.",
5
5
  "keywords": [
6
6
  "threejs",
@@ -285,3 +285,33 @@ export const seaBody = (water = {}) => {
285
285
  export const seaOpacity = (water = {}) => `/* glsl */
286
286
  opacity = mix(0.06, 0.95, 1.0 - dot(exp(-${glslVec3({ ...SEA_WATER, ...water }.absorption)} * opticalPath), vec3(0.2, 0.5, 0.3)));
287
287
  opacity = mix(opacity, 1.0, smoothstep(6.0, 7.8, waterline));`;
288
+
289
+ const glslStretch = ([along, across]) =>
290
+ along === across ? `vec2(${glslFloat(along)})` : `vec2(${glslFloat(along)}, ${glslFloat(across)})`;
291
+ const glslAcross = across => (across < 0 ? `-pileRow.z * ${glslFloat(-across)}` : `pileRow.z * ${glslFloat(across)}`);
292
+
293
+ /**
294
+ * Generates the GLSL snippet for water ripples around rows of pier/jetty piles (`uPileRows`), returning an empty string
295
+ * when no pile rows are configured so materials without piles compile no loop.
296
+ * @param {object|null} options Pile-row shader options (`PileRowsOptions`).
297
+ * @returns {string}
298
+ */
299
+ export function pileRowsGlsl(options) {
300
+ if (!options) return '';
301
+ const { rows, along, halfWidth, firstPile, pitch, pairs, gap, pileRange, spots = [], reach = 160, strength = 0.19 } = options;
302
+ const spotLines = spots.map(
303
+ spot =>
304
+ ` pileRipples += ripple(q - vec2(${glslFloat(spot.along)}, ${glslAcross(spot.across)}), ${glslStretch(spot.stretch)}, ${glslFloat(spot.range)}, ${glslFloat(spot.phase)});\n`,
305
+ );
306
+ return ` if (uHasDepth > 0.5 && dist < ${glslFloat(reach)}) {
307
+ vec2 pileRipples = vec2(0.0);
308
+ for (int i = 0; i < ${rows}; i++) {
309
+ vec4 pileRow = uPileRows[i]; vec2 d = vec2(cos(pileRow.w - 10.0), sin(pileRow.w - 10.0)), r = vW.xz - pileRow.xy, q = vec2(dot(r, d), d.x * r.y - d.y * r.x);
310
+ if (pileRow.w < 0.5 || abs(q.y) > ${glslFloat(halfWidth)} || q.x < ${glslFloat(along[0])} || q.x > ${glslFloat(along[1])}) continue;
311
+ float pile = clamp(floor((q.x - ${glslFloat(firstPile)}) / ${glslFloat(pitch)} + 0.5), 0.0, ${glslFloat(pairs - 1)});
312
+ pileRipples += ripple(q - vec2(${glslFloat(firstPile)} + ${glslFloat(pitch)} * pile, sign(q.y) * ${glslFloat(gap)}), vec2(1.0), ${glslFloat(pileRange)}, pile);
313
+ ${spotLines.join('')} }
314
+ g += pileRipples * ${glslFloat(strength)} * OW(8.0);
315
+ }
316
+ `;
317
+ }
@@ -4,10 +4,10 @@
4
4
  */
5
5
 
6
6
  import * as THREE from 'three';
7
- import { FOAM_GLSL, SEA_WAVES, seaBody, seaOpacity } from './sea-shaders.js';
7
+ import { FOAM_GLSL, SEA_WAVES, pileRowsGlsl, seaBody, seaOpacity } from './sea-shaders.js';
8
8
  import { RAIN_GLSL, RAIN_PARS } from './rain-rings.js';
9
9
  import { STREAM_FLOW_GLSL } from './stream-flow.js';
10
- import { glslFloat, glslVec3 } from './glsl-literal.js';
10
+ import { glslVec3 } from './glsl-literal.js';
11
11
  import { DEEP, FAR } from '../geometry/stream.js';
12
12
 
13
13
  /** Default linear RGB colours and Beer-Lambert absorption for open water and streams. */
@@ -53,33 +53,6 @@ export const REFLECTION_SMEAR = 2 * 0.75 * REFLECTION_SMEAR_MOST;
53
53
  * @property {number} [strength] - How much the ripples bend the surface's normal (0.19).
54
54
  */
55
55
 
56
- const glslStretch = ([along, across]) =>
57
- along === across ? `vec2(${glslFloat(along)})` : `vec2(${glslFloat(along)}, ${glslFloat(across)})`;
58
- const glslAcross = across => (across < 0 ? `-pileRow.z * ${glslFloat(-across)}` : `pileRow.z * ${glslFloat(across)}`);
59
-
60
- // The ripples of the rows of piles, written from the options so that a material with none compiles no loop. A row's uniform is
61
- // (anchor x, anchor z, side, heading + 10); a w under 0.5 turns the row off. Rows start as a Vector4's defaults (0, 0, 0, 1):
62
- // on, at the origin, so a world places every row it asks for.
63
- function pileRowsGlsl(options) {
64
- if (!options) return '';
65
- const { rows, along, halfWidth, firstPile, pitch, pairs, gap, pileRange, spots = [], reach = 160, strength = 0.19 } = options;
66
- const spotLines = spots.map(
67
- spot =>
68
- ` pileRipples += ripple(q - vec2(${glslFloat(spot.along)}, ${glslAcross(spot.across)}), ${glslStretch(spot.stretch)}, ${glslFloat(spot.range)}, ${glslFloat(spot.phase)});\n`,
69
- );
70
- return ` if (uHasDepth > 0.5 && dist < ${glslFloat(reach)}) {
71
- vec2 pileRipples = vec2(0.0);
72
- for (int i = 0; i < ${rows}; i++) {
73
- vec4 pileRow = uPileRows[i]; vec2 d = vec2(cos(pileRow.w - 10.0), sin(pileRow.w - 10.0)), r = vW.xz - pileRow.xy, q = vec2(dot(r, d), d.x * r.y - d.y * r.x);
74
- if (pileRow.w < 0.5 || abs(q.y) > ${glslFloat(halfWidth)} || q.x < ${glslFloat(along[0])} || q.x > ${glslFloat(along[1])}) continue;
75
- float pile = clamp(floor((q.x - ${glslFloat(firstPile)}) / ${glslFloat(pitch)} + 0.5), 0.0, ${glslFloat(pairs - 1)});
76
- pileRipples += ripple(q - vec2(${glslFloat(firstPile)} + ${glslFloat(pitch)} * pile, sign(q.y) * ${glslFloat(gap)}), vec2(1.0), ${glslFloat(pileRange)}, pile);
77
- ${spotLines.join('')} }
78
- g += pileRipples * ${glslFloat(strength)} * OW(8.0);
79
- }
80
- `;
81
- }
82
-
83
56
  /**
84
57
  * Place row `index` of a water material's rows of piles (see {@link PileRowsOptions}): its seaward anchor (`x`, `z`, metres),
85
58
  * the `side` (+1 or -1) its extra ripple spots lie to, and its `heading` (radians) towards the shore.
@@ -381,7 +354,7 @@ ${pileRowsGlsl(pileRows)} #ifdef SEA
381
354
  });
382
355
  if (pileRows) waterMat.uniforms.uPileRows = { value: Array.from({ length: pileRows.rows }, () => new THREE.Vector4()) };
383
356
  Object.assign(waterMat.uniforms, wakeUniforms, cloudUniforms);
384
- const streamMat = waterMat.clone();
357
+ const streamMat = cloneWaterMaterial(waterMat);
385
358
  Object.assign(streamMat.uniforms, wakeUniforms, cloudUniforms);
386
359
  streamMat.uniforms.uHasDepth.value = 0;
387
360
  streamMat.uniforms.uRefl = { value: null };
@@ -392,6 +365,33 @@ ${pileRowsGlsl(pileRows)} #ifdef SEA
392
365
  return { waterMat, streamMat, waterMaterials };
393
366
  }
394
367
 
368
+ const isRenderTargetUniform = v => Boolean(v?.isRenderTargetTexture || (Array.isArray(v) && v.some(x => x?.isRenderTargetTexture)));
369
+
370
+ // Clone a water ShaderMaterial without feeding render-target textures (such as `uRefl`) into Three's
371
+ // `UniformsUtils.clone`, which warns and nulls them; skipped uniforms are shared by reference.
372
+ function cloneWaterMaterial(source, shareAll = false) {
373
+ const saved = source.uniforms,
374
+ shared = [];
375
+ if (shareAll) source.uniforms = {};
376
+ else {
377
+ const filtered = {};
378
+ for (const [key, entry] of Object.entries(saved)) {
379
+ if (isRenderTargetUniform(entry?.value)) shared.push([key, entry]);
380
+ else filtered[key] = entry;
381
+ }
382
+ if (shared.length) source.uniforms = filtered;
383
+ }
384
+ let variant;
385
+ try {
386
+ variant = source.clone();
387
+ } finally {
388
+ source.uniforms = saved;
389
+ }
390
+ if (shareAll) variant.uniforms = { ...saved };
391
+ else for (const [key, entry] of shared) variant.uniforms[key] = entry;
392
+ return variant;
393
+ }
394
+
395
395
  function trackWaterVariant(source, variant) {
396
396
  const list = source._waterMaterials;
397
397
  if (list) {
@@ -402,9 +402,8 @@ function trackWaterVariant(source, variant) {
402
402
 
403
403
  // Preserve the live lighting, clock and wake uniforms when specializing a water material.
404
404
  function waterVariant(material) {
405
- const variant = material.clone();
405
+ const variant = cloneWaterMaterial(material, true);
406
406
  Object.assign(variant.defaultAttributeValues, material.defaultAttributeValues);
407
- Object.assign(variant.uniforms, material.uniforms);
408
407
  trackWaterVariant(material, variant);
409
408
  return variant;
410
409
  }
@@ -467,7 +466,7 @@ const SHARED = [
467
466
  /** Clone open-water and stream materials for another land while sharing global lighting, wind and wake uniforms. */
468
467
  export function landWater(waterMat, depth, rect, level, sea = null) {
469
468
  const make = (hasDepth, own) => {
470
- const variant = waterMat.clone();
469
+ const variant = cloneWaterMaterial(waterMat);
471
470
  for (const uniformName of SHARED) {
472
471
  if (waterMat.uniforms[uniformName]) variant.uniforms[uniformName] = waterMat.uniforms[uniformName];
473
472
  }