Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

SurfaceMaterial weathering by cause: damp at a wall's foot, faces toward the sun bleached (#261)

Evan (Yifeng) Wangeecee2efb600
diff --git a/packages/scene/src/materials.ts b/packages/scene/src/materials.ts
index 9f2de09..ee85d3c 100644
--- a/packages/scene/src/materials.ts
+++ b/packages/scene/src/materials.ts
@@ -796,8 +796,8 @@ const SURFACE = new MaterialFunction("std_surface", {
params: {
WorldScale: 0, NormalScale: 1, Wet: 0, Flags: 0,
PuddleScale: 1, PuddleAmount: 0, GrimeAmount: 0, GrimeScale: 1,
- WinTop: 1e4, WinSeed: 0,
- UV: [1, 1, 0, 0], Win0: [3.2, 2.4, 1.5, 1.6], Win1: [4, 0.45, 0.9, 40],
+ WinTop: 1e4, WinSeed: 0, FootHeight: 0, FootAmount: 0,
+ UV: [1, 1, 0, 0], Win0: [3.2, 2.4, 1.5, 1.6], Win1: [4, 0.45, 0.9, 40], Fade: [0, 0, 1, 0],
},
textures: ["Map", "RoughnessMap", "MetalnessMap", "NormalMap", "EmissiveMap"],
helpers: SURFACE_HELPERS,
@@ -857,6 +857,22 @@ const SURFACE = new MaterialFunction("std_surface", {
if (dot(pn, pn) > 1e-12 && dot(pn, ng) > 0.0) { n = normalize(pn); }
}
}
+ // Weathering by cause on walls (mesh UVs in metres, v up from the wall's foot): splash and rising damp at the foot,
+ // darker and browner up to a ragged tide line; and the faces toward the sun's side bleached.
+ // (the height: the geometry's v, up; i.uv is v down, 1 - v)
+ let up = 1.0 - i.uv.y;
+ if ($FootHeight > 0.0 && !world && abs(ng.y) < 0.5 && up > 0.0) {
+ let top = $FootHeight * (0.7 + 0.6 * std_fbm(vec2<f32>(i.uv.x * 0.9, 3.7)));
+ let k = (1.0 - smoothstep(0.15 * top, top, up)) * $FootAmount;
+ let tide = smoothstep(0.85 * top, top, up) * (1.0 - smoothstep(top, 1.08 * top, up)) * $FootAmount;
+ col = col * mix(vec3<f32>(1.0), vec3<f32>(0.64, 0.6, 0.54), k) + vec3<f32>(0.03) * tide;
+ rough = mix(rough, 1.0, 0.5 * k);
+ }
+ if ($Fade.w > 0.0 && abs(ng.y) < 0.5) {
+ let f = clamp(dot(ng, $Fade.xyz), 0.0, 1.0) * $Fade.w;
+ let l = dot(col, vec3<f32>(0.2126, 0.7152, 0.0722));
+ col = mix(col, vec3<f32>(l * 1.12 + 0.02), 0.45 * f);
+ }
if ($GrimeAmount > 0.0 && i.near) {
let g = std_grime(p, ng, $GrimeScale) * $GrimeAmount;
col *= 1.0 - 0.6 * g;
@@ -964,6 +980,15 @@ export interface SurfaceParameters {
grime?: { amount: number; scale: number };
/** Lit windows on the vertical faces (`facadeWindows({...})`). */
windows?: FacadeWindows;
+ /**
+ * Weathering by cause on vertical faces, not a tiled noise:
+ * - `foot`: splash and rising damp at a wall's foot, `height` m up to a ragged tide line, darker and browner by up to
+ * `amount` (0..1) at the bottom. Reads the height from the mesh UVs: metres, v up from the wall's foot (a wall
+ * drawn in its own frame); faces with v < 0 (a reveal's) and world-mapped surfaces are left as they are.
+ * - `fade`: faces toward `toward` (a unit horizontal direction: the sun's side, south in the northern hemisphere)
+ * bleached, lighter and greyer, by up to `amount` (0..1).
+ */
+ weathering?: { foot?: { height: number; amount: number }; fade?: { toward: [number, number, number]; amount: number } };
side?: "front" | "double";
}
@@ -998,6 +1023,8 @@ export class SurfaceMaterial extends LibraryMaterial<SurfaceParameters> {
PuddleScale: Math.max(p.puddles?.scale ?? 1, 0.05), PuddleAmount: p.puddles?.amount ?? 0,
GrimeAmount: p.grime?.amount ?? 0, GrimeScale: Math.max(p.grime?.scale ?? 1, 0.05),
WinTop: w?.top ?? 1e4, WinSeed: w?.seed ?? 0,
+ FootHeight: Math.max(p.weathering?.foot?.height ?? 0, 0), FootAmount: p.weathering?.foot?.amount ?? 0,
+ Fade: p.weathering?.fade ? ([...p.weathering.fade.toward, p.weathering.fade.amount] as V4) : [0, 0, 1, 0],
UV: [...(first?.repeat ?? [1, 1]), ...(first?.offset ?? [0, 0])] as V4,
Win0: w ? [w.floorHeight, w.bayWidth, w.windowWidth, w.windowHeight] : [0, 0, 0, 0],
Win1: w ? [w.groundHeight, w.litChance, w.sill, w.brightness] : [0, 0, 0, 0],
diff --git a/tests/render/surface-weathering.ts b/tests/render/surface-weathering.ts
new file mode 100644
index 0000000..4562376
--- /dev/null
+++ b/tests/render/surface-weathering.ts
@@ -0,0 +1,25 @@
+// Weathering by cause: a wall's foot darkened by splash and rising damp up to a tide line, read from mesh UVs in metres
+// (v up from the foot). 0.8 grey walls facing the camera, uniform 1-nit sky: the weathered wall over a clean one, in a
+// band near the foot and one above the tide line (no change there: the ratio pins both the darkening and its edge).
+import { Mesh, PlaneGeometry, SurfaceMaterial } from "@threert/scene";
+import { camera, result, sceneWithSky, type TestMeta } from "./lib.ts";
+
+export const test: TestMeta = {
+ id: "surface/weathering", features: ["material-library", "lambert"], reference: "converged", temporal: false,
+ metric: { kind: "ratio", a: [0.6, 0.72, 0.9, 0.79], b: [0.1, 0.72, 0.4, 0.79], expected: 0.748, tolerance: 0.02 },
+};
+
+export default function () {
+ const scene = sceneWithSky(1);
+ const wall = (x: number, weathered: boolean) => {
+ const g = new PlaneGeometry(1.8, 1.8);
+ // UVs in metres, v up from the wall's foot (as a wall drawn in its own frame has them)
+ const uv = g.uvs!;
+ for (let k = 0; k < uv.length; k += 2) { uv[k] *= 1.8; uv[k + 1] = (g.positions![(k / 2) * 3 + 1] + 0.9); }
+ const mat = new SurfaceMaterial({ color: [0.8, 0.8, 0.8], roughness: 1, weathering: weathered ? { foot: { height: 0.8, amount: 1 } } : undefined });
+ const m = new Mesh(g, mat);
+ m.position.set(x, 0, 0);
+ return m;
+ };
+ return result(scene, camera(scene, [0, 0, 4], [0, 0, 0]), [wall(-1, false), wall(1, true)]);
+}