Skip to content

Commit 2ffdbde

Browse files
committed
particles
1 parent f90e4ef commit 2ffdbde

File tree

12 files changed

+5019
-4
lines changed

12 files changed

+5019
-4
lines changed
180 KB
Loading

packages/website/routes/static/particles.js

Lines changed: 2966 additions & 0 deletions
Large diffs are not rendered by default.

packages/website/verse/Graphics.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ THREE.InstancedMesh.prototype.resize = function (size) {
4343
// }
4444
}
4545

46+
// add alpha to Color
47+
THREE.Color.prototype.setRGBAHex = function (val) {
48+
if (typeof val === 'string' && val.length === 9) {
49+
const r = parseInt(val.slice(1, 3), 16) / 255
50+
const g = parseInt(val.slice(3, 5), 16) / 255
51+
const b = parseInt(val.slice(5, 7), 16) / 255
52+
const a = parseInt(val.slice(7, 9), 16) / 255
53+
this.setRGB(r, g, b)
54+
this.a = a
55+
return this
56+
}
57+
this.set(val)
58+
this.a = 1
59+
return this
60+
}
61+
4662
const v1 = new THREE.Vector3()
4763
const vec2 = new THREE.Vector2()
4864

packages/website/verse/Particles.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { isBoolean } from 'lodash-es'
2+
3+
import { System } from './System'
4+
5+
let ids = -1
6+
7+
export class Particles extends System {
8+
constructor(world) {
9+
super(world)
10+
this.worker = null
11+
this.systems = new Map() // id -> System
12+
}
13+
14+
init() {
15+
this.worker = new Worker('/static/particles.js')
16+
this.worker.onmessage = this.onMessage
17+
this.worker.onerror = this.onError
18+
}
19+
20+
update(delta) {
21+
this.systems.forEach(system => {
22+
system.node.update(delta)
23+
})
24+
// for (const system of this.systems) {
25+
// }
26+
}
27+
28+
onMessage = msg => {
29+
msg = msg.data
30+
this.systems.get(msg.systemId)?.onMessage(msg)
31+
}
32+
33+
onError = err => {
34+
console.error('[particles]', err)
35+
}
36+
37+
createSystem(node, options) {
38+
const id = ++ids
39+
const system = {
40+
id,
41+
node,
42+
onMessage: null,
43+
send: (msg, transfers) => {
44+
msg.systemId = id
45+
this.worker.postMessage(msg, transfers)
46+
},
47+
destroy: () => {
48+
this.systems.delete(id)
49+
this.worker.postMessage({ op: 'destroy', systemId: id })
50+
},
51+
}
52+
this.systems.set(id, system)
53+
this.worker.postMessage({ op: 'create', id, ...options })
54+
return system
55+
}
56+
57+
debug(enabled) {
58+
enabled = isBoolean(enabled) ? enabled : !this.isDebugging
59+
if (this.isDebugging === enabled) return
60+
this.worker.postMessage({ op: 'debug', enabled })
61+
this.isDebugging = enabled
62+
}
63+
64+
destroy() {
65+
console.error('[particles] todo: destroy')
66+
}
67+
}

packages/website/verse/Scripts.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { Vector3Enhanced } from './extras/Vector3Enhanced'
1111
import { clamp } from './extras/utils'
1212
import { Layers } from './extras/Layers'
1313
import { isNumber } from 'lodash-es'
14+
import { Curve } from './extras/Curve'
15+
import { Gradient } from './extras/Gradient'
1416

1517
export class Scripts extends System {
1618
constructor(world) {
@@ -43,6 +45,8 @@ export class Scripts extends System {
4345
Vector3Lerp: Vector3Lerp,
4446
QuaternionLerp: QuaternionLerp,
4547
Material: Material,
48+
Curve: Curve,
49+
Gradient: Gradient,
4650
DEG2RAD: DEG2RAD,
4751
RAD2DEG: RAD2DEG,
4852
// pause: () => this.world.pause(),

packages/website/verse/World.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Actions } from './Actions'
99
import { Models } from './Models'
1010
import { Composites } from './Composites'
1111
import { LODs } from './LODs'
12+
import { Particles } from './Particles'
1213
import { Scripts } from './Scripts'
1314
import { Panels } from './Panels'
1415
import { Permissions } from './Permissions'
@@ -46,6 +47,7 @@ export class World extends EventEmitter {
4647
this.models = this.register(Models)
4748
this.composites = this.register(Composites)
4849
this.lods = this.register(LODs)
50+
this.particles = this.register(Particles)
4951
this.scripts = this.register(Scripts)
5052
this.panels = this.register(Panels)
5153
this.permissions = this.register(Permissions)

0 commit comments

Comments
 (0)