@woosh/meep-engine 3.78.1 → 3.78.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.
Files changed (25) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/package.json +1 -1
  3. package/src/generation/grid/generation/discrete/GridTaskWaveFunctionCollapse.d.ts +119 -0
  4. package/src/generation/grid/generation/discrete/GridTaskWaveFunctionCollapse.d.ts.map +1 -0
  5. package/src/generation/grid/generation/discrete/GridTaskWaveFunctionCollapse.js +563 -0
  6. package/src/generation/wfc/GRID_WFC_PIECE_SOCKET_PREFIX.d.ts +11 -0
  7. package/src/generation/wfc/GRID_WFC_PIECE_SOCKET_PREFIX.d.ts.map +1 -0
  8. package/src/generation/wfc/GRID_WFC_PIECE_SOCKET_PREFIX.js +10 -0
  9. package/src/generation/wfc/GridWFCCompiledTileSet.d.ts +123 -0
  10. package/src/generation/wfc/GridWFCCompiledTileSet.d.ts.map +1 -0
  11. package/src/generation/wfc/GridWFCCompiledTileSet.js +355 -0
  12. package/src/generation/wfc/GridWFCEdge.d.ts +13 -0
  13. package/src/generation/wfc/GridWFCEdge.d.ts.map +1 -0
  14. package/src/generation/wfc/GridWFCEdge.js +14 -0
  15. package/src/generation/wfc/GridWFCTileSet.d.ts +120 -0
  16. package/src/generation/wfc/GridWFCTileSet.d.ts.map +1 -0
  17. package/src/generation/wfc/GridWFCTileSet.js +275 -0
  18. package/src/shade/renderer/hair/GPUHairSystem.d.ts.map +1 -1
  19. package/src/shade/renderer/hair/GPUHairSystem.js +28 -9
  20. package/src/shade/renderer/hair/HAIR_SIMULATION.d.ts +50 -9
  21. package/src/shade/renderer/hair/HAIR_SIMULATION.d.ts.map +1 -1
  22. package/src/shade/renderer/hair/HAIR_SIMULATION.js +71 -11
  23. package/src/shade/renderer/hair/shader_hair_simulate.d.ts +22 -16
  24. package/src/shade/renderer/hair/shader_hair_simulate.d.ts.map +1 -1
  25. package/src/shade/renderer/hair/shader_hair_simulate.js +151 -78
@@ -11,23 +11,25 @@ export { HAIR_EPOCH_MASK } from "./chunk_hair_guide.js";
11
11
  export const shader_hair_simulate_begin: ComputeShader;
12
12
  export const HAIR_COLLIDER_POSE_SETTINGS: WebGPUStruct;
13
13
  /**
14
- * Writes each collider of the frame's list the way from the world into its own frame (`HAIR_COLLIDER_WORDS`), a
15
- * thread a collider, from its entity's world matrix as the scene holds it: a rigid motion and a uniform scale, whose
16
- * largest axis is taken.
14
+ * Writes each collider of the frame's list the way from the world into its own frame (`HAIR_COLLIDER_WORDS`), now
15
+ * and a frame ago, a thread a collider, from its entity's world matrices as the scene holds them: a rigid motion and
16
+ * a uniform scale, whose largest axis is taken.
17
17
  *
18
18
  * @type {ComputeShader}
19
19
  */
20
20
  export const shader_hair_collider_poses: ComputeShader;
21
21
  export const HAIR_SIMULATE_SETTINGS: WebGPUStruct;
22
22
  /**
23
- * Steps every simulated instance's guides through the frame, a thread a guide (`HAIR_SIMULATION.js`), the threads
24
- * of a row found from the inclusive sums of the rows' guide counts.
23
+ * Steps every simulated instance's guides through the frame's steps and writes what is drawn of them, a thread a
24
+ * guide (`HAIR_SIMULATION.js`), the threads of a row found from the inclusive sums of the rows' guide counts.
25
25
  *
26
- * A guide's root rides the entity, or its strand's root frame where the instance is bound to a mesh, the frame taken
27
- * linearly from a frame ago to now across the substeps; the first
28
- * segment's rest direction is held, as every other's, by the local shape from the entity's frame. A frame of no time
29
- * carries the guides rigidly from the entity's frame a frame ago to now, as its own geometry is carried. Each substep,
30
- * from the node after the root to the tip, a node:
26
+ * Every step is `settings.step` long; a frame takes `settings.steps` of them, none when its time has not reached the
27
+ * next (`hair_frame_steps`). A step reads the world at its own time within the frame: a guide's root rides the
28
+ * entity, or its strand's root frame where the instance is bound to a mesh, the frame taken linearly from a frame
29
+ * ago to now at the step's share of the frame's motion, and the colliders are where they were at that share. The
30
+ * first segment's rest direction is held, as every other's, by the local shape from the entity's frame. A frame of
31
+ * no time carries the solved state rigidly from the entity's frame a frame ago to now, as its own geometry is
32
+ * carried. Each step, from the node after the root to the tip, a node:
31
33
  *
32
34
  * - **predicts**: its velocity loses `1 − e^(−damping·h)`, gains gravity, and relaxes toward the air's
33
35
  * (`wind_field_sample` at the node) as `v ← u + (v − u) e^(−k h)`, `k` the wisp's drag rate
@@ -35,17 +37,21 @@ export const HAIR_SIMULATE_SETTINGS: WebGPUStruct;
35
37
  * - is pulled toward its rest place in the entity's frame (**global shape**) at a rate that falls from
36
38
  * `global_shape_rate` at the root to nothing at `global_shape_range` of the length, and toward its rest
37
39
  * direction turned by its parent's frame (**local shape**) at `local_shape_rate`, each a share
38
- * `1 − e^(−rate·h)`, so the hair behaves alike at any frame rate;
40
+ * `1 − e^(−rate·h)` a step;
39
41
  * - is pushed out of each of its instance's colliders by its wisp's radius plus the instance's thickness and the
40
42
  * collider's inflation, and loses `friction` of its sliding along it per push out as deep as the sliding;
41
43
  * - is put back onto the sphere of its rest segment's length about its parent (**follow the leader**), and never
42
- * farther from the root than its length along the guide; its velocity is its move over the substep, and its
43
- * parent's loses `HAIR_FOLLOW_THE_LEADER_DAMPING` of this node's correction over the substep (Müller's DFTL).
44
+ * farther from the root than its length along the guide; its velocity is its move over the step, and its
45
+ * parent's loses `HAIR_FOLLOW_THE_LEADER_DAMPING` of this node's correction over the step (Müller's DFTL).
44
46
  *
45
47
  * A segment's frame is its parent's, turned the least that takes the rest direction it predicts to the direction
46
- * the segment has; a node's frame, written on the last substep, blends the two segments either side of it. A state
47
- * to be laid at rest is the rest pose, still. The nodes' bounds, from the anchor, are gathered into the row's guide
48
- * bounds. Colliders and the wind are read in the world: the anchor plus a node's position.
48
+ * the segment has; a node's frame blends the two segments either side of it. A state to be laid at rest is the rest
49
+ * pose, still. Colliders and the wind are read in the world: the anchor plus a node's position.
50
+ *
51
+ * **What is drawn**, every frame, stepped or not: each node between where the last two steps solved it, in the frame
52
+ * of its guide's root at each, `settings.blend` of the way from the step before the last to the last, carried by the
53
+ * root's frame now. The drawn root is the root now; what was drawn the frame before is kept as a frame ago. The drawn
54
+ * nodes' bounds, from the anchor, are gathered into the row's guide bounds.
49
55
  *
50
56
  * @type {ComputeShader}
51
57
  */
@@ -1 +1 @@
1
- {"version":3,"file":"shader_hair_simulate.d.ts","sourceRoot":"","sources":["../../../../../src/shade/renderer/hair/shader_hair_simulate.js"],"names":[],"mappings":";AAmDA;;;;;;;;GAQG;AACH,yCAFU,aAAa,CAkEpB;AAEH,uDAK+B;AAU/B;;;;;;GAMG;AACH,yCAFU,aAAa,CA+BpB;AAEH,kDAO2B;AAsV3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,mCAFU,aAAa,CAMpB;8BAtiB2B,4BAA4B;6BAK7B,gCAAgC"}
1
+ {"version":3,"file":"shader_hair_simulate.d.ts","sourceRoot":"","sources":["../../../../../src/shade/renderer/hair/shader_hair_simulate.js"],"names":[],"mappings":";AAuDA;;;;;;;;GAQG;AACH,yCAFU,aAAa,CAkEpB;AAEH,uDAK+B;AAU/B;;;;;;GAMG;AACH,yCAFU,aAAa,CAqCpB;AAEH,kDAe2B;AAuY3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,mCAFU,aAAa,CAMpB;8BA/mB2B,4BAA4B;6BAK7B,gCAAgC"}
@@ -33,6 +33,10 @@ import {
33
33
  HAIR_STATE_LAST_POSITION,
34
34
  HAIR_STATE_NONE,
35
35
  HAIR_STATE_POSITION,
36
+ HAIR_STATE_PREVIOUS_POSITION,
37
+ HAIR_STATE_PREVIOUS_ROTATION,
38
+ HAIR_STATE_SOLVED_POSITION,
39
+ HAIR_STATE_SOLVED_ROTATION,
36
40
  HAIR_STATE_VELOCITY
37
41
  } from "./HAIR_SIMULATION.js";
38
42
  import { HAIR_LOD_WORKGROUP_SIZE } from "./shader_hair_lod.js";
@@ -140,9 +144,9 @@ pose_resources.createGroup()
140
144
  .addStorageBuffer("hair_colliders", WebGPUArray.u32, true);
141
145
 
142
146
  /**
143
- * Writes each collider of the frame's list the way from the world into its own frame (`HAIR_COLLIDER_WORDS`), a
144
- * thread a collider, from its entity's world matrix as the scene holds it: a rigid motion and a uniform scale, whose
145
- * largest axis is taken.
147
+ * Writes each collider of the frame's list the way from the world into its own frame (`HAIR_COLLIDER_WORDS`), now
148
+ * and a frame ago, a thread a collider, from its entity's world matrices as the scene holds them: a rigid motion and
149
+ * a uniform scale, whose largest axis is taken.
146
150
  *
147
151
  * @type {ComputeShader}
148
152
  */
@@ -152,6 +156,22 @@ export const shader_hair_collider_poses = ComputeShader.from({
152
156
  body: CodeChunk.from(
153
157
  //language=WGSL
154
158
  `
159
+ // the way from the world into the frame of a matrix, eight words from a word of the collider list
160
+ fn hair_collider_write_pose(at: u32, m: mat4x4<f32>) {
161
+ let scale = max(length(m[0].xyz), max(length(m[1].xyz), length(m[2].xyz)));
162
+ let rotation = quat_from_m3(mat3x3<f32>(m[0].xyz, m[1].xyz, m[2].xyz) * (1.0 / max(scale, 1e-30)));
163
+ let inverse = quat_conjugate(rotation);
164
+
165
+ hair_colliders[at] = bitcast<u32>(inverse.x);
166
+ hair_colliders[at + 1u] = bitcast<u32>(inverse.y);
167
+ hair_colliders[at + 2u] = bitcast<u32>(inverse.z);
168
+ hair_colliders[at + 3u] = bitcast<u32>(inverse.w);
169
+ hair_colliders[at + 4u] = bitcast<u32>(m[3].x);
170
+ hair_colliders[at + 5u] = bitcast<u32>(m[3].y);
171
+ hair_colliders[at + 6u] = bitcast<u32>(m[3].z);
172
+ hair_colliders[at + 7u] = bitcast<u32>(scale);
173
+ }
174
+
155
175
  @compute @workgroup_size(${COMPUTE_SHADER_DEFAULT_GROUP_SIZE})
156
176
  fn main(@builtin(global_invocation_id) id: vec3<u32>) {
157
177
  if (id.x >= settings.count) {
@@ -159,20 +179,10 @@ fn main(@builtin(global_invocation_id) id: vec3<u32>) {
159
179
  }
160
180
 
161
181
  let at = settings.first + id.x * ${HAIR_COLLIDER_WORDS}u;
162
- let m = scene_read_node(&scene_database, hair_colliders[at + 1u]).global;
163
-
164
- let scale = max(length(m[0].xyz), max(length(m[1].xyz), length(m[2].xyz)));
165
- let rotation = quat_from_m3(mat3x3<f32>(m[0].xyz, m[1].xyz, m[2].xyz) * (1.0 / max(scale, 1e-30)));
166
- let inverse = quat_conjugate(rotation);
182
+ let node = scene_read_node(&scene_database, hair_colliders[at + 1u]);
167
183
 
168
- hair_colliders[at + 8u] = bitcast<u32>(inverse.x);
169
- hair_colliders[at + 9u] = bitcast<u32>(inverse.y);
170
- hair_colliders[at + 10u] = bitcast<u32>(inverse.z);
171
- hair_colliders[at + 11u] = bitcast<u32>(inverse.w);
172
- hair_colliders[at + 12u] = bitcast<u32>(m[3].x);
173
- hair_colliders[at + 13u] = bitcast<u32>(m[3].y);
174
- hair_colliders[at + 14u] = bitcast<u32>(m[3].z);
175
- hair_colliders[at + 15u] = bitcast<u32>(scale);
184
+ hair_collider_write_pose(at + 8u, node.global);
185
+ hair_collider_write_pose(at + 16u, node.prev_global);
176
186
  }
177
187
  `, [chunk_scene_database_access, chunk_quat_from_m3, chunk_quat_conjugate]),
178
188
  });
@@ -180,10 +190,18 @@ fn main(@builtin(global_invocation_id) id: vec3<u32>) {
180
190
  export const HAIR_SIMULATE_SETTINGS = WebGPUStruct.from({
181
191
  /** the world's gravity, metres a second squared */
182
192
  gravity: vec3f,
183
- /** a substep, seconds */
193
+ /** a step, seconds */
184
194
  step: f32,
185
- /** substeps this frame */
186
- substeps: u32,
195
+ /** steps this frame */
196
+ steps: u32,
197
+ /** the share of the frame's motion the first step falls at, a frame ago at 0 and now at 1 */
198
+ first: f32,
199
+ /** the share of the frame's motion from a step to the next */
200
+ stride: f32,
201
+ /** how far what is drawn is from the step before the last, 0, to the last, 1 */
202
+ blend: f32,
203
+ /** the scene's clock moved this frame, seconds */
204
+ seconds: f32,
187
205
  }, "HairSimulateSettings");
188
206
 
189
207
  const simulate_resources = new ShaderResourceSetDescriptor();
@@ -216,8 +234,9 @@ fn hair_ordered(v: f32) -> u32 {
216
234
  return bits ^ select(0x80000000u, 0xFFFFFFFFu, (bits & 0x80000000u) != 0u);
217
235
  }
218
236
 
219
- // pushes a node out of the instance's colliders, by its wisp's radius, and takes away part of its sliding along them
220
- fn hair_collide(row: u32, anchor: vec3<f32>, start: vec3<f32>, position: vec3<f32>, radius: f32) -> vec3<f32> {
237
+ // pushes a node out of the instance's colliders, by its wisp's radius, and takes away part of its sliding along them;
238
+ // the colliders where they are at a share of the frame's motion, a frame ago at 0 and now at 1
239
+ fn hair_collide(row: u32, anchor: vec3<f32>, start: vec3<f32>, position: vec3<f32>, radius: f32, share: f32) -> vec3<f32> {
221
240
  var p = position;
222
241
 
223
242
  let first = hair_colliders[row * 2u];
@@ -234,9 +253,16 @@ fn hair_collide(row: u32, anchor: vec3<f32>, start: vec3<f32>, position: vec3<f3
234
253
  let friction = bitcast<f32>(hair_colliders[at + 2u]);
235
254
  let inflation = bitcast<f32>(hair_colliders[at + 3u]);
236
255
  let parameters = bitcast<vec4<f32>>(vec4<u32>(hair_colliders[at + 4u], hair_colliders[at + 5u], hair_colliders[at + 6u], hair_colliders[at + 7u]));
237
- let inverse = bitcast<vec4<f32>>(vec4<u32>(hair_colliders[at + 8u], hair_colliders[at + 9u], hair_colliders[at + 10u], hair_colliders[at + 11u]));
238
- let origin = bitcast<vec3<f32>>(vec3<u32>(hair_colliders[at + 12u], hair_colliders[at + 13u], hair_colliders[at + 14u]));
239
- let scale = bitcast<f32>(hair_colliders[at + 15u]);
256
+ let inverse_now = bitcast<vec4<f32>>(vec4<u32>(hair_colliders[at + 8u], hair_colliders[at + 9u], hair_colliders[at + 10u], hair_colliders[at + 11u]));
257
+ let origin_now = bitcast<vec3<f32>>(vec3<u32>(hair_colliders[at + 12u], hair_colliders[at + 13u], hair_colliders[at + 14u]));
258
+ let scale_now = bitcast<f32>(hair_colliders[at + 15u]);
259
+ let inverse_before = bitcast<vec4<f32>>(vec4<u32>(hair_colliders[at + 16u], hair_colliders[at + 17u], hair_colliders[at + 18u], hair_colliders[at + 19u]));
260
+ let origin_before = bitcast<vec3<f32>>(vec3<u32>(hair_colliders[at + 20u], hair_colliders[at + 21u], hair_colliders[at + 22u]));
261
+ let scale_before = bitcast<f32>(hair_colliders[at + 23u]);
262
+
263
+ let inverse = hair_quat_nlerp(inverse_before, inverse_now, share);
264
+ let origin = mix(origin_before, origin_now, share);
265
+ let scale = mix(scale_before, scale_now, share);
240
266
 
241
267
  let local = quat_rotate_v3(inverse, anchor + p - origin) / scale;
242
268
  let field = shape_record_signed_distance_gradient(kind, parameters, local);
@@ -247,7 +273,7 @@ fn hair_collide(row: u32, anchor: vec3<f32>, start: vec3<f32>, position: vec3<f3
247
273
 
248
274
  p -= normal * depth;
249
275
 
250
- // positional friction: of the sliding this substep, as much is taken away as the push out was deep
276
+ // positional friction: of the sliding this step, as much is taken away as the push out was deep
251
277
  let travel = p - start;
252
278
  let sliding = travel - normal * dot(travel, normal);
253
279
  let length_ = length(sliding);
@@ -320,6 +346,11 @@ fn main(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(local_invocatio
320
346
 
321
347
  const IDENTITY = vec4<f32>(0.0, 0.0, 0.0, 1.0);
322
348
 
349
+ // the root's frame now: its rotation, and where the root is
350
+ let turn_now = normalize(quat_from_m3(now * (1.0 / max(scale, 1e-30))));
351
+ let rest_0 = hair_rest_point(instance, strand, 0u).xyz;
352
+ let root_now = now * rest_0 + now_origin;
353
+
323
354
  if (reset) {
324
355
  // at rest in the entity's frame, still
325
356
  for (var i = 0u; i < nodes; i++) {
@@ -330,34 +361,42 @@ fn main(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(local_invocatio
330
361
  hair_guide_state[hair_state_index(instance, ${HAIR_STATE_FRAME}u, i, guide)] = IDENTITY;
331
362
  hair_guide_state[hair_state_index(instance, ${HAIR_STATE_LAST_POSITION}u, i, guide)] = vec4<f32>(x, 0.0);
332
363
  hair_guide_state[hair_state_index(instance, ${HAIR_STATE_LAST_FRAME}u, i, guide)] = IDENTITY;
364
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_POSITION}u, i, guide)] = vec4<f32>(x, 0.0);
365
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_ROTATION}u, i, guide)] = turn_now;
366
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_PREVIOUS_POSITION}u, i, guide)] = vec4<f32>(quat_rotate_v3(quat_conjugate(turn_now), x - root_now), 0.0);
367
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_PREVIOUS_ROTATION}u, i, guide)] = IDENTITY;
333
368
  }
334
- } else if (settings.substeps == 0u) {
335
- // no time this frame: the guides are carried with the entity from where it was to where it is, the way its
336
- // own geometry is, and what was becomes a frame ago
337
- let before_scale = max(length(before[0]), max(length(before[1]), length(before[2])));
338
- let carry = now * (transpose(before) * (1.0 / max(before_scale * before_scale, 1e-30)));
339
- let turn = quat_from_m3(carry * (before_scale / max(scale, 1e-30)));
340
-
369
+ } else {
370
+ // the frame begins: what was drawn becomes a frame ago, and the solved positions follow the anchor
341
371
  for (var i = 0u; i < nodes; i++) {
342
- let x = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_POSITION}u, i, guide)].xyz - moved;
343
- let v = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_VELOCITY}u, i, guide)].xyz;
344
- let q = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_FRAME}u, i, guide)];
372
+ let drawn = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_POSITION}u, i, guide)].xyz - moved;
373
+ let drawn_frame = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_FRAME}u, i, guide)];
374
+ let x = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_POSITION}u, i, guide)].xyz - moved;
345
375
 
346
- hair_guide_state[hair_state_index(instance, ${HAIR_STATE_LAST_POSITION}u, i, guide)] = vec4<f32>(x, 0.0);
347
- hair_guide_state[hair_state_index(instance, ${HAIR_STATE_LAST_FRAME}u, i, guide)] = q;
348
- hair_guide_state[hair_state_index(instance, ${HAIR_STATE_POSITION}u, i, guide)] = vec4<f32>(carry * (x - before_origin) + now_origin, 0.0);
349
- hair_guide_state[hair_state_index(instance, ${HAIR_STATE_VELOCITY}u, i, guide)] = vec4<f32>(quat_rotate_v3(turn, v), 0.0);
350
- hair_guide_state[hair_state_index(instance, ${HAIR_STATE_FRAME}u, i, guide)] = quat_multiply(quat_multiply(turn, q), quat_conjugate(turn));
376
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_LAST_POSITION}u, i, guide)] = vec4<f32>(drawn, 0.0);
377
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_LAST_FRAME}u, i, guide)] = drawn_frame;
378
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_POSITION}u, i, guide)] = vec4<f32>(x, 0.0);
351
379
  }
352
- } else {
353
- // the frame begins: positions follow the anchor, and what is now becomes a frame ago
354
- for (var i = 0u; i < nodes; i++) {
355
- let x = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_POSITION}u, i, guide)].xyz - moved;
356
- let q = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_FRAME}u, i, guide)];
357
380
 
358
- hair_guide_state[hair_state_index(instance, ${HAIR_STATE_POSITION}u, i, guide)] = vec4<f32>(x, 0.0);
359
- hair_guide_state[hair_state_index(instance, ${HAIR_STATE_LAST_POSITION}u, i, guide)] = vec4<f32>(x, 0.0);
360
- hair_guide_state[hair_state_index(instance, ${HAIR_STATE_LAST_FRAME}u, i, guide)] = q;
381
+ if (settings.seconds == 0.0) {
382
+ // no time this frame: the solved state is carried with the entity from where it was to where it is, the
383
+ // way its own geometry is
384
+ let before_scale = max(length(before[0]), max(length(before[1]), length(before[2])));
385
+ let carry = now * (transpose(before) * (1.0 / max(before_scale * before_scale, 1e-30)));
386
+ let turn = normalize(quat_from_m3(carry * (before_scale / max(scale, 1e-30))));
387
+ let grown = scale / max(before_scale, 1e-30);
388
+
389
+ for (var i = 0u; i < nodes; i++) {
390
+ let x = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_POSITION}u, i, guide)].xyz;
391
+ let v = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_VELOCITY}u, i, guide)].xyz;
392
+ let q = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_ROTATION}u, i, guide)];
393
+ let u = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_PREVIOUS_POSITION}u, i, guide)].xyz;
394
+
395
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_POSITION}u, i, guide)] = vec4<f32>(carry * (x - before_origin) + now_origin, 0.0);
396
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_VELOCITY}u, i, guide)] = vec4<f32>(quat_rotate_v3(turn, v), 0.0);
397
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_ROTATION}u, i, guide)] = normalize(quat_multiply(turn, q));
398
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_PREVIOUS_POSITION}u, i, guide)] = vec4<f32>(u * grown, 0.0);
399
+ }
361
400
  }
362
401
 
363
402
  let h = settings.step;
@@ -366,22 +405,36 @@ fn main(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(local_invocatio
366
405
  let keep_velocity = exp(-instance.damping * h);
367
406
  let local_pull = 1.0 - exp(-instance.local_shape_rate * h);
368
407
 
369
- let rest_0 = hair_rest_point(instance, strand, 0u).xyz;
408
+ for (var k = 0u; k < settings.steps; k++) {
409
+ if (k + 1u == settings.steps) {
410
+ // what the last step starts from is what the drawn state is blended from: in its root's frame
411
+ let from_root = quat_conjugate(hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_ROTATION}u, 0u, guide)]);
412
+ let root_before = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_POSITION}u, 0u, guide)].xyz;
370
413
 
371
- for (var k = 0u; k < settings.substeps; k++) {
372
- let last_substep = k + 1u == settings.substeps;
414
+ for (var i = 0u; i < nodes; i++) {
415
+ let x = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_POSITION}u, i, guide)].xyz;
416
+ let q = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_ROTATION}u, i, guide)];
373
417
 
374
- // the entity's frame through the frame, the anchor being where it ends
375
- let f = f32(k + 1u) / f32(settings.substeps);
418
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_PREVIOUS_POSITION}u, i, guide)] = vec4<f32>(quat_rotate_v3(from_root, x - root_before), 0.0);
419
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_PREVIOUS_ROTATION}u, i, guide)] = quat_multiply(from_root, q);
420
+ }
421
+ }
422
+
423
+ // the world at the step's time: the entity's frame, the anchor being where the frame ends
424
+ let f = settings.first + f32(k) * settings.stride;
376
425
  let linear = mat3x3<f32>(mix(before[0], now[0], f), mix(before[1], now[1], f), mix(before[2], now[2], f));
377
426
  let origin = mix(before_origin, now_origin, f);
378
427
 
428
+ let linear_scale = max(length(linear[0]), max(length(linear[1]), length(linear[2])));
429
+ let turn = normalize(quat_from_m3(linear * (1.0 / max(linear_scale, 1e-30))));
430
+
379
431
  // the root rides the entity; the first segment's rest direction is held by the local shape, from the
380
432
  // entity's frame
381
433
  let root = origin + linear * rest_0;
382
434
  var parent = root;
383
435
 
384
- hair_guide_state[hair_state_index(instance, ${HAIR_STATE_POSITION}u, 0u, guide)] = vec4<f32>(root, 0.0);
436
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_POSITION}u, 0u, guide)] = vec4<f32>(root, 0.0);
437
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_ROTATION}u, 0u, guide)] = turn;
385
438
 
386
439
  var rest_parent = rest_0;
387
440
  var arc = 0.0;
@@ -395,7 +448,7 @@ fn main(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(local_invocatio
395
448
  for (var i = 1u; i < nodes; i++) {
396
449
  let rest = hair_rest_point(instance, strand, i * step).xyz;
397
450
 
398
- let x = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_POSITION}u, i, guide)].xyz;
451
+ let x = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_POSITION}u, i, guide)].xyz;
399
452
  var v = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_VELOCITY}u, i, guide)].xyz;
400
453
 
401
454
  // predict: damping and gravity, then the drag of the wisp relaxing the velocity toward the air's
@@ -420,7 +473,7 @@ fn main(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(local_invocatio
420
473
 
421
474
  p += local_pull * (parent + quat_rotate_v3(frame, segment) - p);
422
475
 
423
- p = hair_collide(row, anchor, x, p, radius);
476
+ p = hair_collide(row, anchor, x, p, radius, f);
424
477
 
425
478
  // follow the leader: back onto the sphere of the segment's length about the parent
426
479
  let reach = p - parent;
@@ -461,13 +514,11 @@ fn main(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(local_invocatio
461
514
  if (pending) {
462
515
  pending_velocity -= correction * (${HAIR_FOLLOW_THE_LEADER_DAMPING} / h);
463
516
 
464
- hair_guide_state[hair_state_index(instance, ${HAIR_STATE_POSITION}u, i - 1u, guide)] = vec4<f32>(pending_position, 0.0);
517
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_POSITION}u, i - 1u, guide)] = vec4<f32>(pending_position, 0.0);
465
518
  hair_guide_state[hair_state_index(instance, ${HAIR_STATE_VELOCITY}u, i - 1u, guide)] = vec4<f32>(pending_velocity, 0.0);
466
- }
467
519
 
468
- if (last_substep && i >= 2u) {
469
520
  // a node's frame blends the segments either side of it; the root's is the entity's
470
- hair_guide_state[hair_state_index(instance, ${HAIR_STATE_FRAME}u, i - 1u, guide)] = hair_quat_nlerp(frame, next_frame, 0.5);
521
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_ROTATION}u, i - 1u, guide)] = quat_multiply(hair_quat_nlerp(frame, next_frame, 0.5), turn);
471
522
  }
472
523
 
473
524
  pending_position = p;
@@ -480,14 +531,30 @@ fn main(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(local_invocatio
480
531
  }
481
532
 
482
533
  if (pending) {
483
- hair_guide_state[hair_state_index(instance, ${HAIR_STATE_POSITION}u, nodes - 1u, guide)] = vec4<f32>(pending_position, 0.0);
534
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_POSITION}u, nodes - 1u, guide)] = vec4<f32>(pending_position, 0.0);
484
535
  hair_guide_state[hair_state_index(instance, ${HAIR_STATE_VELOCITY}u, nodes - 1u, guide)] = vec4<f32>(pending_velocity, 0.0);
536
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_ROTATION}u, nodes - 1u, guide)] = quat_multiply(frame, turn);
485
537
  }
538
+ }
486
539
 
487
- if (last_substep) {
488
- hair_guide_state[hair_state_index(instance, ${HAIR_STATE_FRAME}u, 0u, guide)] = IDENTITY;
489
- hair_guide_state[hair_state_index(instance, ${HAIR_STATE_FRAME}u, nodes - 1u, guide)] = frame;
490
- }
540
+ // what is drawn: between the last two steps in the root's frame, carried by the root's frame now
541
+ let solved_turn = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_ROTATION}u, 0u, guide)];
542
+ let solved_root = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_POSITION}u, 0u, guide)].xyz;
543
+ let from_solved = quat_conjugate(solved_turn);
544
+ let carry_solved = quat_multiply(turn_now, from_solved);
545
+ let from_now = quat_conjugate(turn_now);
546
+
547
+ for (var i = 0u; i < nodes; i++) {
548
+ let x = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_POSITION}u, i, guide)].xyz;
549
+ let q = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_SOLVED_ROTATION}u, i, guide)];
550
+ let u = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_PREVIOUS_POSITION}u, i, guide)].xyz;
551
+ let w = hair_guide_state[hair_state_index(instance, ${HAIR_STATE_PREVIOUS_ROTATION}u, i, guide)];
552
+
553
+ let place = mix(quat_rotate_v3(turn_now, u), quat_rotate_v3(carry_solved, x - solved_root), settings.blend);
554
+ let turned = hair_quat_nlerp(w, quat_multiply(from_solved, q), settings.blend);
555
+
556
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_POSITION}u, i, guide)] = vec4<f32>(root_now + place, 0.0);
557
+ hair_guide_state[hair_state_index(instance, ${HAIR_STATE_FRAME}u, i, guide)] = quat_multiply(quat_multiply(turn_now, turned), from_now);
491
558
  }
492
559
  }
493
560
 
@@ -527,14 +594,16 @@ fn main(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(local_invocatio
527
594
  ]);
528
595
 
529
596
  /**
530
- * Steps every simulated instance's guides through the frame, a thread a guide (`HAIR_SIMULATION.js`), the threads
531
- * of a row found from the inclusive sums of the rows' guide counts.
597
+ * Steps every simulated instance's guides through the frame's steps and writes what is drawn of them, a thread a
598
+ * guide (`HAIR_SIMULATION.js`), the threads of a row found from the inclusive sums of the rows' guide counts.
532
599
  *
533
- * A guide's root rides the entity, or its strand's root frame where the instance is bound to a mesh, the frame taken
534
- * linearly from a frame ago to now across the substeps; the first
535
- * segment's rest direction is held, as every other's, by the local shape from the entity's frame. A frame of no time
536
- * carries the guides rigidly from the entity's frame a frame ago to now, as its own geometry is carried. Each substep,
537
- * from the node after the root to the tip, a node:
600
+ * Every step is `settings.step` long; a frame takes `settings.steps` of them, none when its time has not reached the
601
+ * next (`hair_frame_steps`). A step reads the world at its own time within the frame: a guide's root rides the
602
+ * entity, or its strand's root frame where the instance is bound to a mesh, the frame taken linearly from a frame
603
+ * ago to now at the step's share of the frame's motion, and the colliders are where they were at that share. The
604
+ * first segment's rest direction is held, as every other's, by the local shape from the entity's frame. A frame of
605
+ * no time carries the solved state rigidly from the entity's frame a frame ago to now, as its own geometry is
606
+ * carried. Each step, from the node after the root to the tip, a node:
538
607
  *
539
608
  * - **predicts**: its velocity loses `1 − e^(−damping·h)`, gains gravity, and relaxes toward the air's
540
609
  * (`wind_field_sample` at the node) as `v ← u + (v − u) e^(−k h)`, `k` the wisp's drag rate
@@ -542,17 +611,21 @@ fn main(@builtin(workgroup_id) workgroup_id: vec3<u32>, @builtin(local_invocatio
542
611
  * - is pulled toward its rest place in the entity's frame (**global shape**) at a rate that falls from
543
612
  * `global_shape_rate` at the root to nothing at `global_shape_range` of the length, and toward its rest
544
613
  * direction turned by its parent's frame (**local shape**) at `local_shape_rate`, each a share
545
- * `1 − e^(−rate·h)`, so the hair behaves alike at any frame rate;
614
+ * `1 − e^(−rate·h)` a step;
546
615
  * - is pushed out of each of its instance's colliders by its wisp's radius plus the instance's thickness and the
547
616
  * collider's inflation, and loses `friction` of its sliding along it per push out as deep as the sliding;
548
617
  * - is put back onto the sphere of its rest segment's length about its parent (**follow the leader**), and never
549
- * farther from the root than its length along the guide; its velocity is its move over the substep, and its
550
- * parent's loses `HAIR_FOLLOW_THE_LEADER_DAMPING` of this node's correction over the substep (Müller's DFTL).
618
+ * farther from the root than its length along the guide; its velocity is its move over the step, and its
619
+ * parent's loses `HAIR_FOLLOW_THE_LEADER_DAMPING` of this node's correction over the step (Müller's DFTL).
551
620
  *
552
621
  * A segment's frame is its parent's, turned the least that takes the rest direction it predicts to the direction
553
- * the segment has; a node's frame, written on the last substep, blends the two segments either side of it. A state
554
- * to be laid at rest is the rest pose, still. The nodes' bounds, from the anchor, are gathered into the row's guide
555
- * bounds. Colliders and the wind are read in the world: the anchor plus a node's position.
622
+ * the segment has; a node's frame blends the two segments either side of it. A state to be laid at rest is the rest
623
+ * pose, still. Colliders and the wind are read in the world: the anchor plus a node's position.
624
+ *
625
+ * **What is drawn**, every frame, stepped or not: each node between where the last two steps solved it, in the frame
626
+ * of its guide's root at each, `settings.blend` of the way from the step before the last to the last, carried by the
627
+ * root's frame now. The drawn root is the root now; what was drawn the frame before is kept as a frame ago. The drawn
628
+ * nodes' bounds, from the anchor, are gathered into the row's guide bounds.
556
629
  *
557
630
  * @type {ComputeShader}
558
631
  */