roamwright 0.1.1 → 0.2.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/CONTRIBUTING.md +39 -8
- package/README.md +53 -24
- package/examples/viaduct-camera.js +40 -0
- package/examples/viaduct-landscape.js +81 -0
- package/examples/viaduct.css +75 -0
- package/examples/viaduct.html +47 -0
- package/examples/viaduct.js +332 -0
- package/geometry/creased-sheet.js +127 -0
- package/geometry/ear.js +97 -0
- package/geometry/erosion.js +106 -0
- package/geometry/face-grid.js +143 -0
- package/geometry/garment-tags.js +11 -0
- package/geometry/hair-gathers.js +174 -0
- package/geometry/hair-locks.js +156 -0
- package/geometry/hair-patch.js +42 -0
- package/geometry/hair-shell.js +186 -0
- package/geometry/hair-tags.js +6 -0
- package/geometry/head-profile.js +138 -0
- package/geometry/head-shape.js +499 -0
- package/geometry/heightfield.js +68 -0
- package/geometry/outline-grid.js +46 -0
- package/geometry/path-field.js +65 -0
- package/geometry/sagging-tube.js +58 -0
- package/geometry/sampled-surface.js +101 -0
- package/geometry/scatter-grid.js +32 -0
- package/geometry/sculpt.js +263 -0
- package/geometry/shore-grid.js +67 -0
- package/geometry/strata-field.js +339 -0
- package/geometry/strata-tiles.js +215 -0
- package/geometry/stream.js +94 -0
- package/geometry/terrain-chunks.js +300 -0
- package/geometry/terrain-extension.js +44 -0
- package/math/cell-edge.js +44 -0
- package/math/cloth-textures.js +83 -0
- package/math/cloud-textures.js +112 -0
- package/math/color.js +29 -0
- package/math/mountain-ranges.js +463 -0
- package/math/particle-noise.js +48 -0
- package/math/periodic.js +20 -0
- package/math/rock-texture.js +56 -0
- package/math/surface-textures.js +241 -0
- package/motion/arm-reach.js +74 -0
- package/motion/biped-stride.js +276 -0
- package/motion/crowd-alarms.js +127 -0
- package/motion/crowd-routine.js +183 -0
- package/motion/daily-clock.js +59 -0
- package/motion/instance-sight.js +84 -0
- package/motion/quadruped.js +332 -0
- package/motion/weather.js +59 -0
- package/motion/wind.js +27 -0
- package/package.json +6 -4
- package/rendering/caster-motion.js +79 -0
- package/rendering/cell-lod.js +175 -0
- package/rendering/cloud-dome.js +371 -0
- package/rendering/cloud-field.js +83 -0
- package/rendering/compilation-schedule.js +34 -0
- package/rendering/contact-shadows.js +79 -0
- package/rendering/crowd-skinning.js +133 -0
- package/rendering/crust.js +28 -0
- package/rendering/daylight.js +50 -0
- package/rendering/far-shadow.js +186 -0
- package/rendering/flocks.js +323 -0
- package/rendering/garment-material.js +110 -0
- package/rendering/glow-lines.js +74 -0
- package/rendering/glow-shell.js +90 -0
- package/rendering/hair-strands.js +67 -0
- package/rendering/height-haze.js +129 -0
- package/rendering/height-probe.js +91 -0
- package/rendering/interior-mapping.js +19 -0
- package/rendering/light-budget.js +222 -0
- package/rendering/light-points.js +51 -0
- package/rendering/light-shafts.js +131 -0
- package/rendering/local-lights.js +28 -0
- package/rendering/low-light.js +13 -0
- package/rendering/material-lod.js +110 -0
- package/rendering/particle-emitters.js +223 -0
- package/rendering/particles.js +297 -0
- package/rendering/planar-reflection.js +175 -0
- package/rendering/post-shaders.js +21 -1
- package/rendering/post.js +4 -0
- package/rendering/rain-rings.js +22 -0
- package/rendering/road-surface.js +63 -0
- package/rendering/sea-shaders.js +230 -0
- package/rendering/shader-warmup.js +197 -0
- package/rendering/shadow-alignment.js +28 -0
- package/rendering/shadow-depth.js +129 -0
- package/rendering/sky-occlusion.js +187 -0
- package/rendering/sky.js +169 -0
- package/rendering/stage-lights.js +209 -0
- package/rendering/static-point-shadow.js +119 -0
- package/rendering/strata-material.js +93 -0
- package/rendering/stream-flow.js +36 -0
- package/rendering/surface-light.js +22 -0
- package/rendering/surface-projection.js +69 -0
- package/rendering/talus.js +26 -0
- package/rendering/terrain-rock.js +48 -0
- package/rendering/texture-fills.js +68 -0
- package/rendering/viewport.js +9 -0
- package/rendering/wakes.js +99 -0
- package/rendering/water-material.js +288 -0
- package/runtime/device-quality.js +27 -0
- package/runtime/diagnostic-log.js +80 -0
- package/runtime/events.js +14 -0
- package/runtime/first-use.js +3 -1
- package/runtime/frame-yield.js +14 -0
- package/runtime/grid-cache.js +88 -0
- package/runtime/grid-rows.js +9 -0
- package/runtime/module-preload.js +15 -0
- package/runtime/search.js +69 -0
- package/runtime/spatial-audio.js +57 -0
- package/scripts/serve.py +26 -0
|
@@ -0,0 +1,499 @@
|
|
|
1
|
+
import { clamp, lerp, sstep } from '../math/scalar.js';
|
|
2
|
+
import { BACK, FRONT, MIDDLE, NOSE_OUT, NOSE_WIDE, SQUARE, WIDTH, sampled } from './head-profile.js';
|
|
3
|
+
|
|
4
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
5
|
+
// The shape of a head. It is a stack of level slices, each a rounded outline about a middle that moves forward toward the chin:
|
|
6
|
+
// a skull that closes into a dome above and a jaw that closes into the chin below. The features of a face (brow, sockets, lids,
|
|
7
|
+
// nose, cheeks, lips, chin) are pushed out of and into that form. Lengths in here are millimetres from the head's middle, which
|
|
8
|
+
// is the level of the eyes; what leaves this module is in metres.
|
|
9
|
+
// ════════════════════════════════════════════════════════════════════════
|
|
10
|
+
const MM = 0.001,
|
|
11
|
+
HALF = 118, // from the middle up to the crown, and down to the underside of the chin
|
|
12
|
+
CROWN = 45, // above this the slices close into the dome of the skull
|
|
13
|
+
JAW = -85, // below this (the angle of the jaw) they close into the chin
|
|
14
|
+
SEAM = -67, // where the lips meet
|
|
15
|
+
LID_TOP = 3.2, // the rows of the face's grid that are laid along the edges of the upper and the lower lid,
|
|
16
|
+
LID_LOW = -6, // and the rows above and below them where the grid lies level again
|
|
17
|
+
LID_ABOVE = 17,
|
|
18
|
+
LID_BELOW = -16,
|
|
19
|
+
FURROWS = [40, 49, 58], // the heights of the furrows of an aged brow
|
|
20
|
+
UNDER_STEPS = 3, // times a point is drawn in toward the skull to find what lies under it: its surface is no sphere
|
|
21
|
+
SCAR_WIDE = 1.7, // millimetres across an old scar's seam, unless the look says
|
|
22
|
+
SCAR_SHADE = 0.14, // how much a millimetre of a scar's seam darkens the skin in it (a crease holds shadow)
|
|
23
|
+
WOUND_DEEP = 4.5, // millimetres a fresh wound's torn flesh lies below the skin round it,
|
|
24
|
+
WOUND_SWELL = 2.2, // and how far the bruised skin at its edges swells;
|
|
25
|
+
HEALED_DEEP = 2.4, // once healed (H.healed), how far the scar lies sunk where the flesh was torn, and how high its edges and
|
|
26
|
+
HEALED_RIDGE = 0.9, // the cords of scar tissue across it stand
|
|
27
|
+
// Where a wound has torn the nose away, the opening into the skull's nose left in its place: a pear-shaped hollow down the
|
|
28
|
+
// middle of the face (from the top of its rows to its foot, millimetres up the face), its half width at the foot, how deep it
|
|
29
|
+
// sinks, and how dark it is within, fresh and healed.
|
|
30
|
+
NOSE_HOLE = { top: -19, foot: -46, wide: 9.5, deep: 6, dark: 0.78, healedDeep: 3.5, healedDark: 0.6 },
|
|
31
|
+
// Optional single brow eye: centre height, radius, opening, offset and brow ridge in millimetres.
|
|
32
|
+
ONE_EYE = { y: 16.5, r: 17.5, wide: 16.5, high: 10.5, out: 7, ridge: 7 };
|
|
33
|
+
|
|
34
|
+
// A soft rise of that width about a point, and one about a point of the face. Far from the point it is nothing, and is not
|
|
35
|
+
// worked out: a head asks for these some hundred thousand times.
|
|
36
|
+
const FAR = 3.2;
|
|
37
|
+
function bump(v, at, wide) {
|
|
38
|
+
const d = (v - at) / wide;
|
|
39
|
+
return d > FAR || d < -FAR ? 0 : Math.exp(-d * d);
|
|
40
|
+
}
|
|
41
|
+
function blob(x, y, ax, ay, wx, wy) {
|
|
42
|
+
const dx = (x - ax) / wx,
|
|
43
|
+
dy = (y - ay) / wy,
|
|
44
|
+
d = dx * dx + dy * dy;
|
|
45
|
+
return d > FAR * FAR ? 0 : Math.exp(-d);
|
|
46
|
+
}
|
|
47
|
+
// The larger of two heights, rounded where they meet.
|
|
48
|
+
function smoothMax(a, b, k) {
|
|
49
|
+
const h = clamp(0.5 + (a - b) / (2 * k), 0, 1);
|
|
50
|
+
return lerp(b, a, h) + k * h * (1 - h);
|
|
51
|
+
}
|
|
52
|
+
// How far a point lies from the line a-b, to which side of it (positive to the outside of the face), and how far along.
|
|
53
|
+
function fromLine(x, y, ax, ay, bx, by, out) {
|
|
54
|
+
const dx = bx - ax,
|
|
55
|
+
dy = by - ay,
|
|
56
|
+
len = Math.hypot(dx, dy),
|
|
57
|
+
along = ((x - ax) * dx + (y - ay) * dy) / (len * len);
|
|
58
|
+
out[0] = ((x - ax) * -dy + (y - ay) * dx) / len;
|
|
59
|
+
out[1] = along;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const _slice = { x: 0, z: 0 },
|
|
63
|
+
_line = [0, 0];
|
|
64
|
+
|
|
65
|
+
// The surface of a head of shape H, as a function of a direction from its middle: fn(dir, outPoint, channels). Channels: lips,
|
|
66
|
+
// shade, warmth (cheeks, nose), where a beard grows on cheeks and jaw, brow, where a moustache grows, the chin's tuft, a scar. A fine head has open lids and a mouth that can open (its grid lays rows
|
|
67
|
+
// along their edges); a plain one, for the far mesh and for coarse sculpts, has them closed and painted.
|
|
68
|
+
function headFn(features = {}, fine = false) {
|
|
69
|
+
const H = { jaw: 0.5, lips: 1, cheek: 1, mouthW: 1, brow: 1, socket: 1, hollow: 0, chin: 1, nose: 1, bridge: 0, ...features };
|
|
70
|
+
const age = H.age || 0,
|
|
71
|
+
fat = H.fat || 0,
|
|
72
|
+
dead = H.dead || 0,
|
|
73
|
+
jawWide = (H.jaw - 0.5) * 0.17,
|
|
74
|
+
chinWide = ((H.chinW ?? 1) - 1) * 0.5,
|
|
75
|
+
slope = H.slope ?? 1,
|
|
76
|
+
lips = H.lips,
|
|
77
|
+
noseLong = H.noseL ?? 1,
|
|
78
|
+
noseWide = H.noseW ?? 1;
|
|
79
|
+
const width = sampled(
|
|
80
|
+
y =>
|
|
81
|
+
WIDTH(y) *
|
|
82
|
+
(1 +
|
|
83
|
+
jawWide * bump(y, -84, 26) +
|
|
84
|
+
chinWide * sstep(-88, -108, y) +
|
|
85
|
+
(H.cheek - 1) * 0.05 * bump(y, -18, 16) +
|
|
86
|
+
fat * 0.1 * bump(y, -62, 36)),
|
|
87
|
+
-HALF,
|
|
88
|
+
HALF,
|
|
89
|
+
),
|
|
90
|
+
front = sampled(y => FRONT(y) * (1 - slope * 0.04 * sstep(26, 80, y)), -HALF, HALF),
|
|
91
|
+
back = sampled(y => BACK(y) + fat * 26 * bump(y, -104, 13), -HALF, HALF);
|
|
92
|
+
// how far the slices have closed toward the crown or the chin (the ends, where it falls steeply, are worked out each time)
|
|
93
|
+
const closingAt = y => {
|
|
94
|
+
if (y > CROWN) return Math.pow(1 - Math.pow(Math.min(1, (y - CROWN) / (HALF - CROWN)), 2.2), 1 / 2.2);
|
|
95
|
+
if (y < JAW) return Math.pow(1 - Math.pow(Math.min(1, (JAW - y) / (HALF + JAW)), 1.6), 1 / 1.6);
|
|
96
|
+
return 1;
|
|
97
|
+
},
|
|
98
|
+
closingSampled = sampled(closingAt, -HALF, HALF),
|
|
99
|
+
closing = y => (y > HALF - 6 || y < 6 - HALF ? closingAt(y) : closingSampled(y));
|
|
100
|
+
// the outline of the bare skull and jaw at a height, at an angle round from the front
|
|
101
|
+
// (a grid asks for one height many times running, so the slice last asked for is kept)
|
|
102
|
+
const kept = { y: NaN, square: 2, wide: 0, fore: 0, aft: 0, mid: 0 };
|
|
103
|
+
const slice = (y, angle, o) => {
|
|
104
|
+
if (y !== kept.y) {
|
|
105
|
+
const k = closing(y);
|
|
106
|
+
kept.y = y;
|
|
107
|
+
kept.square = SQUARE(y);
|
|
108
|
+
kept.wide = width(y) * k;
|
|
109
|
+
kept.fore = front(y) * k;
|
|
110
|
+
kept.aft = back(y) * k;
|
|
111
|
+
kept.mid = MIDDLE(y);
|
|
112
|
+
}
|
|
113
|
+
const n = kept.square,
|
|
114
|
+
s = Math.sin(angle),
|
|
115
|
+
c = Math.cos(angle),
|
|
116
|
+
q = Math.pow(Math.pow(Math.abs(s), n) + Math.pow(Math.abs(c), n), -1 / n);
|
|
117
|
+
o.x = kept.wide * s * q;
|
|
118
|
+
o.z = kept.mid + (c > 0 ? kept.fore : kept.aft) * c * q;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// the eyes: where each ball sits, and the edges of its lids from the inner corner (0) to the outer (1)
|
|
122
|
+
const eyeX = 31.5 * (H.eyeGap ?? 1),
|
|
123
|
+
eyeY = -1,
|
|
124
|
+
ballR = 12.2,
|
|
125
|
+
lidR = ballR + 1.3,
|
|
126
|
+
innerX = eyeX - 14.5,
|
|
127
|
+
outerX = eyeX + 14,
|
|
128
|
+
one = !!H.oneEye, // the two eyes healed over, and one in the middle of the brow
|
|
129
|
+
opening = one ? 0 : (H.eye ?? 1),
|
|
130
|
+
tilt = H.tilt ?? 0;
|
|
131
|
+
slice(eyeY, Math.asin(eyeX / width(eyeY)) * 0.93, _slice);
|
|
132
|
+
const eyeZ = _slice.z - 14.4 - (H.deep ?? 0);
|
|
133
|
+
slice(ONE_EYE.y, 0, _slice);
|
|
134
|
+
const oneZ = _slice.z + ONE_EYE.out - ONE_EYE.r, // where the middle of the one eye's ball is
|
|
135
|
+
oneLid = ONE_EYE.r + 1.3;
|
|
136
|
+
const corner = t => eyeY - 1.9 + (1.5 + 1.6 * tilt) * t,
|
|
137
|
+
upperLid = t => corner(t) + 5.4 * opening * Math.pow(4 * Math.pow(t, 0.85) * (1 - Math.pow(t, 0.85)), 0.72),
|
|
138
|
+
lowerLid = t => corner(t) - 3.9 * opening * Math.pow(4 * Math.pow(t, 1.15) * (1 - Math.pow(t, 1.15)), 0.8);
|
|
139
|
+
// a row of the grid is bent so that the two lid rows run along the lids' edges; away from the eye it lies level
|
|
140
|
+
const lidEdges = (ax, o) => {
|
|
141
|
+
const t = (ax - innerX) / (outerX - innerX);
|
|
142
|
+
if (t > 0 && t < 1) {
|
|
143
|
+
o[0] = upperLid(t);
|
|
144
|
+
o[1] = lowerLid(t);
|
|
145
|
+
} else {
|
|
146
|
+
const c = corner(clamp(t, 0, 1)),
|
|
147
|
+
e = sstep(0, 7, t < 0 ? innerX - ax : ax - outerX);
|
|
148
|
+
o[0] = c + (LID_TOP - c) * e;
|
|
149
|
+
o[1] = c + (LID_LOW - c) * e;
|
|
150
|
+
}
|
|
151
|
+
return t;
|
|
152
|
+
};
|
|
153
|
+
const bendRow = (row, up, low) =>
|
|
154
|
+
row >= LID_TOP
|
|
155
|
+
? up + ((row - LID_TOP) * (LID_ABOVE - up)) / (LID_ABOVE - LID_TOP)
|
|
156
|
+
: row >= LID_LOW
|
|
157
|
+
? low + ((row - LID_LOW) * (up - low)) / (LID_TOP - LID_LOW)
|
|
158
|
+
: low - ((LID_LOW - row) * (low - LID_BELOW)) / (LID_LOW - LID_BELOW);
|
|
159
|
+
|
|
160
|
+
// An old scar at a point of the face (millimetres; x to the person's left), as how far the skin is sunk there and how pale it
|
|
161
|
+
// is: H.scar is the seam's two ends, its width (millimetres) and how far its middle bows down from the straight, like a smile.
|
|
162
|
+
// A wider one is deeper and paler.
|
|
163
|
+
const _scar = [0, 0],
|
|
164
|
+
scarAt = (x, y) => {
|
|
165
|
+
const [ax0, ay0, bx, by, wide = SCAR_WIDE, bow = 0] = H.scar,
|
|
166
|
+
k = Math.sqrt(wide / SCAR_WIDE);
|
|
167
|
+
fromLine(x, y, ax0, ay0, bx, by, _line);
|
|
168
|
+
const t = _line[1],
|
|
169
|
+
d = _line[0] + bow * 4 * t * (1 - t), // the seam's own line, bowed
|
|
170
|
+
ends = sstep(-0.04, 0.05, t) * sstep(1.04, 0.95, t);
|
|
171
|
+
_scar[0] = k * ends * (0.75 * bump(d, 0, 0.45 * wide) - 0.22 * bump(Math.abs(d), 0.95 * wide, 0.4 * wide));
|
|
172
|
+
_scar[1] = k * ends * bump(d, 0, wide);
|
|
173
|
+
return _scar;
|
|
174
|
+
};
|
|
175
|
+
// A fresh wound (H.wound: as a scar, its two ends, width and bow): a broad band of torn flesh, sunk and ragged, the skin
|
|
176
|
+
// swollen along its edges; as how deep it lies at a point and how much of it there is (0 to 1). Healed, the band has closed
|
|
177
|
+
// into a scar: shallower, the swelling gone, its edges and cords of scar tissue across it standing proud.
|
|
178
|
+
const _wound = [0, 0],
|
|
179
|
+
woundAt = (x, y) => {
|
|
180
|
+
const [ax0, ay0, bx, by, wide, bow = 0] = H.wound;
|
|
181
|
+
fromLine(x, y, ax0, ay0, bx, by, _line);
|
|
182
|
+
const t = _line[1],
|
|
183
|
+
ragged = 0.22 * wide * (Math.sin(t * 23 + x * 0.31) * 0.6 + Math.sin(t * 41 - y * 0.27) * 0.4),
|
|
184
|
+
d = Math.abs(_line[0] + bow * 4 * t * (1 - t)) - ragged,
|
|
185
|
+
ends = sstep(-0.06, 0.08, t) * sstep(1.06, 0.92, t),
|
|
186
|
+
torn = ends * sstep(wide, wide * 0.7, d);
|
|
187
|
+
_wound[0] = H.healed
|
|
188
|
+
? torn * HEALED_DEEP * (0.75 + 0.25 * Math.sin(x * 0.9) * Math.sin(y * 1.1)) -
|
|
189
|
+
HEALED_RIDGE * (ends * bump(d, wide * 0.85, wide * 0.18) + torn * Math.max(0, Math.sin(t * 29 + x * 0.2)) ** 3)
|
|
190
|
+
: torn * WOUND_DEEP * (0.8 + 0.2 * Math.sin(x * 0.9) * Math.sin(y * 1.1)) - ends * WOUND_SWELL * bump(d, wide * 1.15, wide * 0.4);
|
|
191
|
+
_wound[1] = torn;
|
|
192
|
+
return _wound;
|
|
193
|
+
};
|
|
194
|
+
// an eye the wound crosses is burst and swollen shut
|
|
195
|
+
const woundShut = H.wound ? [woundAt(-eyeX, eyeY)[1], woundAt(eyeX, eyeY)[1]] : null;
|
|
196
|
+
// a wound across the middle of the nose takes all of it below the bone, tip and wings, not only the band it crosses
|
|
197
|
+
const noseLost = H.wound ? Math.max(woundAt(0, -12)[1], woundAt(0, -22)[1], woundAt(0, -32)[1]) : 0;
|
|
198
|
+
const mouthX = 24.5 * H.mouthW,
|
|
199
|
+
browLine = ax => 19 + 5 * Math.sin(Math.PI * clamp(ax / 56, 0, 1)) - 2.5 * sstep(40, 60, ax),
|
|
200
|
+
foldDeep = (0.55 + 1.5 * age + fat) * (H.fold ?? 1),
|
|
201
|
+
alaX = 11.8 * noseWide;
|
|
202
|
+
// the soft forms of the face without the nose, lids and lips: brow ridge, sockets, cheekbones, the muzzle of the mouth, chin
|
|
203
|
+
const soft = (ax, y) => {
|
|
204
|
+
const line = browLine(ax),
|
|
205
|
+
ridge = (1 + 2.8 * bump(ax, 17, 14)) * H.brow * sstep(64, 42, ax) * (y > line ? bump(y, line, 8) : bump(y, line, 4.2)),
|
|
206
|
+
socket = H.socket * (4 * blob(ax, y, 30, 5, 17, 11) + 2.6 * blob(ax, y, 17.5, 1, 7, 9));
|
|
207
|
+
return (
|
|
208
|
+
ridge -
|
|
209
|
+
socket +
|
|
210
|
+
2.6 * H.cheek * blob(ax, y, 46, -16, 14, 11) -
|
|
211
|
+
(0.5 + 1.2 * age) * blob(ax, y, 26, -15, 9, 5) -
|
|
212
|
+
(3 * H.hollow + 2 * age) * blob(ax, y, 43, -55, 13, 16) +
|
|
213
|
+
3.4 * blob(ax, y, 0, -63, 26, 19) +
|
|
214
|
+
3 * fat * blob(ax, y, 38, -52, 18, 22) +
|
|
215
|
+
2.2 * (age + fat) * blob(ax, y, 36, -91, 10, 9) -
|
|
216
|
+
3 * blob(ax, y, 0, -83.5, 17, 4.6) +
|
|
217
|
+
3.6 * H.chin * blob(ax, y, 0, -100, 15 * (H.chinW ?? 1), 10)
|
|
218
|
+
);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const point = (row, angle, o, ch) => {
|
|
222
|
+
slice(row, angle, _slice);
|
|
223
|
+
const side = _slice.x < 0 ? -1 : 1,
|
|
224
|
+
cosA = Math.cos(angle),
|
|
225
|
+
face = sstep(0.1, 0.5, cosA),
|
|
226
|
+
eyeRows = face > 0 && row < LID_ABOVE && row > LID_BELOW && !one; // (one eye's grid lies level)
|
|
227
|
+
let y = row,
|
|
228
|
+
lidT = -1,
|
|
229
|
+
lidUp = 0,
|
|
230
|
+
lidLow = 0;
|
|
231
|
+
if (eyeRows && fine) {
|
|
232
|
+
lidT = lidEdges(Math.abs(_slice.x), _line);
|
|
233
|
+
lidUp = _line[0];
|
|
234
|
+
lidLow = _line[1];
|
|
235
|
+
y = lerp(row, bendRow(row, lidUp, lidLow), face);
|
|
236
|
+
slice(y, angle, _slice);
|
|
237
|
+
}
|
|
238
|
+
const ax = Math.abs(_slice.x);
|
|
239
|
+
let out = 0, // how far the face stands forward of the bare skull here
|
|
240
|
+
lip = 0,
|
|
241
|
+
shade = 1,
|
|
242
|
+
warm = 0,
|
|
243
|
+
beard = 0, // where a beard grows: on the cheeks and jaw, on the upper lip, and on the chin
|
|
244
|
+
moustache = 0,
|
|
245
|
+
tuft = 0,
|
|
246
|
+
brow = 0,
|
|
247
|
+
scar = 0,
|
|
248
|
+
raw = 0;
|
|
249
|
+
if (face > 0) {
|
|
250
|
+
out = soft(ax, y);
|
|
251
|
+
shade -=
|
|
252
|
+
0.15 * clamp((H.socket * blob(ax, y, 30, 5, 17, 11)) / 1.2, 0, 1) +
|
|
253
|
+
0.1 * bump(y, browLine(ax) - 8, 4.5) * sstep(50, 36, ax) * sstep(6, 14, ax);
|
|
254
|
+
if (age > 0) {
|
|
255
|
+
// the lines of age: furrows across the brow (an aged head's grid has rows for them), the frown between the brows,
|
|
256
|
+
// and the creases that fan from the outer corner of each eye
|
|
257
|
+
const furrow =
|
|
258
|
+
(bump(y, FURROWS[0], 0.75) + bump(y, FURROWS[1], 0.75) + 0.8 * bump(y, FURROWS[2], 0.75)) *
|
|
259
|
+
sstep(48, 30, ax) *
|
|
260
|
+
(0.7 + 0.3 * Math.sin(ax * 0.21 + y)),
|
|
261
|
+
frown = bump(ax, 4.6, 1.5) * sstep(13, 17, y) * sstep(31, 25, y),
|
|
262
|
+
fan =
|
|
263
|
+
sstep(outerX + 1, outerX + 4, ax) *
|
|
264
|
+
sstep(outerX + 15, outerX + 9, ax) *
|
|
265
|
+
bump(y, eyeY, 7) *
|
|
266
|
+
(0.5 + 0.5 * Math.sin((y - eyeY) * 1.5 + (ax - outerX) * 0.5));
|
|
267
|
+
out -= age * (0.5 * furrow + 0.7 * frown + 0.3 * fan);
|
|
268
|
+
shade -= age * (0.17 * furrow + 0.16 * frown + 0.12 * fan);
|
|
269
|
+
}
|
|
270
|
+
if (H.scar) {
|
|
271
|
+
// an old wound the skin has closed over: a pale band with a sunken seam down its middle, puckered a little either side
|
|
272
|
+
const [deep, pale] = scarAt(side * ax, y);
|
|
273
|
+
scar = pale;
|
|
274
|
+
out -= deep;
|
|
275
|
+
shade -= SCAR_SHADE * Math.max(0, deep);
|
|
276
|
+
}
|
|
277
|
+
if (H.wound) {
|
|
278
|
+
const [deep, torn] = woundAt(side * ax, y);
|
|
279
|
+
raw = torn;
|
|
280
|
+
out -= deep;
|
|
281
|
+
shade -= (H.healed ? 0.12 : 0.3) * torn * (0.6 + 0.4 * Math.sin(ax * 0.7 + y * 0.9)); // the torn flesh holds shadow in its pits
|
|
282
|
+
}
|
|
283
|
+
// the lids over the ball of the eye, with the fold above the upper one and, with age, the bag under the lower
|
|
284
|
+
const ex = ax - eyeX,
|
|
285
|
+
ey = y - eyeY,
|
|
286
|
+
over = lidR * lidR - ex * ex - ey * ey;
|
|
287
|
+
if (over > 0 && !one) out = smoothMax(out, eyeZ + Math.sqrt(over) - _slice.z, 1.3);
|
|
288
|
+
if (one) {
|
|
289
|
+
// the one eye: a ridge jutting over it and sloping away to the temples, the lids wrapped round the ball, and within
|
|
290
|
+
// their opening the skin falls back behind the ball
|
|
291
|
+
const dy = y - ONE_EYE.y,
|
|
292
|
+
ridgeY = ONE_EYE.y + ONE_EYE.high + 5.5 - 0.006 * ax * ax,
|
|
293
|
+
ball = oneLid * oneLid - ax * ax - dy * dy,
|
|
294
|
+
r = Math.hypot(ax / ONE_EYE.wide, dy / ONE_EYE.high);
|
|
295
|
+
out += ONE_EYE.ridge * bump(y, ridgeY, y > ridgeY ? 7 : 3.6) * sstep(56, 36, ax);
|
|
296
|
+
if (ball > 0) out = smoothMax(out, oneZ + Math.sqrt(ball) - _slice.z, 1.6);
|
|
297
|
+
shade -= 0.22 * bump(r, 1, 0.14) + 0.12 * bump(y, ridgeY - 5, 3) * sstep(30, 10, ax);
|
|
298
|
+
if (r < 1) {
|
|
299
|
+
const within = (1 - r) * ONE_EYE.high; // millimetres inside the lids' edge
|
|
300
|
+
out -= fine ? 1.4 * sstep(0, 0.4, within) + 10 * sstep(0.4, 2.2, within) : 0;
|
|
301
|
+
shade = fine ? lerp(shade, 0.3, sstep(0, 0.6, within)) : shade * (1 - 0.8 * sstep(0, 1.2, within));
|
|
302
|
+
}
|
|
303
|
+
} else if (ax > innerX - 4 && ax < outerX + 6) {
|
|
304
|
+
const t = clamp((ax - innerX) / (outerX - innerX), 0, 1),
|
|
305
|
+
foldY = upperLid(t) + 3.4 - 1.2 * age,
|
|
306
|
+
span = Math.pow(4 * t * (1 - t), 0.5);
|
|
307
|
+
out +=
|
|
308
|
+
-0.75 * bump(y, foldY, 1.25) * span +
|
|
309
|
+
1.2 * age * bump(y, foldY + 3.4, 2.8) * span +
|
|
310
|
+
1.1 * age * blob(ax, y, eyeX, lowerLid(0.5) - 5.5, 10, 2.6);
|
|
311
|
+
shade -= 0.2 * bump(y, foldY, 1.1) * span + 0.1 * age * blob(ax, y, eyeX - 3, lowerLid(0.5) - 8.5, 10, 2);
|
|
312
|
+
}
|
|
313
|
+
if (one) {
|
|
314
|
+
// the sockets healed over: no line where the lids met
|
|
315
|
+
} else if (lidT > 0 && lidT < 1 && row <= LID_TOP && row >= LID_LOW && !(woundShut && woundShut[(side + 1) / 2] > 0.5)) {
|
|
316
|
+
// between the lids: the edge of each lid has a thickness, then the surface drops behind the ball of the eye
|
|
317
|
+
const within = Math.min(LID_TOP - row, row - LID_LOW),
|
|
318
|
+
gap = sstep(0.2, 2, lidUp - lidLow) * sstep(0.02, 0.16, lidT);
|
|
319
|
+
out -= gap * (1.5 * sstep(0, 0.35, within) + 9 * sstep(0.35, 1.6, within));
|
|
320
|
+
shade = row > (LID_TOP + LID_LOW) / 2 ? 0.34 : lerp(0.8, 0.62, sstep(0, 0.35, within));
|
|
321
|
+
warm = 1;
|
|
322
|
+
} else if (!fine && !dead) {
|
|
323
|
+
// a plain head's eyes are painted: the dark of lashes and iris between the lids
|
|
324
|
+
const t = (ax - innerX) / (outerX - innerX);
|
|
325
|
+
if (t > 0 && t < 1) shade *= 1 - 0.72 * sstep(0, 1.6, Math.min(upperLid(t) - y, y - lowerLid(t)) + 1.2);
|
|
326
|
+
}
|
|
327
|
+
// the nose: a ridge down the middle of the face to a rounded tip, wings beside it and the nostrils beneath
|
|
328
|
+
const noseY = 12 + (y - 12) / noseLong;
|
|
329
|
+
if (noseY < 17 && noseY > -52) {
|
|
330
|
+
const reach = NOSE_OUT(noseY) * (1 + (H.nose - 1) * sstep(8, -6, noseY)) + 2.2 * H.bridge * bump(noseY, -8, 11),
|
|
331
|
+
t = ax / (NOSE_WIDE(noseY) * (1 + (noseWide - 1) * sstep(-5, -35, noseY))),
|
|
332
|
+
tip = bump(noseY, -36, 7);
|
|
333
|
+
const torn = 1 - Math.max(0.85 * raw, noseLost * sstep(-6, -16, noseY)); // what a wound has torn away of the nose
|
|
334
|
+
if (t < 1) out += reach * lerp(1 - sstep(0.22, 1, t), Math.pow(1 - t * t, 0.75), 0.75 * tip) * torn;
|
|
335
|
+
const wr = ((ax - alaX) / (6.4 * noseWide)) ** 2 + ((noseY + 41.5) / 6.4) ** 2;
|
|
336
|
+
if (wr < 1) out = smoothMax(out, soft(ax, y) + 7.2 * H.nose * Math.pow(1 - wr, 0.7) * torn, 1.2);
|
|
337
|
+
const nostril = ((ax - 6.6 * noseWide) / (4.2 * noseWide)) ** 2 + ((noseY + 45.4) / 2.2) ** 2;
|
|
338
|
+
if (nostril < 1.6) {
|
|
339
|
+
out -= (fine ? 2.6 : 0.6) * sstep(1.6, 0.4, nostril);
|
|
340
|
+
shade *= 1 - (fine ? 0.86 : 0.3) * sstep(1.5, 0.5, nostril);
|
|
341
|
+
}
|
|
342
|
+
shade -= 0.2 * bump(ax, 0, 12) * bump(noseY, -49.6, 2.2) + 0.08 * bump(ax, alaX + 6 * noseWide, 1.6) * bump(noseY, -40, 5);
|
|
343
|
+
if (noseLost > 0) {
|
|
344
|
+
// the nose gone: the opening it covered, narrow under the bone and widening to its foot, sunk and dark within
|
|
345
|
+
const { top, foot, wide } = NOSE_HOLE,
|
|
346
|
+
half = wide * (0.35 + 0.65 * sstep(top, foot + 6, noseY)) * noseWide,
|
|
347
|
+
hole = noseLost * sstep(top + 2, top - 4, noseY) * sstep(foot - 2, foot + 3, noseY) * sstep(1, 0.55, ax / half);
|
|
348
|
+
out -= hole * (H.healed ? NOSE_HOLE.healedDeep : NOSE_HOLE.deep);
|
|
349
|
+
shade *= 1 - hole * (H.healed ? NOSE_HOLE.healedDark : NOSE_HOLE.dark);
|
|
350
|
+
raw = Math.max(raw, hole); // coloured as the wound round it is, raw or scarred
|
|
351
|
+
}
|
|
352
|
+
warm += 0.5 * tip * bump(ax, 0, 13);
|
|
353
|
+
}
|
|
354
|
+
// the crease from the wing of the nose past the corner of the mouth, with the cheek standing proud of it
|
|
355
|
+
fromLine(ax, y, 19.5 * noseWide, -40, mouthX + 6.5, -73, _line);
|
|
356
|
+
const along = sstep(-0.12, 0.1, _line[1]) * sstep(1.25, 0.9, _line[1]);
|
|
357
|
+
out += foldDeep * along * (0.7 * bump(_line[0], 6, 6) - bump(_line[0], 0, 2.3));
|
|
358
|
+
shade -= 0.17 * Math.min(1, foldDeep) * along * bump(_line[0], 0, 2);
|
|
359
|
+
// the lips: the groove under the nose, the upper lip with its bow, the seam, the fuller lower lip
|
|
360
|
+
const t = ax / mouthX;
|
|
361
|
+
if (t < 1.4 && y < -46 && y > -80) {
|
|
362
|
+
const taper = Math.sqrt(Math.max(0, 1 - t * t)),
|
|
363
|
+
upper = t < 1 ? 6.8 * lips * Math.pow(1 - Math.pow(t, 2.2), 0.8) * (1 - 0.16 * bump(ax, 0, 3.2)) : 0,
|
|
364
|
+
lower = t < 1 ? 9.2 * lips * Math.pow(1 - t * t, 0.75) : 0,
|
|
365
|
+
v = y > SEAM ? (upper > 0.01 ? (y - SEAM) / upper : 9) : lower > 0.01 ? (SEAM - y) / lower : 9;
|
|
366
|
+
if (v < 1) {
|
|
367
|
+
lip = sstep(1, 0.86, v) * sstep(0, 0.9, taper);
|
|
368
|
+
out += (y > SEAM ? 2 : 3) * lips * Math.pow(Math.sin(Math.PI * Math.min(1, v * 0.85 + 0.15)), 0.7) * taper;
|
|
369
|
+
}
|
|
370
|
+
out += 0.5 * lips * bump(v, 1, 0.14) * taper * (y > SEAM ? 1 : 0.5);
|
|
371
|
+
const groove = sstep(-47.5, -50.5, y) * sstep(SEAM + upper - 0.4, SEAM + upper + 2.2, y);
|
|
372
|
+
out += groove * (0.6 * bump(ax, 5.2, 1.9) - 1.3 * bump(ax, 0, 3.5));
|
|
373
|
+
out -= 1.6 * blob(ax, y, mouthX + 2.5, SEAM, 4.5, 5);
|
|
374
|
+
shade -= 0.1 * blob(ax, y, mouthX + 1, SEAM, 3, 2.5) + 0.1 * bump(ax, 0, 14) * bump(y, SEAM - lower - 3.5, 2.6);
|
|
375
|
+
const off = Math.abs(row - SEAM);
|
|
376
|
+
if (fine && t < 1) {
|
|
377
|
+
// a pocket behind the seam: the lips part from it when the jaw drops
|
|
378
|
+
if (off < 0.05) {
|
|
379
|
+
out -= 11 * taper;
|
|
380
|
+
shade = 0.06;
|
|
381
|
+
} else if (off < 0.5) {
|
|
382
|
+
out -= 1.3 * (1 - t ** 4);
|
|
383
|
+
shade *= 0.5;
|
|
384
|
+
}
|
|
385
|
+
} else if (t < 1.03) {
|
|
386
|
+
out -= 1.5 * bump(y, SEAM, 1.1) * sstep(1.03, 0.9, t);
|
|
387
|
+
shade *= 1 - 0.55 * bump(y, SEAM, 0.9) * sstep(1.03, 0.9, t);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
warm += 0.6 * blob(ax, y, 40, -30, 17, 16);
|
|
391
|
+
const cheekLine = -12 - 1.25 * (66 - ax); // a beard's upper edge, from before the ear down to the corner of the mouth
|
|
392
|
+
beard = sstep(cheekLine + 3, cheekLine - 5, y) * sstep(78, 70, ax) * (1 - lip);
|
|
393
|
+
// a moustache's upper edge follows the nostrils and falls toward the corners of the mouth; the beard under the lower lip
|
|
394
|
+
// leaves the hollow below it bare and rises to meet the moustache at the corners
|
|
395
|
+
const mTop = -50 - 0.014 * ax * ax,
|
|
396
|
+
tTop = -80 + 10 * (ax / (mouthX + 6)) ** 2;
|
|
397
|
+
moustache = sstep(mTop + 1.5, mTop - 1.5, y) * sstep(SEAM - 1, SEAM + 1, y) * sstep(mouthX + 9, mouthX + 3, ax) * (1 - lip);
|
|
398
|
+
tuft = sstep(tTop + 2, tTop - 3, y) * sstep(mouthX + 6, mouthX - 6, ax) * (1 - lip);
|
|
399
|
+
brow = one ? 0 : bump(y, browLine(ax) - 1.5, 2.7) * sstep(7.5, 11, ax) * sstep(53, 46, ax) * (1 - Math.min(1, scar + raw)); // no hair grows on a scar
|
|
400
|
+
out *= face;
|
|
401
|
+
}
|
|
402
|
+
if (dead && face > 0) {
|
|
403
|
+
// a dried face: pits for the eyes, an open cavity for the nose, the teeth bared
|
|
404
|
+
const pit = blob(ax, y, eyeX, eyeY + 1, 12.5, 10.5),
|
|
405
|
+
cavity = blob(ax, y, 0, -36, 7.5, 10) * sstep(-50, -44, y),
|
|
406
|
+
teeth = sstep(mouthX + 2, mouthX - 2, ax) * sstep(SEAM - 7.5, SEAM - 6, y) * sstep(SEAM + 7, SEAM + 5.5, y);
|
|
407
|
+
out -= (7 * sstep(0.35, 0.8, pit) + 5 * sstep(0.4, 0.75, cavity)) * face;
|
|
408
|
+
shade = (1 - 0.9 * sstep(0.35, 0.85, pit)) * (1 - 0.88 * sstep(0.4, 0.75, cavity)) * (0.94 + 0.06 * Math.sin(ax * 0.9 + y * 0.7));
|
|
409
|
+
shade *=
|
|
410
|
+
teeth > 0.5
|
|
411
|
+
? (0.55 + 0.45 * Math.abs(Math.sin(ax * 0.85))) * (1 - 0.6 * bump(y, SEAM, 0.8))
|
|
412
|
+
: 1 - 0.25 * blob(ax, y, 0, SEAM, 36, 11);
|
|
413
|
+
lip = teeth;
|
|
414
|
+
warm = beard = moustache = tuft = brow = scar = raw = 0;
|
|
415
|
+
}
|
|
416
|
+
// the sides of the head: the hollow of the temple, the arch of the cheekbone, the muscle of the jaw
|
|
417
|
+
const turn = Math.abs(angle),
|
|
418
|
+
flank =
|
|
419
|
+
1 +
|
|
420
|
+
(-2.8 * (1 + 0.8 * age) * bump(y, 22, 17) * bump(turn, 1.05, 0.33) +
|
|
421
|
+
1.6 * H.cheek * bump(y, -14, 8) * bump(turn, 1.2, 0.35) +
|
|
422
|
+
(0.6 + 1.2 * H.jaw) * bump(y, -62, 18) * bump(turn, 1.25, 0.3)) /
|
|
423
|
+
75;
|
|
424
|
+
if (y < -84) {
|
|
425
|
+
shade -= 0.2 * sstep(0.25, -0.35, cosA) * sstep(-84, -98, y); // under the jaw
|
|
426
|
+
if (!dead) beard = Math.max(beard, sstep(-84, -93, y));
|
|
427
|
+
}
|
|
428
|
+
const mid = MIDDLE(y);
|
|
429
|
+
o.x = side * ax * flank * MM;
|
|
430
|
+
o.y = y * MM;
|
|
431
|
+
o.z = (mid + (_slice.z - mid) * flank + out) * MM;
|
|
432
|
+
if (ch) {
|
|
433
|
+
ch[0] = lip;
|
|
434
|
+
ch[1] = Math.max(0.04, shade);
|
|
435
|
+
ch[2] = Math.min(1, warm);
|
|
436
|
+
ch[3] = beard;
|
|
437
|
+
ch[4] = brow;
|
|
438
|
+
ch[5] = moustache;
|
|
439
|
+
ch[6] = tuft;
|
|
440
|
+
ch[7] = scar || raw; // a head has an old scar or a fresh wound: the look says which (head.js colours it)
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
|
|
444
|
+
const fn = (d, o, ch) => point(d.y * HALF, Math.atan2(d.x, d.z), o, ch);
|
|
445
|
+
fn.point = point; // by a row of the grid (millimetres up from the middle) and an angle round from the front
|
|
446
|
+
// the bare skull at a direction, for what sits over it (hair, hoods, helmets)
|
|
447
|
+
fn.skull = (d, o) => {
|
|
448
|
+
const y = d.y * HALF;
|
|
449
|
+
slice(y, Math.atan2(d.x, d.z), _slice);
|
|
450
|
+
o.set(_slice.x * MM, y * MM, _slice.z * MM);
|
|
451
|
+
};
|
|
452
|
+
// how far out from the bare skull a point lies (metres from the head's middle): 1 on its surface, less within
|
|
453
|
+
fn.reach = (px, py, pz) => {
|
|
454
|
+
const up = py / MM,
|
|
455
|
+
y = clamp(up, -HALF, HALF),
|
|
456
|
+
n = SQUARE(y),
|
|
457
|
+
deep = pz / MM - MIDDLE(y),
|
|
458
|
+
across = Math.pow(Math.pow(Math.abs(px / MM) / width(y), n) + Math.pow(Math.abs(deep) / (deep > 0 ? front(y) : back(y)), n), 1 / n);
|
|
459
|
+
if (up > CROWN) return Math.pow(Math.pow(across, 2.2) + Math.pow((up - CROWN) / (HALF - CROWN), 2.2), 1 / 2.2);
|
|
460
|
+
if (up < JAW) return Math.pow(Math.pow(across, 1.6) + Math.pow((JAW - up) / (HALF + JAW), 1.6), 1 / 1.6);
|
|
461
|
+
return across;
|
|
462
|
+
};
|
|
463
|
+
// the direction at which the bare skull lies under a point (metres from the head's middle), on the line from the middle
|
|
464
|
+
// through it: what fn.skull turns into a point again. Hair that lies over the skull finds its place by it.
|
|
465
|
+
fn.under = (px, py, pz, d) => {
|
|
466
|
+
for (let i = 0; i < UNDER_STEPS; i++) {
|
|
467
|
+
const k = fn.reach(px, py, pz);
|
|
468
|
+
px /= k;
|
|
469
|
+
py /= k;
|
|
470
|
+
pz /= k;
|
|
471
|
+
}
|
|
472
|
+
const y = clamp(py / MM, -HALF, HALF),
|
|
473
|
+
deep = pz / MM - MIDDLE(y),
|
|
474
|
+
angle = Math.atan2(px / MM / width(y), deep / (deep > 0 ? front(y) : back(y))),
|
|
475
|
+
round = Math.sqrt(Math.max(0, 1 - (y / HALF) * (y / HALF)));
|
|
476
|
+
return d.set(Math.sin(angle) * round, y / HALF, Math.cos(angle) * round);
|
|
477
|
+
};
|
|
478
|
+
const upAt = t => Math.asin(clamp((upperLid(t) - eyeY) / lidR, -0.95, 0.95)),
|
|
479
|
+
lowAt = t => Math.asin(clamp((lowerLid(t) - eyeY) / lidR, -0.95, 0.95));
|
|
480
|
+
fn.eye = {
|
|
481
|
+
x: eyeX * MM,
|
|
482
|
+
y: eyeY * MM,
|
|
483
|
+
z: eyeZ * MM,
|
|
484
|
+
r: ballR * MM,
|
|
485
|
+
inner: innerX * MM,
|
|
486
|
+
outer: outerX * MM,
|
|
487
|
+
close: upAt(0.45) - lowAt(0.45) + 0.06, // the angle the upper lid turns through to shut
|
|
488
|
+
shut: t =>
|
|
489
|
+
t > 0 && t < 1 ? clamp((upAt(t) - lowAt(t) + 0.06 * Math.sqrt(Math.sin(Math.PI * t))) / (upAt(0.45) - lowAt(0.45) + 0.06), 0, 1) : 0, // the share of it at each point along the lid
|
|
490
|
+
upper: t => upperLid(t) * MM,
|
|
491
|
+
};
|
|
492
|
+
fn.mouth = { x: mouthX * MM, y: SEAM * MM };
|
|
493
|
+
fn.one = one ? { y: ONE_EYE.y * MM, z: oneZ * MM, r: ONE_EYE.r * MM, wide: ONE_EYE.wide * MM } : null; // the one eye's ball, if so
|
|
494
|
+
fn.browLine = x => browLine(x / MM) * MM;
|
|
495
|
+
fn.scar = (x, y) => (H.scar ? scarAt(x / MM, y / MM)[1] : 0); // how much of a scar there is at a point (metres)
|
|
496
|
+
return fn;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
export { FURROWS, LID_ABOVE, LID_BELOW, LID_LOW, LID_TOP, SEAM, headFn };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Sample the triangles of a regular, axis-aligned X/Z PlaneGeometry in geometry coordinates.
|
|
2
|
+
// The first vertex is the minimum X and Z; the index follows Three.js PlaneGeometry's diagonal.
|
|
3
|
+
export function heightfieldSampler(geometry, outside) {
|
|
4
|
+
const { widthSegments: columns, heightSegments: rows } = geometry.parameters;
|
|
5
|
+
const positions = geometry.getAttribute('position');
|
|
6
|
+
const x0 = positions.getX(0);
|
|
7
|
+
const z0 = positions.getZ(0);
|
|
8
|
+
const width = positions.getX(columns) - x0;
|
|
9
|
+
const depth = positions.getZ(rows * (columns + 1)) - z0;
|
|
10
|
+
if (!(width > 0 && depth > 0)) throw new RangeError('Expected a regular heightfield in the X/Z plane');
|
|
11
|
+
return (x, z) => {
|
|
12
|
+
const fx = (x - x0) / width * columns;
|
|
13
|
+
const fz = (z - z0) / depth * rows;
|
|
14
|
+
if (fx < 0 || fz < 0 || fx > columns || fz > rows) return outside?.(x, z);
|
|
15
|
+
const col = Math.min(Math.floor(fx), columns - 1);
|
|
16
|
+
const row = Math.min(Math.floor(fz), rows - 1);
|
|
17
|
+
const u = fx - col, v = fz - row;
|
|
18
|
+
const k = row * (columns + 1) + col;
|
|
19
|
+
const b = positions.getY(k + columns + 1);
|
|
20
|
+
const d = positions.getY(k + 1);
|
|
21
|
+
if (u + v <= 1) {
|
|
22
|
+
const a = positions.getY(k);
|
|
23
|
+
return a + (d - a) * u + (b - a) * v;
|
|
24
|
+
}
|
|
25
|
+
const c = positions.getY(k + columns + 2);
|
|
26
|
+
return c + (b - c) * (1 - u) + (d - c) * (1 - v);
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Replace a square of regular cells by a finer patch. The caller builds its mesh, materials and
|
|
31
|
+
// attributes with the supplied height function and returns its geometry. Call during construction.
|
|
32
|
+
export function refineHeightfield(base, { x, z, cells, subdivisions, height, createPatch }) {
|
|
33
|
+
const { widthSegments: columns, heightSegments: rows } = base.parameters;
|
|
34
|
+
if (!(Number.isInteger(cells) && cells > 0 && cells <= Math.min(columns, rows) &&
|
|
35
|
+
Number.isInteger(subdivisions) && subdivisions >= 2 && Number.isFinite(x) && Number.isFinite(z))) {
|
|
36
|
+
throw new RangeError('Invalid heightfield patch dimensions or position');
|
|
37
|
+
}
|
|
38
|
+
if (!base.index || base.index.count !== rows * columns * 6) throw new TypeError('Expected an indexed PlaneGeometry');
|
|
39
|
+
const positions = base.getAttribute('position');
|
|
40
|
+
const x0 = positions.getX(0), z0 = positions.getZ(0);
|
|
41
|
+
const dx = (positions.getX(columns) - x0) / columns;
|
|
42
|
+
const dz = (positions.getZ(rows * (columns + 1)) - z0) / rows;
|
|
43
|
+
const col = Math.round((x - x0) / dx - cells / 2);
|
|
44
|
+
const row = Math.round((z - z0) / dz - cells / 2);
|
|
45
|
+
if (col < 0 || row < 0 || col + cells > columns || row + cells > rows) {
|
|
46
|
+
throw new RangeError('Heightfield patch extends past the base grid');
|
|
47
|
+
}
|
|
48
|
+
const coarse = heightfieldSampler(base, height);
|
|
49
|
+
const cx = x0 + (col + cells / 2) * dx, cz = z0 + (row + cells / 2) * dz;
|
|
50
|
+
const width = cells * dx, depth = cells * dz;
|
|
51
|
+
const patch = createPatch({
|
|
52
|
+
x: cx, z: cz, width, depth, segments: cells * subdivisions,
|
|
53
|
+
height(px, pz) {
|
|
54
|
+
const weight = Math.min((width / 2 - Math.abs(px - cx)) / dx, (depth / 2 - Math.abs(pz - cz)) / dz);
|
|
55
|
+
if (weight >= 1) return height(px, pz);
|
|
56
|
+
const edge = coarse(px, pz);
|
|
57
|
+
return weight <= 0 ? edge : edge + (height(px, pz) - edge) * weight;
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
const sample = heightfieldSampler(patch, coarse);
|
|
61
|
+
// Keep the base vertex and index layout for other disjoint patches and external attributes.
|
|
62
|
+
for (let r = row; r < row + cells; r++) {
|
|
63
|
+
const start = (r * columns + col) * 6;
|
|
64
|
+
base.index.array.fill(0, start, start + cells * 6);
|
|
65
|
+
}
|
|
66
|
+
base.index.needsUpdate = true;
|
|
67
|
+
return sample;
|
|
68
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { outlineDistance, outlineEdges } from './shore-grid.js';
|
|
2
|
+
|
|
3
|
+
// A closed outline's signed distance (metres, negative inside) baked on a grid over its box, `pad` metres wider than the outline:
|
|
4
|
+
// plain numbers and an array, which can be handed to a worker of the pool.
|
|
5
|
+
function polyGrid(poly, cell, pad) {
|
|
6
|
+
if (!poly.length || !Number.isFinite(cell) || cell <= 0 || !Number.isFinite(pad) || pad < 0) throw new RangeError('Provide an outline, positive cell size and nonnegative padding');
|
|
7
|
+
let x0 = Infinity,
|
|
8
|
+
z0 = Infinity,
|
|
9
|
+
x1 = -Infinity,
|
|
10
|
+
z1 = -Infinity;
|
|
11
|
+
for (const [x, z] of poly) {
|
|
12
|
+
x0 = Math.min(x0, x);
|
|
13
|
+
x1 = Math.max(x1, x);
|
|
14
|
+
z0 = Math.min(z0, z);
|
|
15
|
+
z1 = Math.max(z1, z);
|
|
16
|
+
}
|
|
17
|
+
x0 -= pad;
|
|
18
|
+
z0 -= pad;
|
|
19
|
+
x1 += pad;
|
|
20
|
+
z1 += pad;
|
|
21
|
+
const nx = Math.max(2, Math.ceil((x1 - x0) / cell) + 1),
|
|
22
|
+
nz = Math.max(2, Math.ceil((z1 - z0) / cell) + 1),
|
|
23
|
+
g = new Float32Array(nx * nz),
|
|
24
|
+
edges = outlineEdges(poly);
|
|
25
|
+
for (let j = 0; j < nz; j++)
|
|
26
|
+
for (let i = 0; i < nx; i++) g[j * nx + i] = -outlineDistance(edges, x0 + i * cell, z0 + j * cell);
|
|
27
|
+
return { x0, z0, x1, z1, nx, nz, cell, pad, g };
|
|
28
|
+
}
|
|
29
|
+
// The signed distance anywhere, read from a grid of polyGrid's; beyond the box, the distance to the box.
|
|
30
|
+
function gridField({ x0, z0, x1, z1, nx, nz, cell, pad, g }) {
|
|
31
|
+
return (x, z) => {
|
|
32
|
+
const fx = (x - x0) / cell,
|
|
33
|
+
fz = (z - z0) / cell;
|
|
34
|
+
if (fx < 0 || fz < 0 || fx > nx - 1 || fz > nz - 1) return pad + Math.hypot(Math.max(x0 - x, 0, x - x1), Math.max(z0 - z, 0, z - z1));
|
|
35
|
+
const i = Math.min(nx - 2, fx | 0),
|
|
36
|
+
j = Math.min(nz - 2, fz | 0),
|
|
37
|
+
u = fx - i,
|
|
38
|
+
v = fz - j,
|
|
39
|
+
k = j * nx + i;
|
|
40
|
+
return (g[k] * (1 - u) + g[k + 1] * u) * (1 - v) + (g[k + nx] * (1 - u) + g[k + nx + 1] * u) * v;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
// A closed outline as a signed distance, baked once on a grid over its box (polyGrid).
|
|
44
|
+
const polyField = (poly, cell, pad) => gridField(polyGrid(poly, cell, pad));
|
|
45
|
+
|
|
46
|
+
export { gridField, polyField, polyGrid };
|