babylon.quarks-editor 0.28.0 → 0.30.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.
Files changed (48) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +1 -1
  3. package/dist/core/collision.d.ts +2 -2
  4. package/dist/core/collision.js +3 -1
  5. package/dist/core/colors.d.ts +1 -8
  6. package/dist/core/colors.js +4 -10
  7. package/dist/core/defaultEffect.js +4 -2
  8. package/dist/core/editorSession.d.ts +0 -2
  9. package/dist/core/editorSession.js +0 -1
  10. package/dist/core/emitterShapeWireframe.js +11 -4
  11. package/dist/core/loadEffect.js +2 -0
  12. package/dist/core/shapes.d.ts +7 -1
  13. package/dist/core/shapes.js +13 -3
  14. package/dist/core/systems.js +6 -4
  15. package/dist/core/values.d.ts +23 -6
  16. package/dist/core/values.js +47 -9
  17. package/dist/index.d.ts +1 -1
  18. package/dist/index.js +1 -1
  19. package/dist/react/AxesCurveField.d.ts +21 -0
  20. package/dist/react/AxesCurveField.js +25 -0
  21. package/dist/react/LifeCurveField.d.ts +18 -0
  22. package/dist/react/LifeCurveField.js +26 -0
  23. package/dist/react/ValueField.d.ts +4 -4
  24. package/dist/react/ValueField.js +20 -6
  25. package/dist/react/behaviorModules.js +23 -20
  26. package/dist/react/index.d.ts +11 -1
  27. package/dist/react/inspectorHints.js +1 -1
  28. package/dist/react/modules.js +60 -29
  29. package/dist/session.d.ts +8 -0
  30. package/package.json +2 -2
  31. package/src/core/collision.ts +5 -3
  32. package/src/core/colors.ts +4 -11
  33. package/src/core/defaultEffect.ts +3 -2
  34. package/src/core/editorSession.ts +0 -3
  35. package/src/core/emitterShapeWireframe.ts +12 -4
  36. package/src/core/loadEffect.ts +2 -0
  37. package/src/core/shapes.ts +20 -2
  38. package/src/core/systems.ts +8 -6
  39. package/src/core/values.ts +73 -12
  40. package/src/index.ts +0 -1
  41. package/src/react/AxesCurveField.tsx +72 -0
  42. package/src/react/LifeCurveField.tsx +57 -0
  43. package/src/react/ValueField.tsx +46 -6
  44. package/src/react/behaviorModules.tsx +45 -35
  45. package/src/react/index.ts +19 -1
  46. package/src/react/inspectorHints.ts +1 -1
  47. package/src/react/modules.tsx +149 -51
  48. package/src/session.ts +16 -0
@@ -71,8 +71,6 @@ export interface EffectEditorHostHandle {
71
71
  importJson: (json: unknown) => void;
72
72
  /** Imports effect files (and the texture/geometry files they reference) into Assets. */
73
73
  importAssets: (files: FileList | File[]) => Promise<void>;
74
- /** @deprecated Renamed to `importAssets`. */
75
- importGallery: (files: FileList | File[]) => Promise<void>;
76
74
  /** The focused effect as quarks JSON; throws when no effect is focused. */
77
75
  exportJson: () => string;
78
76
  }
@@ -1079,7 +1077,6 @@ export function createEditorSession(options: EditorSessionOptions): EditorSessio
1079
1077
  renderer,
1080
1078
  importJson,
1081
1079
  importAssets,
1082
- importGallery: importAssets,
1083
1080
  exportJson: () => makeHandle().binding.exportJSON('EditorEffect'),
1084
1081
  });
1085
1082
 
@@ -6,6 +6,7 @@ import {MeshBuilder} from '@babylonjs/core/Meshes/meshBuilder.js';
6
6
  import {TransformNode} from '@babylonjs/core/Meshes/transformNode.js';
7
7
  import type {Scene} from '@babylonjs/core/scene.js';
8
8
  import type {ParticleSystem} from 'babylon.quarks';
9
+ import {ConeEmitFrom} from 'babylon.quarks';
9
10
  import {EDITOR_SCENE_NODE_METADATA} from './editorScene.js';
10
11
  import './lineShaders.js';
11
12
  import {getShapeType, readShapeParams} from './shapes.js';
@@ -120,8 +121,16 @@ function buildEmitterWireframe(system: ParticleSystem, scene: Scene): Mesh[] {
120
121
  lines('wf-cone-spoke-b', [Vector3.Zero(), arcPoint(radius, arc)]);
121
122
  }
122
123
 
123
- const preview = Math.max(radius * 2, 1.25);
124
- const farRadius = radius + preview * Math.tan(angle);
124
+ // Emit from Volume draws the volume itself, out to its length; the base a stretch of the
125
+ // directions.
126
+ const preview =
127
+ params.coneEmitFrom === ConeEmitFrom.Volume
128
+ ? Math.max(Number.isFinite(params.length) ? params.length : 0, 0.001)
129
+ : Math.max(radius * 2, 1.25);
130
+ // The rays and the far rim end at the same point, which a near-flat cone keeps in view.
131
+ const travel = preview / Math.max(Math.cos(angle), 0.05);
132
+ const farRadius = radius + Math.sin(angle) * travel;
133
+ const farZ = Math.cos(angle) * travel;
125
134
  const spokeCount = Math.max(4, Math.ceil((arc / (Math.PI * 2)) * 12));
126
135
  for (let i = 0; i <= spokeCount; i++) {
127
136
  const theta = (i / spokeCount) * arc;
@@ -131,10 +140,9 @@ function buildEmitterWireframe(system: ParticleSystem, scene: Scene): Mesh[] {
131
140
  Math.sin(angle) * Math.sin(theta),
132
141
  Math.cos(angle)
133
142
  );
134
- const travel = preview / Math.max(Math.cos(angle), 0.05);
135
143
  lines(`wf-cone-ray-${i}`, [rim, rim.add(dir.scale(travel))], DIRECTION_COLOR);
136
144
  }
137
- lines('wf-cone-far', xyArc(farRadius, arc, 32, preview), DIRECTION_COLOR);
145
+ lines('wf-cone-far', xyArc(farRadius, arc, 32, farZ), DIRECTION_COLOR);
138
146
  break;
139
147
  }
140
148
  case 'sphere': {
@@ -110,6 +110,8 @@ export function parseEffectFromJson(
110
110
  const systems: ParticleSystem[] = [];
111
111
  QuarksUtil.runOnAllParticleEmitters(root, (emitter: ParticleEmitter) => {
112
112
  const system = emitter.system as ParticleSystem;
113
+ // The editor previews over and over; the stop action is saved, not applied.
114
+ system.ignoreStopAction = true;
113
115
  if (registerRenderer && renderer) {
114
116
  renderer.addSystem(system);
115
117
  }
@@ -4,12 +4,14 @@ import {
4
4
  BoxEmitFrom,
5
5
  BoxEmitter,
6
6
  CircleEmitter,
7
+ ConeEmitFrom,
7
8
  ConeEmitter,
8
9
  ConstantValue,
9
10
  DonutEmitter,
10
11
  EdgeEmitter,
11
12
  EmitterMode,
12
13
  GridEmitter,
14
+ GridSampling,
13
15
  HemisphereEmitter,
14
16
  MeshSurfaceEmitter,
15
17
  PointEmitter,
@@ -82,6 +84,12 @@ export interface ShapeParams {
82
84
  depth: number;
83
85
  /** Where a box emits from: its volume, its faces or its edges. */
84
86
  emitFrom: BoxEmitFrom;
87
+ /** Where a cone emits from: its base or its volume. */
88
+ coneEmitFrom: ConeEmitFrom;
89
+ /** Height of a cone's volume (Unity's Length), for Emit from Volume. */
90
+ length: number;
91
+ /** How a grid picks its cells: at random or in turn. */
92
+ sampling: GridSampling;
85
93
  /** How fast an edge emitter's loop / ping-pong point travels, in lengths per second. */
86
94
  speed: number;
87
95
  /** The mesh a mesh surface emitter emits from. */
@@ -103,6 +111,9 @@ export const DEFAULT_SHAPE_PARAMS: ShapeParams = {
103
111
  row: 10,
104
112
  depth: 1,
105
113
  emitFrom: BoxEmitFrom.Volume,
114
+ coneEmitFrom: ConeEmitFrom.Base,
115
+ length: 5,
116
+ sampling: GridSampling.Random,
106
117
  speed: 1,
107
118
  mesh: undefined,
108
119
  };
@@ -131,7 +142,11 @@ export function readShapeParams(shape: EmitterShape): ShapeParams {
131
142
  column: num('column', DEFAULT_SHAPE_PARAMS.column),
132
143
  row: num('row', DEFAULT_SHAPE_PARAMS.row),
133
144
  depth: num('depth', DEFAULT_SHAPE_PARAMS.depth),
134
- emitFrom: num('emitFrom', DEFAULT_SHAPE_PARAMS.emitFrom) as BoxEmitFrom,
145
+ // A box and a cone both name it emitFrom, with different enums: each keeps its own.
146
+ emitFrom: shape instanceof BoxEmitter ? shape.emitFrom : DEFAULT_SHAPE_PARAMS.emitFrom,
147
+ coneEmitFrom: shape instanceof ConeEmitter ? shape.emitFrom : DEFAULT_SHAPE_PARAMS.coneEmitFrom,
148
+ length: num('length', DEFAULT_SHAPE_PARAMS.length),
149
+ sampling: num('sampling', DEFAULT_SHAPE_PARAMS.sampling) as GridSampling,
135
150
  speed: anyShape.speed instanceof ConstantValue ? anyShape.speed.value : DEFAULT_SHAPE_PARAMS.speed,
136
151
  mesh: anyShape.mesh as Mesh | undefined,
137
152
  };
@@ -143,7 +158,7 @@ export const SHAPE_PARAM_KEYS: {[K in ShapeType]: Array<keyof ShapeParams>} = {
143
158
  point: [],
144
159
  sphere: ['radius', 'arc', 'thickness'],
145
160
  hemisphere: ['radius', 'arc', 'thickness'],
146
- cone: ['radius', 'arc', 'thickness', 'angle'],
161
+ cone: ['radius', 'arc', 'thickness', 'angle', 'length'],
147
162
  circle: ['radius', 'arc', 'thickness'],
148
163
  donut: ['radius', 'arc', 'thickness', 'donutRadius'],
149
164
  rectangle: ['width', 'height'],
@@ -180,6 +195,8 @@ export function createShape(type: ShapeType, params: ShapeParams): EmitterShape
180
195
  arc: params.arc,
181
196
  thickness: params.thickness,
182
197
  angle: params.angle,
198
+ emitFrom: params.coneEmitFrom,
199
+ length: params.length,
183
200
  mode: params.mode,
184
201
  spread: params.spread,
185
202
  });
@@ -213,6 +230,7 @@ export function createShape(type: ShapeType, params: ShapeParams): EmitterShape
213
230
  height: params.height,
214
231
  column: params.column,
215
232
  row: params.row,
233
+ sampling: params.sampling,
216
234
  });
217
235
  case 'box':
218
236
  return new BoxEmitter({
@@ -3,10 +3,10 @@ import {
3
3
  ConeEmitter,
4
4
  ConstantColor,
5
5
  ConstantValue,
6
+ EmitSubParticleSystem,
6
7
  IntervalValue,
7
8
  ParticleSystem,
8
9
  RenderMode,
9
- Vector4,
10
10
  type ParticleEmitter,
11
11
  } from 'babylon.quarks';
12
12
  import type {EffectBinding} from './binding.js';
@@ -36,7 +36,7 @@ export function createChildSystem(parent: ParticleSystem, options: ChildSystemOp
36
36
  startLife: new IntervalValue(0.3, oneShot ? 0.6 : 1.2),
37
37
  startSpeed: new IntervalValue(1, 3),
38
38
  startSize: new IntervalValue(0.05, 0.2),
39
- startColor: new ConstantColor(new Vector4(1, 0.9, 0.6, 1)),
39
+ startColor: new ConstantColor({r: 1, g: 0.9, b: 0.6, a: 1}),
40
40
  emissionOverTime: new ConstantValue(oneShot ? 0 : 30),
41
41
  emissionBursts: oneShot
42
42
  ? [{time: 0, count: new ConstantValue(8), cycle: 1, interval: 0.01, probability: 1}]
@@ -49,6 +49,8 @@ export function createChildSystem(parent: ParticleSystem, options: ChildSystemOp
49
49
  worldSpace: true,
50
50
  });
51
51
  child.emitter.name = options.name ?? (oneShot ? 'sub-emitter' : 'child-system');
52
+ // The editor previews over and over; the stop action is saved, not applied.
53
+ child.ignoreStopAction = true;
52
54
  if (oneShot) {
53
55
  child.onlyUsedByOther = true;
54
56
  }
@@ -64,13 +66,13 @@ export function createChildSystem(parent: ParticleSystem, options: ChildSystemOp
64
66
  export function unlinkSubEmitterReferences(systems: ParticleSystem[], removed: ParticleSystem[]): void {
65
67
  const removedEmitters = new Set(removed.map((system) => system.emitter));
66
68
  for (const system of systems) {
67
- const behaviors = system.behaviors as Array<{type?: string; subParticleSystem?: ParticleEmitter}>;
69
+ const behaviors = system.behaviors;
68
70
  for (let i = behaviors.length - 1; i >= 0; i--) {
69
71
  const behavior = behaviors[i];
70
72
  if (
71
- behavior.type === 'EmitSubParticleSystem' &&
72
- behavior.subParticleSystem &&
73
- removedEmitters.has(behavior.subParticleSystem)
73
+ behavior instanceof EmitSubParticleSystem &&
74
+ typeof behavior.subParticleSystem === 'object' &&
75
+ removedEmitters.has(behavior.subParticleSystem as ParticleEmitter)
74
76
  ) {
75
77
  behaviors.splice(i, 1);
76
78
  }
@@ -1,12 +1,15 @@
1
1
  import type {FunctionValueGenerator, RotationGenerator, ValueGenerator, Vector3Generator} from 'babylon.quarks';
2
- import {Bezier, ConstantValue, IntervalValue, PiecewiseBezier} from 'babylon.quarks';
2
+ import {Bezier, ConstantValue, IntervalValue, PiecewiseBezier, RandomBetweenCurves} from 'babylon.quarks';
3
3
 
4
4
  /** A generator of one number: a constant or random value, or a curve over the particle's life. */
5
5
  export type ScalarGenerator = ValueGenerator | FunctionValueGenerator;
6
6
  /** Any generator a value field can be pointed at; ones that are not scalar read as the default. */
7
7
  export type AnyGenerator = ScalarGenerator | Vector3Generator | RotationGenerator;
8
- /** How a value field produces its number: Unity's Constant, Random Between Two Constants or Curve. */
9
- export type ScalarMode = 'constant' | 'random' | 'curve';
8
+ /**
9
+ * How a value field produces its number: Unity's Constant, Random Between Two Constants, Curve or
10
+ * Random Between Two Curves.
11
+ */
12
+ export type ScalarMode = 'constant' | 'random' | 'curve' | 'curves';
10
13
 
11
14
  /** Editable snapshot of a scalar value generator. */
12
15
  export interface ScalarValueState {
@@ -18,8 +21,10 @@ export interface ScalarValueState {
18
21
  min: number;
19
22
  /** Upper end of the random range (mode: random). */
20
23
  max: number;
21
- /** Cubic bezier control values over normalized lifetime (mode: curve). */
24
+ /** Cubic bezier control values over normalized lifetime (mode: curve; the first curve of curves). */
22
25
  curve: [number, number, number, number];
26
+ /** The second curve's control values (mode: curves); the first curve's when left out. */
27
+ curve2?: [number, number, number, number];
23
28
  }
24
29
 
25
30
  /**
@@ -27,7 +32,14 @@ export interface ScalarValueState {
27
32
  * Anything else, including a missing generator, reads as the constant 1.
28
33
  */
29
34
  export function readScalar(generator: AnyGenerator | undefined): ScalarValueState {
30
- const state: ScalarValueState = {mode: 'constant', value: 1, min: 0.5, max: 1.5, curve: [0, 1, 1, 0]};
35
+ const state: ScalarValueState = {
36
+ mode: 'constant',
37
+ value: 1,
38
+ min: 0.5,
39
+ max: 1.5,
40
+ curve: [0, 1, 1, 0],
41
+ curve2: [0, 1, 1, 0],
42
+ };
31
43
  if (generator instanceof ConstantValue) {
32
44
  state.mode = 'constant';
33
45
  state.value = generator.value;
@@ -40,15 +52,24 @@ export function readScalar(generator: AnyGenerator | undefined): ScalarValueStat
40
52
  state.value = (generator.a + generator.b) / 2;
41
53
  } else if (generator instanceof PiecewiseBezier) {
42
54
  state.mode = 'curve';
43
- const first = generator.functions[0]?.[0];
44
- if (first) {
45
- state.curve = [first.p[0], first.p[1], first.p[2], first.p[3]];
46
- state.value = first.p[0];
47
- }
55
+ state.curve = firstSegment(generator) ?? state.curve;
56
+ state.curve2 = state.curve;
57
+ state.value = state.curve[0];
58
+ } else if (generator instanceof RandomBetweenCurves) {
59
+ state.mode = 'curves';
60
+ state.curve = firstSegment(generator.curve1) ?? state.curve;
61
+ state.curve2 = firstSegment(generator.curve2) ?? state.curve;
62
+ state.value = state.curve[0];
48
63
  }
49
64
  return state;
50
65
  }
51
66
 
67
+ /** The control values of a curve's first segment, if it has one. */
68
+ function firstSegment(curve: PiecewiseBezier): [number, number, number, number] | undefined {
69
+ const first = curve.functions[0]?.[0];
70
+ return first ? [first.p[0], first.p[1], first.p[2], first.p[3]] : undefined;
71
+ }
72
+
52
73
  /** The generator a value field's state describes; a curve state builds a one-segment curve. */
53
74
  export function buildScalar(state: ScalarValueState & {mode: 'curve'}): PiecewiseBezier;
54
75
  export function buildScalar(state: ScalarValueState): ScalarGenerator;
@@ -60,6 +81,11 @@ export function buildScalar(state: ScalarValueState): ScalarGenerator {
60
81
  return new IntervalValue(Math.min(state.min, state.max), Math.max(state.min, state.max));
61
82
  case 'curve':
62
83
  return new PiecewiseBezier([[new Bezier(...state.curve), 0]]);
84
+ case 'curves':
85
+ return new RandomBetweenCurves(
86
+ new PiecewiseBezier([[new Bezier(...state.curve), 0]]),
87
+ new PiecewiseBezier([[new Bezier(...(state.curve2 ?? state.curve)), 0]])
88
+ );
63
89
  }
64
90
  }
65
91
 
@@ -71,8 +97,20 @@ export interface CurvePiece {
71
97
  p: [number, number, number, number];
72
98
  }
73
99
 
74
- /** Reads pieces from a PiecewiseBezier (or synthesizes one segment for other generators). */
75
- export function readPieces(generator: AnyGenerator | undefined): CurvePiece[] {
100
+ /**
101
+ * Reads pieces from a PiecewiseBezier, or from the first curve of a RandomBetweenCurves (the
102
+ * second with `second`). A constant reads as a flat curve, a random range as a flat curve at its
103
+ * lower end (its upper with `second`), and other generators as one default segment.
104
+ */
105
+ export function readPieces(generator: AnyGenerator | undefined, second = false): CurvePiece[] {
106
+ if (generator instanceof RandomBetweenCurves) {
107
+ return readPieces(second ? generator.curve2 : generator.curve1);
108
+ }
109
+ // A constant is a flat curve, and a random range two flat curves at its ends.
110
+ if (generator instanceof ConstantValue || generator instanceof IntervalValue) {
111
+ const value = generator instanceof ConstantValue ? generator.value : second ? generator.b : generator.a;
112
+ return [{start: 0, p: [value, value, value, value]}];
113
+ }
76
114
  if (generator instanceof PiecewiseBezier && generator.functions.length > 0) {
77
115
  return generator.functions.map(([bezier, start]) => ({
78
116
  start,
@@ -89,6 +127,29 @@ export function buildCurve(pieces: CurvePiece[]): PiecewiseBezier {
89
127
  return new PiecewiseBezier(sorted.map((piece) => [new Bezier(...piece.p), piece.start] as [Bezier, number]));
90
128
  }
91
129
 
130
+ /**
131
+ * `generator` as one curve (`'curve'`) or two (`'curves'`, Unity's Random Between Two Curves),
132
+ * keeping every segment: one curve becomes both bounds of two, and two keep their first as one.
133
+ * A constant is a flat curve, and a random range flat curves at its ends.
134
+ */
135
+ export function withCurves(generator: AnyGenerator | undefined, mode: 'curve'): PiecewiseBezier;
136
+ export function withCurves(generator: AnyGenerator | undefined, mode: 'curves'): RandomBetweenCurves;
137
+ export function withCurves(
138
+ generator: AnyGenerator | undefined,
139
+ mode: 'curve' | 'curves'
140
+ ): PiecewiseBezier | RandomBetweenCurves;
141
+ export function withCurves(
142
+ generator: AnyGenerator | undefined,
143
+ mode: 'curve' | 'curves'
144
+ ): PiecewiseBezier | RandomBetweenCurves {
145
+ const first = buildCurve(readPieces(generator));
146
+ if (mode === 'curve') {
147
+ return first;
148
+ }
149
+ const second = generator instanceof RandomBetweenCurves || generator instanceof IntervalValue;
150
+ return new RandomBetweenCurves(first, buildCurve(readPieces(generator, second)));
151
+ }
152
+
92
153
  /** Splits the piece containing x into two exact halves using Bezier.split. */
93
154
  export function splitPieces(pieces: CurvePiece[], x: number): CurvePiece[] {
94
155
  const sorted = [...pieces].sort((a, b) => a.start - b.start);
package/src/index.ts CHANGED
@@ -10,7 +10,6 @@ export {
10
10
  findBehavior,
11
11
  hexToRgb,
12
12
  rgbToHex,
13
- rgbaToVector4,
14
13
  stopToCss,
15
14
  stopsToCssGradient,
16
15
  } from './core/colors.js';
@@ -0,0 +1,72 @@
1
+ import {IntervalValue, PiecewiseBezier, RandomBetweenCurves, Vector3Function} from 'babylon.quarks';
2
+ import React from 'react';
3
+ import {AnyGenerator, withCurves} from '../core/values.js';
4
+ import {CheckboxField, Row} from './fields.js';
5
+ import {LifeCurveField} from './LifeCurveField.js';
6
+
7
+ const captionStyle: React.CSSProperties = {fontSize: 10.5, opacity: 0.6, marginTop: 6};
8
+
9
+ /** One curve or two, for one axis or for all of them. */
10
+ type AxisCurve = PiecewiseBezier | RandomBetweenCurves;
11
+
12
+ /** Props of an {@link AxesCurveField}. */
13
+ export interface AxesCurveFieldProps {
14
+ /** The generator shown: one curve (or two) for every axis, or a `Vector3Function` per axis. */
15
+ generator: AnyGenerator | undefined;
16
+ /** Top of the curve editors' value range. */
17
+ maxValue?: number;
18
+ /** Receives the edited generator. */
19
+ onChange: (generator: AxisCurve | Vector3Function) => void;
20
+ }
21
+
22
+ /**
23
+ * The curve or curves `generator` holds, as a fresh copy: a constant as a flat curve, a random
24
+ * range as two flat ones.
25
+ */
26
+ function curveOf(generator: AnyGenerator | undefined): AxisCurve {
27
+ const two = generator instanceof RandomBetweenCurves || generator instanceof IntervalValue;
28
+ return withCurves(generator, two ? 'curves' : 'curve');
29
+ }
30
+
31
+ /**
32
+ * A curve over life with Unity's Separate Axes switch: off, one curve (or two) for all axes; on, a
33
+ * {@link Vector3Function} with its own for X, Y and Z. Switching on gives every axis the shared
34
+ * curve; switching off keeps the X one.
35
+ */
36
+ export function AxesCurveField(props: AxesCurveFieldProps): React.ReactElement {
37
+ const {generator} = props;
38
+ const axes = generator instanceof Vector3Function ? generator : undefined;
39
+ const toggle = (on: boolean) =>
40
+ props.onChange(
41
+ on ? new Vector3Function(curveOf(generator), curveOf(generator), curveOf(generator)) : curveOf(axes?.x)
42
+ );
43
+ return (
44
+ <>
45
+ <Row label="Separate axes" hint="A curve for each of X, Y and Z, as Unity's Separate Axes.">
46
+ <CheckboxField value={axes !== undefined} onChange={toggle} />
47
+ </Row>
48
+ {axes === undefined ? (
49
+ <LifeCurveField generator={generator} maxValue={props.maxValue} onChange={props.onChange} />
50
+ ) : (
51
+ (['x', 'y', 'z'] as const).map((axis) => (
52
+ <div key={axis}>
53
+ <div style={captionStyle}>{axis.toUpperCase()}</div>
54
+ <LifeCurveField
55
+ generator={axes[axis]}
56
+ maxValue={props.maxValue}
57
+ onChange={(curve) =>
58
+ props.onChange(
59
+ new Vector3Function(
60
+ axis === 'x' ? curve : axes.x,
61
+ axis === 'y' ? curve : axes.y,
62
+ axis === 'z' ? curve : axes.z
63
+ )
64
+ )
65
+ }
66
+ />
67
+ </div>
68
+ ))
69
+ )}
70
+ </>
71
+ );
72
+ }
@@ -0,0 +1,57 @@
1
+ import {IntervalValue, PiecewiseBezier, RandomBetweenCurves} from 'babylon.quarks';
2
+ import React from 'react';
3
+ import {AnyGenerator, buildCurve, CurvePiece, readPieces, withCurves} from '../core/values.js';
4
+ import {CurveEditor} from './CurveEditor.js';
5
+ import {CheckboxField, Row} from './fields.js';
6
+
7
+ const captionStyle: React.CSSProperties = {fontSize: 10.5, opacity: 0.6, flex: 'none'};
8
+
9
+ /** Props of a {@link LifeCurveField}. */
10
+ export interface LifeCurveFieldProps {
11
+ /** The generator shown: one curve, two curves, or anything else, read as one curve. */
12
+ generator: AnyGenerator | undefined;
13
+ /** Top of the curve editors' value range. */
14
+ maxValue?: number;
15
+ /** Receives the edited curve or curves. */
16
+ onChange: (generator: PiecewiseBezier | RandomBetweenCurves) => void;
17
+ }
18
+
19
+ /**
20
+ * A setting that is a curve over the particle's life, or two with a "Two curves" switch (Unity's
21
+ * Random Between Two Curves): each particle then follows its own curve between the lower and the
22
+ * upper one. Every segment of both curves is kept through edits and switches.
23
+ */
24
+ export function LifeCurveField(props: LifeCurveFieldProps): React.ReactElement {
25
+ const {generator} = props;
26
+ // A random range shows as two flat curves, and an edit turns it into them.
27
+ const two = generator instanceof RandomBetweenCurves || generator instanceof IntervalValue;
28
+ const edit = (pieces: CurvePiece[], upper: boolean) => {
29
+ if (!two) {
30
+ props.onChange(buildCurve(pieces));
31
+ return;
32
+ }
33
+ const lower = upper ? buildCurve(readPieces(generator)) : buildCurve(pieces);
34
+ const higher = upper ? buildCurve(pieces) : buildCurve(readPieces(generator, true));
35
+ props.onChange(new RandomBetweenCurves(lower, higher));
36
+ };
37
+ return (
38
+ <>
39
+ <Row label="Two curves" hint="Random between two curves: each particle follows its own curve between them.">
40
+ <CheckboxField
41
+ value={two}
42
+ onChange={(on) => props.onChange(withCurves(generator, on ? 'curves' : 'curve'))}
43
+ />
44
+ </Row>
45
+ {(two ? (['lower', 'upper'] as const) : (['curve'] as const)).map((bound) => (
46
+ <div key={bound} style={{marginTop: 6}}>
47
+ {two && <span style={captionStyle}>{bound}</span>}
48
+ <CurveEditor
49
+ pieces={readPieces(generator, bound === 'upper')}
50
+ maxValue={props.maxValue}
51
+ onChange={(pieces) => edit(pieces, bound === 'upper')}
52
+ />
53
+ </div>
54
+ ))}
55
+ </>
56
+ );
57
+ }
@@ -1,4 +1,5 @@
1
1
  import type {ValueGenerator} from 'babylon.quarks';
2
+ import {RandomBetweenCurves} from 'babylon.quarks';
2
3
  import React from 'react';
3
4
  import {
4
5
  AnyGenerator,
@@ -8,6 +9,7 @@ import {
8
9
  buildScalar,
9
10
  readPieces,
10
11
  readScalar,
12
+ withCurves,
11
13
  } from '../core/values.js';
12
14
  import {CurveEditor} from './CurveEditor.js';
13
15
  import {NumberField, Row, SelectField} from './fields.js';
@@ -15,7 +17,7 @@ import {NumberField, Row, SelectField} from './fields.js';
15
17
  const captionStyle: React.CSSProperties = {fontSize: 10.5, opacity: 0.6, flex: 'none'};
16
18
 
17
19
  /** Props every {@link ValueField} takes. */
18
- interface ValueFieldBaseProps {
20
+ export interface ValueFieldBaseProps {
19
21
  /** The row's label. */
20
22
  label: string;
21
23
  /** Tooltip text of the label. */
@@ -51,13 +53,15 @@ function isValueGenerator(generator: ScalarGenerator): generator is ValueGenerat
51
53
  }
52
54
 
53
55
  /**
54
- * Unity-like value field: Constant | Random Between Two Constants | Curve, bound to a quarks value
55
- * generator. With `curve={false}` it offers only the first two, for settings sampled once.
56
+ * Unity-like value field: Constant | Random Between Two Constants | Curve | Random Between Two
57
+ * Curves, bound to a quarks value generator. With `curve={false}` it offers only the first two,
58
+ * for settings sampled once.
56
59
  */
57
60
  export function ValueField(props: CurveValueFieldProps | ConstantValueFieldProps): React.ReactElement {
58
61
  const read = readScalar(props.generator);
59
62
  // A setting without curves shows a curve it was loaded with as a constant, so an edit replaces it.
60
- const state: ScalarValueState = props.curve === false && read.mode === 'curve' ? {...read, mode: 'constant'} : read;
63
+ const curved = read.mode === 'curve' || read.mode === 'curves';
64
+ const state: ScalarValueState = props.curve === false && curved ? {...read, mode: 'constant'} : read;
61
65
 
62
66
  const emit = (generator: ScalarGenerator) => {
63
67
  if (props.curve !== false) {
@@ -76,9 +80,19 @@ export function ValueField(props: CurveValueFieldProps | ConstantValueFieldProps
76
80
  options={[
77
81
  {value: 'constant', label: 'Constant'},
78
82
  {value: 'random', label: 'Random between two'},
79
- ...(props.curve !== false ? [{value: 'curve' as const, label: 'Curve'}] : []),
83
+ ...(props.curve !== false
84
+ ? [
85
+ {value: 'curve' as const, label: 'Curve'},
86
+ {value: 'curves' as const, label: 'Random between two curves'},
87
+ ]
88
+ : []),
80
89
  ]}
81
- onChange={(mode) => commit({mode})}
90
+ onChange={(mode) =>
91
+ // Between one curve and two, every segment is kept; other switches start afresh.
92
+ (mode === 'curve' || mode === 'curves') && curved
93
+ ? emit(withCurves(props.generator, mode))
94
+ : commit({mode})
95
+ }
82
96
  />
83
97
  </Row>
84
98
  {state.mode === 'constant' && (
@@ -105,6 +119,32 @@ export function ValueField(props: CurveValueFieldProps | ConstantValueFieldProps
105
119
  />
106
120
  </div>
107
121
  )}
122
+ {state.mode === 'curves' && (
123
+ <>
124
+ {(['lower', 'upper'] as const).map((bound, index) => (
125
+ <div key={bound} style={{marginTop: 6}}>
126
+ <span style={captionStyle}>{bound}</span>
127
+ <CurveEditor
128
+ pieces={readPieces(props.generator, index === 1)}
129
+ maxValue={props.curveMax}
130
+ onChange={(pieces) =>
131
+ emit(
132
+ index === 0
133
+ ? new RandomBetweenCurves(
134
+ buildCurve(pieces),
135
+ buildCurve(readPieces(props.generator, true))
136
+ )
137
+ : new RandomBetweenCurves(
138
+ buildCurve(readPieces(props.generator)),
139
+ buildCurve(pieces)
140
+ )
141
+ )
142
+ }
143
+ />
144
+ </div>
145
+ ))}
146
+ </>
147
+ )}
108
148
  </>
109
149
  );
110
150
  }