babylon.quarks 0.17.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/LICENSE +21 -0
- package/dist/babylon.quarks.cjs +2 -0
- package/dist/babylon.quarks.cjs.map +1 -0
- package/dist/babylon.quarks.esm.js +2 -0
- package/dist/babylon.quarks.esm.js.map +1 -0
- package/dist/types/BatchedParticleRenderer.d.ts +2 -0
- package/dist/types/BatchedRenderer.d.ts +40 -0
- package/dist/types/MeshSurfaceEmitter.d.ts +19 -0
- package/dist/types/ParticleEmitter.d.ts +16 -0
- package/dist/types/ParticleSystem.d.ts +226 -0
- package/dist/types/QuarksLoader.d.ts +28 -0
- package/dist/types/QuarksPrefab.d.ts +48 -0
- package/dist/types/QuarksUtil.d.ts +14 -0
- package/dist/types/SpriteBatch.d.ts +32 -0
- package/dist/types/TrailBatch.d.ts +27 -0
- package/dist/types/VFXBatch.d.ts +51 -0
- package/dist/types/index.d.ts +19 -0
- package/dist/types/materials/ParticleMaterials.d.ts +12 -0
- package/dist/types/materials/index.d.ts +1 -0
- package/dist/types/shaders/chunks/index.d.ts +1 -0
- package/dist/types/shaders/chunks/register-shader-chunks.d.ts +3 -0
- package/dist/types/shaders/chunks/soft_fragment.glsl.d.ts +2 -0
- package/dist/types/shaders/chunks/soft_pars_fragment.glsl.d.ts +2 -0
- package/dist/types/shaders/chunks/soft_pars_vertex.glsl.d.ts +2 -0
- package/dist/types/shaders/chunks/soft_vertex.glsl.d.ts +2 -0
- package/dist/types/shaders/chunks/tile_fragment.glsl.d.ts +2 -0
- package/dist/types/shaders/chunks/tile_pars_fragment.glsl.d.ts +2 -0
- package/dist/types/shaders/chunks/tile_pars_vertex.glsl.d.ts +2 -0
- package/dist/types/shaders/chunks/tile_vertex.glsl.d.ts +2 -0
- package/dist/types/shaders/index.d.ts +9 -0
- package/dist/types/shaders/local_particle_physics_vert.glsl.d.ts +2 -0
- package/dist/types/shaders/local_particle_vert.glsl.d.ts +2 -0
- package/dist/types/shaders/particle_frag.glsl.d.ts +2 -0
- package/dist/types/shaders/particle_physics_frag.glsl.d.ts +2 -0
- package/dist/types/shaders/particle_vert.glsl.d.ts +2 -0
- package/dist/types/shaders/stretched_bb_particle_vert.glsl.d.ts +2 -0
- package/dist/types/shaders/trail_frag.glsl.d.ts +2 -0
- package/dist/types/shaders/trail_vert.glsl.d.ts +2 -0
- package/package.json +58 -0
- package/src/BatchedParticleRenderer.ts +3 -0
- package/src/BatchedRenderer.ts +135 -0
- package/src/MeshSurfaceEmitter.ts +137 -0
- package/src/ParticleEmitter.ts +86 -0
- package/src/ParticleSystem.ts +1162 -0
- package/src/QuarksLoader.ts +503 -0
- package/src/QuarksPrefab.ts +307 -0
- package/src/QuarksUtil.ts +66 -0
- package/src/SpriteBatch.ts +391 -0
- package/src/TrailBatch.ts +345 -0
- package/src/VFXBatch.ts +102 -0
- package/src/index.ts +54 -0
- package/src/materials/ParticleMaterials.ts +23 -0
- package/src/materials/index.ts +1 -0
- package/src/shaders/chunks/index.ts +1 -0
- package/src/shaders/chunks/register-shader-chunks.ts +32 -0
- package/src/shaders/chunks/soft_fragment.glsl.ts +13 -0
- package/src/shaders/chunks/soft_pars_fragment.glsl.ts +20 -0
- package/src/shaders/chunks/soft_pars_vertex.glsl.ts +6 -0
- package/src/shaders/chunks/soft_vertex.glsl.ts +6 -0
- package/src/shaders/chunks/tile_fragment.glsl.ts +9 -0
- package/src/shaders/chunks/tile_pars_fragment.glsl.ts +9 -0
- package/src/shaders/chunks/tile_pars_vertex.glsl.ts +28 -0
- package/src/shaders/chunks/tile_vertex.glsl.ts +18 -0
- package/src/shaders/index.ts +9 -0
- package/src/shaders/local_particle_physics_vert.glsl.ts +66 -0
- package/src/shaders/local_particle_vert.glsl.ts +50 -0
- package/src/shaders/particle_frag.glsl.ts +61 -0
- package/src/shaders/particle_physics_frag.glsl.ts +67 -0
- package/src/shaders/particle_vert.glsl.ts +85 -0
- package/src/shaders/stretched_bb_particle_vert.glsl.ts +74 -0
- package/src/shaders/trail_frag.glsl.ts +21 -0
- package/src/shaders/trail_vert.glsl.ts +58 -0
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import {IAnimationData, IPrefab} from 'quarks.core';
|
|
2
|
+
import {TransformNode} from '@babylonjs/core/Meshes/transformNode';
|
|
3
|
+
import {Scene} from '@babylonjs/core/scene';
|
|
4
|
+
import {ParticleEmitter} from './ParticleEmitter';
|
|
5
|
+
import {BatchedRenderer} from './BatchedRenderer';
|
|
6
|
+
|
|
7
|
+
interface QuarksTimelineClip {
|
|
8
|
+
uuid?: string;
|
|
9
|
+
duration?: number;
|
|
10
|
+
play?: () => void;
|
|
11
|
+
pause?: () => void;
|
|
12
|
+
stop?: () => void;
|
|
13
|
+
setTime?: (time: number) => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface AnimationData extends IAnimationData {
|
|
17
|
+
type: 'three' | 'ps';
|
|
18
|
+
target: TransformNode;
|
|
19
|
+
loop: boolean;
|
|
20
|
+
clip?: QuarksTimelineClip;
|
|
21
|
+
clipUUID?: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface AnimationJSON {
|
|
25
|
+
startTime: number;
|
|
26
|
+
duration: number;
|
|
27
|
+
type: 'three' | 'ps';
|
|
28
|
+
targetUUID: string;
|
|
29
|
+
clipUUID?: string;
|
|
30
|
+
loop: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export class QuarksPrefab extends TransformNode implements IPrefab {
|
|
34
|
+
type = 'QuarksPrefab';
|
|
35
|
+
animationData: Array<AnimationData> = [];
|
|
36
|
+
isPlaying = false;
|
|
37
|
+
currentTime = -0.00001;
|
|
38
|
+
timeScale = 1;
|
|
39
|
+
duration = 0;
|
|
40
|
+
|
|
41
|
+
private _lastUpdateTimeMs = Date.now();
|
|
42
|
+
private _batchedRenderer?: BatchedRenderer;
|
|
43
|
+
private _tempAnimationJSON: Array<AnimationJSON> = [];
|
|
44
|
+
|
|
45
|
+
constructor(name = 'QuarksPrefab', scene?: Scene) {
|
|
46
|
+
super(name, scene);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
registerBatchedRenderer(renderer: BatchedRenderer): void {
|
|
50
|
+
this._batchedRenderer = renderer;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
addParticleSystemAnimation(
|
|
54
|
+
emitter: ParticleEmitter,
|
|
55
|
+
startTime = 0,
|
|
56
|
+
duration = 0,
|
|
57
|
+
loop = false
|
|
58
|
+
): AnimationData {
|
|
59
|
+
const animationDuration = duration > 0 ? duration : emitter.system.duration;
|
|
60
|
+
const data: AnimationData = {
|
|
61
|
+
startTime,
|
|
62
|
+
duration: animationDuration,
|
|
63
|
+
type: 'ps',
|
|
64
|
+
loop,
|
|
65
|
+
target: emitter,
|
|
66
|
+
};
|
|
67
|
+
this.animationData.push(data);
|
|
68
|
+
this.pause();
|
|
69
|
+
this.updateDuration();
|
|
70
|
+
return data;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
addThreeAnimation(
|
|
74
|
+
target: TransformNode,
|
|
75
|
+
clip?: QuarksTimelineClip,
|
|
76
|
+
startTime = 0,
|
|
77
|
+
duration = clip?.duration ?? 0,
|
|
78
|
+
loop = false,
|
|
79
|
+
clipUUID?: string
|
|
80
|
+
): AnimationData {
|
|
81
|
+
const animationDuration = duration > 0 ? duration : clip?.duration ?? 0;
|
|
82
|
+
const data: AnimationData = {
|
|
83
|
+
startTime,
|
|
84
|
+
duration: animationDuration,
|
|
85
|
+
type: 'three',
|
|
86
|
+
loop,
|
|
87
|
+
target,
|
|
88
|
+
clip,
|
|
89
|
+
clipUUID: clipUUID ?? clip?.uuid,
|
|
90
|
+
};
|
|
91
|
+
this.animationData.push(data);
|
|
92
|
+
this.updateDuration();
|
|
93
|
+
return data;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
removeAnimation(index: number): void {
|
|
97
|
+
this.animationData.splice(index, 1);
|
|
98
|
+
this.updateDuration();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
play(): void {
|
|
102
|
+
if (this.isPlaying) return;
|
|
103
|
+
this.isPlaying = true;
|
|
104
|
+
this._lastUpdateTimeMs = Date.now();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
pause(): void {
|
|
108
|
+
if (!this.isPlaying) return;
|
|
109
|
+
this.isPlaying = false;
|
|
110
|
+
this.animationData.forEach((animation) => {
|
|
111
|
+
if (animation.type === 'ps' && animation.target instanceof ParticleEmitter) {
|
|
112
|
+
animation.target.system.pause();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
animation.clip?.pause?.();
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
stop(): void {
|
|
120
|
+
this.pause();
|
|
121
|
+
this.currentTime = -0.00001;
|
|
122
|
+
this.animationData.forEach((animation) => {
|
|
123
|
+
if (animation.type === 'ps' && animation.target instanceof ParticleEmitter) {
|
|
124
|
+
animation.target.system.stop();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
animation.clip?.stop?.();
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
update(forceDelta?: number): void {
|
|
132
|
+
if (!this.isPlaying) return;
|
|
133
|
+
const now = Date.now();
|
|
134
|
+
const delta = forceDelta ?? Math.max((now - this._lastUpdateTimeMs) / 1000, 0);
|
|
135
|
+
this._lastUpdateTimeMs = now;
|
|
136
|
+
const previousTime = this.currentTime;
|
|
137
|
+
this.currentTime += delta * this.timeScale;
|
|
138
|
+
|
|
139
|
+
if (this.currentTime > this.duration) {
|
|
140
|
+
this.stop();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
this.animationData.forEach((animation) => {
|
|
145
|
+
const startTime = animation.startTime;
|
|
146
|
+
const endTime = animation.startTime + animation.duration;
|
|
147
|
+
const isActive = this.currentTime >= startTime && this.currentTime <= endTime;
|
|
148
|
+
const wasActive = previousTime >= startTime && previousTime <= endTime;
|
|
149
|
+
if (isActive && !wasActive) {
|
|
150
|
+
if (animation.type === 'ps' && animation.target instanceof ParticleEmitter) {
|
|
151
|
+
animation.target.system.restart();
|
|
152
|
+
if (this._batchedRenderer) {
|
|
153
|
+
this._batchedRenderer.addSystem(animation.target.system);
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
animation.clip?.play?.();
|
|
157
|
+
}
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!isActive && wasActive) {
|
|
162
|
+
if (animation.loop && animation.type === 'ps' && animation.target instanceof ParticleEmitter) {
|
|
163
|
+
animation.target.system.restart();
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (animation.type === 'ps' && animation.target instanceof ParticleEmitter) {
|
|
167
|
+
animation.target.system.endEmit();
|
|
168
|
+
} else if (animation.loop) {
|
|
169
|
+
animation.clip?.play?.();
|
|
170
|
+
} else {
|
|
171
|
+
animation.clip?.stop?.();
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
setTime(time: number): void {
|
|
178
|
+
const previousTime = this.currentTime;
|
|
179
|
+
this.currentTime = time;
|
|
180
|
+
this.animationData.forEach((animation) => {
|
|
181
|
+
const startTime = animation.startTime;
|
|
182
|
+
const endTime = animation.startTime + animation.duration;
|
|
183
|
+
const isActive = this.currentTime >= startTime && this.currentTime < endTime;
|
|
184
|
+
const wasActive = previousTime >= startTime && previousTime < endTime;
|
|
185
|
+
if (isActive && !wasActive) {
|
|
186
|
+
if (animation.type === 'ps' && animation.target instanceof ParticleEmitter) {
|
|
187
|
+
animation.target.system.restart();
|
|
188
|
+
} else {
|
|
189
|
+
animation.clip?.play?.();
|
|
190
|
+
}
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (!isActive && wasActive) {
|
|
194
|
+
if (animation.type === 'ps' && animation.target instanceof ParticleEmitter) {
|
|
195
|
+
animation.target.system.endEmit();
|
|
196
|
+
} else {
|
|
197
|
+
animation.clip?.stop?.();
|
|
198
|
+
}
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
if (isActive && animation.type === 'three') {
|
|
202
|
+
animation.clip?.setTime?.(Math.max(time - startTime, 0));
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
getDuration(): number {
|
|
208
|
+
return this.duration;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
resolveReferences(root: TransformNode): void {
|
|
212
|
+
const nodesMap: {[uuid: string]: TransformNode} = {};
|
|
213
|
+
const traverse = (node: TransformNode) => {
|
|
214
|
+
if ((node as any)._quarksUUID) {
|
|
215
|
+
nodesMap[(node as any)._quarksUUID] = node;
|
|
216
|
+
}
|
|
217
|
+
nodesMap[node.uniqueId.toString()] = node;
|
|
218
|
+
const children = node.getChildren();
|
|
219
|
+
for (const child of children) {
|
|
220
|
+
if (child instanceof TransformNode) {
|
|
221
|
+
traverse(child);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
traverse(root);
|
|
226
|
+
|
|
227
|
+
this._tempAnimationJSON.forEach((animationJSON) => {
|
|
228
|
+
const target = nodesMap[animationJSON.targetUUID];
|
|
229
|
+
if (animationJSON.type === 'ps' && target instanceof ParticleEmitter) {
|
|
230
|
+
this.addParticleSystemAnimation(
|
|
231
|
+
target,
|
|
232
|
+
animationJSON.startTime,
|
|
233
|
+
animationJSON.duration,
|
|
234
|
+
animationJSON.loop
|
|
235
|
+
);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (animationJSON.type === 'three' && target) {
|
|
239
|
+
const clip = this.resolveTimelineClip(target, animationJSON.clipUUID);
|
|
240
|
+
this.addThreeAnimation(
|
|
241
|
+
target,
|
|
242
|
+
clip,
|
|
243
|
+
animationJSON.startTime,
|
|
244
|
+
animationJSON.duration,
|
|
245
|
+
animationJSON.loop,
|
|
246
|
+
animationJSON.clipUUID
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
this.updateDuration();
|
|
251
|
+
this._tempAnimationJSON = [];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
toJSON(): any {
|
|
255
|
+
return {
|
|
256
|
+
type: this.type,
|
|
257
|
+
name: this.name,
|
|
258
|
+
animationData: this.animationData.map((animation) => ({
|
|
259
|
+
startTime: animation.startTime,
|
|
260
|
+
duration: animation.duration,
|
|
261
|
+
type: animation.type,
|
|
262
|
+
targetUUID: (animation.target as any)._quarksUUID ?? animation.target.uniqueId.toString(),
|
|
263
|
+
clipUUID: animation.type === 'three' ? animation.clipUUID ?? animation.clip?.uuid : undefined,
|
|
264
|
+
loop: animation.loop,
|
|
265
|
+
})),
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
static fromJSON(json: any, scene?: Scene): QuarksPrefab {
|
|
270
|
+
const prefab = new QuarksPrefab(json.name || 'QuarksPrefab', scene);
|
|
271
|
+
if (Array.isArray(json.animationData)) {
|
|
272
|
+
prefab._tempAnimationJSON = json.animationData.filter((item: any) => item?.type === 'ps' || item?.type === 'three');
|
|
273
|
+
}
|
|
274
|
+
return prefab;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
private resolveTimelineClip(target: TransformNode, clipUUID?: string): QuarksTimelineClip | undefined {
|
|
278
|
+
const animationSources = [
|
|
279
|
+
(target as any).animations,
|
|
280
|
+
(target as any).animationGroups,
|
|
281
|
+
(target.metadata as any)?.animations,
|
|
282
|
+
(target.metadata as any)?.animationGroups,
|
|
283
|
+
];
|
|
284
|
+
for (const source of animationSources) {
|
|
285
|
+
if (!Array.isArray(source)) continue;
|
|
286
|
+
if (!clipUUID) {
|
|
287
|
+
return source[0];
|
|
288
|
+
}
|
|
289
|
+
const found = source.find((item: any) => item?.uuid === clipUUID);
|
|
290
|
+
if (found) {
|
|
291
|
+
return found;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return undefined;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private updateDuration(): void {
|
|
298
|
+
let maxDuration = 0;
|
|
299
|
+
this.animationData.forEach((animation) => {
|
|
300
|
+
const endTime = animation.startTime + animation.duration;
|
|
301
|
+
if (endTime > maxDuration) {
|
|
302
|
+
maxDuration = endTime;
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
this.duration = maxDuration;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import {Node} from '@babylonjs/core/node';
|
|
2
|
+
import {TransformNode} from '@babylonjs/core/Meshes/transformNode';
|
|
3
|
+
import {ParticleEmitter} from './ParticleEmitter';
|
|
4
|
+
import {ParticleSystem} from './ParticleSystem';
|
|
5
|
+
import {BatchedRenderer} from './BatchedRenderer';
|
|
6
|
+
import {IParticleSystem} from 'quarks.core';
|
|
7
|
+
|
|
8
|
+
export class QuarksUtil {
|
|
9
|
+
static runOnAllParticleEmitters(root: Node, callback: (emitter: ParticleEmitter) => void): void {
|
|
10
|
+
QuarksUtil.traverseNode(root, (node) => {
|
|
11
|
+
if (node instanceof ParticleEmitter) {
|
|
12
|
+
callback(node);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static addToBatchRenderer(root: Node, batchRenderer: BatchedRenderer): void {
|
|
18
|
+
QuarksUtil.runOnAllParticleEmitters(root, (emitter) => {
|
|
19
|
+
batchRenderer.addSystem(emitter.system);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static play(root: Node): void {
|
|
24
|
+
QuarksUtil.runOnAllParticleEmitters(root, (emitter) => {
|
|
25
|
+
(emitter.system as unknown as ParticleSystem).play();
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static stop(root: Node): void {
|
|
30
|
+
QuarksUtil.runOnAllParticleEmitters(root, (emitter) => {
|
|
31
|
+
(emitter.system as unknown as ParticleSystem).stop();
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
static pause(root: Node): void {
|
|
36
|
+
QuarksUtil.runOnAllParticleEmitters(root, (emitter) => {
|
|
37
|
+
(emitter.system as unknown as ParticleSystem).pause();
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static restart(root: Node): void {
|
|
42
|
+
QuarksUtil.runOnAllParticleEmitters(root, (emitter) => {
|
|
43
|
+
(emitter.system as unknown as ParticleSystem).restart();
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
static setAutoDestroy(root: Node, autoDestroy: boolean): void {
|
|
48
|
+
QuarksUtil.runOnAllParticleEmitters(root, (emitter) => {
|
|
49
|
+
(emitter.system as unknown as ParticleSystem).autoDestroy = autoDestroy;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
static endEmit(root: Node): void {
|
|
54
|
+
QuarksUtil.runOnAllParticleEmitters(root, (emitter) => {
|
|
55
|
+
(emitter.system as unknown as ParticleSystem).endEmit();
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private static traverseNode(node: Node, callback: (node: Node) => void): void {
|
|
60
|
+
callback(node);
|
|
61
|
+
const children = node.getChildren();
|
|
62
|
+
for (const child of children) {
|
|
63
|
+
QuarksUtil.traverseNode(child, callback);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|