-
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.
chore: test pipeline and web workers
- Loading branch information
1 parent
f336bb9
commit 38f689a
Showing
19 changed files
with
349 additions
and
165 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
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,35 @@ | ||
import { StandardPipeline } from '@fibbojs/core' | ||
import type RAPIER from '@dimforge/rapier3d' | ||
import type { FScene } from '../core/FScene' | ||
|
||
export interface PhysicPipelineOptions { | ||
scene: FScene | ||
} | ||
|
||
/** | ||
* Render pipeline. | ||
*/ | ||
export class PhysicPipeline extends StandardPipeline { | ||
scene: FScene | ||
|
||
constructor(options: PhysicPipelineOptions) { | ||
super() | ||
this.scene = options.scene | ||
this.frameRate = 60 | ||
} | ||
|
||
frame(delta: number) { | ||
// Step the physics world | ||
this.scene.world.timestep = delta | ||
this.scene.world.step(this.scene.eventQueue) | ||
|
||
// Call frame for each collider and rigidBody | ||
this.scene.colliders.forEach(collider => collider.frame(delta)) | ||
this.scene.rigidBodies.forEach(rigidBody => rigidBody.frame(delta)) | ||
|
||
// Drain collision events | ||
this.scene.eventQueue.drainCollisionEvents((handle1: RAPIER.ColliderHandle, handle2: RAPIER.ColliderHandle, started: boolean) => { | ||
this.scene.handleCollision(handle1, handle2, started) | ||
}) | ||
} | ||
} |
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,20 @@ | ||
import { BackgroundPipeline } from '@fibbojs/core' | ||
|
||
export enum PhysicPipelineCommands { | ||
START = 'start', | ||
STOP = 'stop', | ||
} | ||
|
||
/** | ||
* Physic pipeline. | ||
* @category Pipeline | ||
*/ | ||
export class PhysicPipelineTest extends BackgroundPipeline { | ||
constructor() { | ||
super('../../3d/dist/pipeline/PhysicWorkerRun.mjs') | ||
} | ||
|
||
frame(_delta: number) { | ||
// console.log('PhysicPipeline frame') | ||
} | ||
} |
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,30 @@ | ||
import { BackgroundWorker } from '@fibbojs/core' | ||
|
||
/** | ||
* Physic web worker. | ||
* @category Pipeline | ||
*/ | ||
export class PhysicWorker extends BackgroundWorker { | ||
constructor(sw: DedicatedWorkerGlobalScope) { | ||
super(sw) | ||
} | ||
|
||
frame(delta: number) { | ||
console.log(`Worker : ${delta}`) | ||
|
||
/* | ||
// Step the physics world | ||
this.world.timestep = delta | ||
this.world.step(this.eventQueue) | ||
// Call frame for each collider and rigidBody | ||
this.colliders.forEach(collider => collider.frame(delta)) | ||
this.rigidBodies.forEach(rigidBody => rigidBody.frame(delta)) | ||
// Drain collision events | ||
this.eventQueue.drainCollisionEvents((handle1: RAPIER.ColliderHandle, handle2: RAPIER.ColliderHandle, started: boolean) => { | ||
this.handleCollision(handle1, handle2, started) | ||
}) | ||
*/ | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...core/src/pipeline/RenderPipelineWorker.ts → packages/3d/src/pipeline/PhysicWorkerRun.ts
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
/// <reference lib="WebWorker" /> | ||
|
||
import { RenderPipeline } from './RenderPipeline' | ||
import { PhysicWorker } from './PhysicWorker' | ||
|
||
export type {} | ||
declare let self: DedicatedWorkerGlobalScope | ||
|
||
new RenderPipeline(self) | ||
new PhysicWorker(self) |
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,30 @@ | ||
import { StandardPipeline } from '@fibbojs/core' | ||
import type { FScene } from '../core/FScene' | ||
|
||
export interface RenderPipelineOptions { | ||
scene: FScene | ||
} | ||
|
||
/** | ||
* Render pipeline. | ||
*/ | ||
export class RenderPipeline extends StandardPipeline { | ||
scene: FScene | ||
|
||
constructor(options: RenderPipelineOptions) { | ||
super() | ||
this.scene = options.scene | ||
this.frameRate = 60 | ||
} | ||
|
||
frame(delta: number) { | ||
// Call frame for each component | ||
this.scene.components.forEach(component => component.frame(delta)) | ||
|
||
// Call frame for the camera | ||
this.scene.camera.frame(delta) | ||
|
||
// Render the scene | ||
this.scene.renderer.render(this.scene.scene, this.scene.camera.__CAMERA__) | ||
} | ||
} |
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,33 @@ | ||
import { Pipeline, PipelineCommands } from './Pipeline' | ||
|
||
/** | ||
* A pipeline that abstract the usage of a web worker. | ||
* It provides better type checking, and more control over the worker. | ||
* @category Pipeline | ||
*/ | ||
export abstract class BackgroundPipeline extends Pipeline { | ||
worker: Worker | ||
|
||
constructor(path: string) { | ||
super() | ||
/* | ||
console.log(import.meta.url) | ||
console.log(new URL(path, import.meta.url).href) | ||
*/ | ||
this.worker = new Worker(new URL(path, import.meta.url), { type: 'module' }) | ||
} | ||
|
||
/** | ||
* Start the corresponding pipeline. | ||
*/ | ||
start(): void { | ||
this.worker.postMessage(PipelineCommands.START) | ||
} | ||
|
||
/** | ||
* Stop the corresponding pipeline. | ||
*/ | ||
stop(): void { | ||
this.worker.postMessage(PipelineCommands.STOP) | ||
} | ||
} |
Oops, something went wrong.