Universal rendering abstraction layer for NovaECS, providing a unified interface for different rendering backends.
NovaECS通用渲染抽象层,为不同的渲染后端提供统一接口。
npm install @esengine/nova-ecs-render-core
├── IRenderer (Core rendering interface)
│ ├── IDebugRenderer (Debug rendering extensions)
│ ├── IGameRenderer (Game rendering extensions)
│ └── IPhysicsDebugRenderer (Physics debug rendering)
│
├── BaseRenderer (Base implementation)
│ ├── BaseDebugRenderer (Debug renderer base)
│ ├── BaseGameRenderer (Game renderer base)
│ └── BasePhysicsDebugRenderer (Physics debug base)
import { BaseRenderer, Color, FixedVector2, LineStyle } from '@esengine/nova-ecs-render-core';
import { Fixed, FixedRect, FixedMatrix2x2 } from '@esengine/nova-ecs-math';
class MyRenderer extends BaseRenderer {
protected onBeginFrame(): void {
// Initialize frame rendering
}
protected onEndFrame(): void {
// Finalize and present frame
}
protected onClear(color: Color): void {
// Clear render target with color
}
protected onDrawLine(start: FixedVector2, end: FixedVector2, style: LineStyle): void {
// Implement line drawing
}
// ... implement other abstract methods
}
import { BaseDebugRenderer, DebugMode, GridStyle } from '@esengine/nova-ecs-render-core';
class MyDebugRenderer extends BaseDebugRenderer {
// ... implement base renderer methods
// Debug-specific functionality is already implemented in BaseDebugRenderer
// Usage example:
render(): void {
this.beginFrame();
// Draw debug grid
this.drawGrid(new Fixed(1), {
lineStyle: { color: { r: 0.3, g: 0.3, b: 0.3, a: 0.5 }, thickness: Fixed.ONE }
});
// Draw coordinate axes
this.drawAxis(FixedVector2.ZERO, new Fixed(2));
// Draw performance stats
this.drawPerformanceStats({
fps: 60,
frameTime: 16.67,
drawCalls: 150,
triangles: 5000,
vertices: 15000
});
this.endFrame();
}
}
import { BaseGameRenderer, SpriteAnimation, CameraConfig } from '@esengine/nova-ecs-render-core';
class MyGameRenderer extends BaseGameRenderer {
// ... implement base renderer methods
setupGame(): void {
// Setup camera
this.setCamera({
position: new FixedVector2(0, 0),
zoom: Fixed.ONE,
rotation: Fixed.ZERO
});
// Create render layers
this.createLayer('background', -10);
this.createLayer('gameplay', 0);
this.createLayer('ui', 10);
// Setup lighting
this.setLightingEnabled(true);
this.setAmbientLight({ r: 0.2, g: 0.2, b: 0.3, a: 1.0 });
}
renderGame(deltaTime: Fixed): void {
this.beginFrame();
// Render sprites with animation
this.drawSprite(playerAnimation, playerPosition, currentTime);
// Render particles
this.updateParticleSystem(explosionParticles, deltaTime);
this.drawParticleSystem(explosionParticles);
this.endFrame();
}
}
import { BasePhysicsDebugRenderer, PhysicsDebugConfig } from '@esengine/nova-ecs-render-core';
class MyPhysicsDebugRenderer extends BasePhysicsDebugRenderer {
// ... implement base renderer methods
setupPhysicsDebug(): void {
this.setPhysicsDebugConfig({
showBodies: true,
showContacts: true,
showVelocities: true,
showForces: true,
colors: {
dynamicBody: { r: 0.9, g: 0.7, b: 0.7, a: 0.8 },
staticBody: { r: 0.5, g: 0.9, b: 0.5, a: 0.8 },
contact: { r: 1.0, g: 0.0, b: 0.0, a: 1.0 }
}
});
}
renderPhysicsDebug(world: PhysicsWorld): void {
this.beginFrame();
// Draw all rigid bodies
for (const body of world.getBodies()) {
this.drawRigidBody(body);
}
// Draw all joints
for (const joint of world.getJoints()) {
this.drawJoint(joint);
}
// Draw contact points
const contacts = world.getContacts();
this.drawContactPoints(contacts);
// Draw physics statistics
this.drawPhysicsStats({
bodyCount: world.getBodyCount(),
contactCount: contacts.length,
jointCount: world.getJointCount(),
stepTime: world.getLastStepTime()
});
this.endFrame();
}
}
This core library provides the abstraction layer. Specific implementations for different rendering backends:
@esengine/nova-ecs-render-canvas
@esengine/nova-ecs-render-webgl
@esengine/nova-ecs-render-pixi
@esengine/nova-ecs-render-three
MIT License - See LICENSE file for details.
Issues and Pull Requests are welcome! 欢迎提交 Issue 和 Pull Request!