forked from Hubs-Foundation/hubs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Hubs-Foundation#5926 from mozilla/bitecs-particle-…
…emitter Bitecs particle emitter
- Loading branch information
Showing
8 changed files
with
171 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { defineQuery, enterQuery, exitQuery } from "bitecs"; | ||
import { ParticleEmitter } from "lib-hubs/packages/three-particle-emitter/lib/esm/index"; | ||
import { Texture } from "three"; | ||
|
||
import { HubsWorld } from "../app"; | ||
import { ParticleEmitterTag } from "../bit-components"; | ||
import { JobRunner } from "../utils/coroutine-utils"; | ||
import { proxiedUrlFor } from "../utils/media-url-utils"; | ||
import { resolveUrl, textureLoader } from "../utils/media-utils"; | ||
|
||
function* setTexture(world: HubsWorld, eid: number) { | ||
let src = APP.sid2str.get(ParticleEmitterTag.src[eid]); | ||
|
||
const result: URL = yield resolveUrl(src); | ||
let canonicalUrl = result.origin; | ||
|
||
// handle protocol relative urls | ||
if (canonicalUrl.startsWith("//")) { | ||
canonicalUrl = location.protocol + canonicalUrl; | ||
} | ||
|
||
// todo: we don't need to proxy for many things if the canonical URL has permissive CORS headers | ||
src = proxiedUrlFor(canonicalUrl); | ||
|
||
const texture: Texture = yield textureLoader.loadAsync(src); | ||
|
||
const particleEmitter: ParticleEmitter = world.eid2obj.get(eid)! as ParticleEmitter; | ||
particleEmitter.material.uniforms.map.value = texture; | ||
particleEmitter.visible = true; | ||
|
||
particleEmitter.updateParticles(); | ||
} | ||
|
||
const particleEmitterQuery = defineQuery([ParticleEmitterTag]); | ||
const particleEmitterEnterQuery = enterQuery(particleEmitterQuery); | ||
const particleEmitterExitQuery = exitQuery(particleEmitterQuery); | ||
|
||
const jobs = new JobRunner(); | ||
export function particleEmitterSystem(world: HubsWorld) { | ||
particleEmitterEnterQuery(world).forEach(eid => { | ||
jobs.add(eid, () => setTexture(world, eid)); | ||
}); | ||
particleEmitterExitQuery(world).forEach(function (eid) { | ||
// Object3D resources release happens in remove-object3D-system | ||
jobs.stop(eid); | ||
}); | ||
particleEmitterQuery(world).forEach(eid => { | ||
jobs.tick(); | ||
|
||
const particleEmitter: ParticleEmitter = world.eid2obj.get(eid)! as ParticleEmitter; | ||
if (particleEmitter.visible) { | ||
particleEmitter.update(world.time.delta / 1000); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { addComponent } from "bitecs"; | ||
import { addObject3DComponent } from "../utils/jsx-entity"; | ||
import { ParticleEmitterTag } from "../bit-components"; | ||
import { HubsWorld } from "../app"; | ||
import defaultSrcImage from "../assets/images/warning_icon.png"; | ||
import { ParticleEmitter } from "lib-hubs/packages/three-particle-emitter/lib/esm/index"; | ||
|
||
const defaultSrcUrl = new URL(defaultSrcImage, window.location.href).href; | ||
|
||
export type ParticleEmitterParams = { | ||
src: string; | ||
startColor: string; | ||
middleColor: string; | ||
endColor: string; | ||
startOpacity: number; | ||
middleOpacity: number; | ||
endOpacity: number; | ||
colorCurve: string; | ||
sizeCurve: string; | ||
startSize: number; | ||
endSize: number; | ||
sizeRandomness: number; | ||
ageRandomness: number; | ||
lifetime: number; | ||
lifetimeRandomness: number; | ||
particleCount: number; | ||
startVelocity: [number, number]; | ||
endVelocity: [number, number, number]; | ||
velocityCurve: string; | ||
angularVelocity: number; | ||
}; | ||
|
||
const DEFAULTS = { | ||
src: defaultSrcImage, | ||
startColor: "#ffffff", | ||
middleColor: "#ffffff", | ||
endColor: "#ffffff", | ||
startOpacity: 1, | ||
middleOpacity: 1, | ||
endOpacity: 1, | ||
colorCurve: "linear", | ||
sizeCurve: "linear", | ||
startSize: 0.25, | ||
endSize: 0.25, | ||
sizeRandomness: 0, | ||
ageRandomness: 10, | ||
lifetime: 5, | ||
lifetimeRandomness: 5, | ||
particleCount: 100, | ||
startVelocity: [0, 0, 0.5], | ||
endVelocity: [0, 0, 0], | ||
velocityCurve: "linear", | ||
angularVelocity: 0 | ||
}; | ||
|
||
export function inflateParticleEmitter(world: HubsWorld, eid: number, params: ParticleEmitterParams) { | ||
params = Object.assign({}, DEFAULTS, params); | ||
|
||
const particleEmitter = new ParticleEmitter(null); | ||
particleEmitter.visible = false; | ||
|
||
addObject3DComponent(world, eid, particleEmitter); | ||
addComponent(world, ParticleEmitterTag, eid); | ||
|
||
ParticleEmitterTag.src[eid] = APP.getSid(params.src || defaultSrcUrl); | ||
|
||
particleEmitter.startColor.set(params.startColor); | ||
particleEmitter.middleColor.set(params.middleColor); | ||
particleEmitter.endColor.set(params.endColor); | ||
particleEmitter.startOpacity = params.startOpacity; | ||
particleEmitter.middleOpacity = params.middleOpacity; | ||
particleEmitter.endOpacity = params.endOpacity; | ||
particleEmitter.colorCurve = params.colorCurve; | ||
particleEmitter.sizeCurve = params.sizeCurve; | ||
particleEmitter.startSize = params.startSize; | ||
particleEmitter.endSize = params.endSize; | ||
particleEmitter.sizeRandomness = params.sizeRandomness; | ||
particleEmitter.ageRandomness = params.ageRandomness; | ||
particleEmitter.lifetime = params.lifetime; | ||
particleEmitter.lifetimeRandomness = params.lifetimeRandomness; | ||
particleEmitter.particleCount = params.particleCount; | ||
particleEmitter.startVelocity.fromArray(params.startVelocity); | ||
particleEmitter.endVelocity.fromArray(params.endVelocity); | ||
particleEmitter.velocityCurve = params.velocityCurve; | ||
particleEmitter.angularVelocity = params.angularVelocity; | ||
|
||
return eid; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
declare module "lib-hubs/packages/three-particle-emitter/lib/esm/index" { | ||
import { ParticleEmitter as PE } from "lib-hubs/packages/three-particle-emitter/lib/types"; | ||
|
||
export class ParticleEmitter extends PE { | ||
material: ShaderMaterial; | ||
constructor(texture: Texture); | ||
} | ||
} |