Skip to content

Commit

Permalink
feat: align playcanvas helpers with three helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac-mason committed Oct 6, 2024
1 parent b1bf2f2 commit 48f0625
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 37 deletions.
39 changes: 20 additions & 19 deletions packages/recast-navigation-playcanvas/src/debug/debug-drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
StandardMaterial,
TYPE_FLOAT32,
VertexBuffer,
VertexFormat
VertexFormat,
} from 'playcanvas';

/**
Expand Down Expand Up @@ -61,23 +61,20 @@ export class DebugDrawer extends Entity {
pointMaterial: StandardMaterial;
lineMaterial: StandardMaterial;

private app: AppBase;
private graphicsDevice: GraphicsDevice;
private debugDrawImpl: any; // Replace 'any' with the actual type if available
private currentVertices: VertexData[] = [];
private currentPrimitive: number = 0;

constructor({
app,
triMaterial,
pointMaterial,
lineMaterial,
}: DebugDrawerParams) {
constructor(graphicsDevice: GraphicsDevice, params?: DebugDrawerParams) {
super();

this.app = app;
this.graphicsDevice = graphicsDevice;

this.triMaterial = triMaterial || new StandardMaterial();
if (!triMaterial) {
if (params?.triMaterial) {
this.triMaterial = params.triMaterial;
} else {
this.triMaterial = new StandardMaterial();
this.triMaterial.useLighting = false;
this.triMaterial.diffuse = new Color(1, 1, 1);
this.triMaterial.opacity = 0.4;
Expand All @@ -86,14 +83,18 @@ export class DebugDrawer extends Entity {
this.triMaterial.update();
}

this.pointMaterial = pointMaterial || new StandardMaterial();
if (!pointMaterial) {
if (params?.pointMaterial) {
this.pointMaterial = params.pointMaterial;
} else {
this.pointMaterial = new StandardMaterial();
this.pointMaterial.useLighting = false;
this.pointMaterial.update();
}

this.lineMaterial = lineMaterial || new StandardMaterial();
if (!lineMaterial) {
if (params?.lineMaterial) {
this.lineMaterial = params.lineMaterial;
} else {
this.lineMaterial = new StandardMaterial();
this.lineMaterial.useLighting = false;
this.lineMaterial.diffuse = new Color(1, 1, 1);
this.lineMaterial.emissive = new Color(1, 1, 1);
Expand Down Expand Up @@ -319,7 +320,7 @@ export class DebugDrawer extends Entity {
}

private endPoints(): void {
const graphicsDevice = this.app.graphicsDevice;
const graphicsDevice = this.graphicsDevice;

const positions: number[] = [];
const colors: number[] = [];
Expand All @@ -340,7 +341,7 @@ export class DebugDrawer extends Entity {
}

private endLines(): void {
const graphicsDevice = this.app.graphicsDevice;
const graphicsDevice = this.graphicsDevice;

const positions: number[] = [];
const colors: number[] = [];
Expand All @@ -361,7 +362,7 @@ export class DebugDrawer extends Entity {
}

private endTris(): void {
const graphicsDevice = this.app.graphicsDevice;
const graphicsDevice = this.graphicsDevice;

const positions: number[] = [];
const colors: number[] = [];
Expand All @@ -383,7 +384,7 @@ export class DebugDrawer extends Entity {

private endQuads(): void {
// Quads are converted to triangles
const graphicsDevice = this.app.graphicsDevice;
const graphicsDevice = this.graphicsDevice;

const positions: number[] = [];
const colors: number[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
} from 'playcanvas';

export type CrowdHelperParams = {
crowd: Crowd;
agentMaterial?: Material;
};

Expand All @@ -36,17 +35,18 @@ export class CrowdHelper extends GraphNode {
agentMeshData = new Map<number, CrowdAgentData>();

constructor(
crowd: Crowd,
graphicsDevice: GraphicsDevice,
{ crowd, agentMaterial }: CrowdHelperParams
params?: CrowdHelperParams
) {
super();

this.agentMeshData = new Map();
this.recastCrowd = crowd;
this.graphicsDevice = graphicsDevice;

if (agentMaterial) {
this.agentMaterial = agentMaterial;
if (params?.agentMaterial) {
this.agentMaterial = params.agentMaterial;
} else {
const material = new StandardMaterial();
material.diffuse.set(1, 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from 'playcanvas';

export type NavMeshHelperParams = {
navMesh: NavMesh;
navMeshMaterial?: Material;
};

Expand All @@ -26,15 +25,17 @@ export class NavMeshHelper extends Entity {
mesh!: Mesh;

constructor(
navMesh: NavMesh,
graphicsDevice: GraphicsDevice,
{ navMesh, navMeshMaterial }: NavMeshHelperParams
params?: NavMeshHelperParams
) {
super();

this.navMesh = navMesh;

// Create material if not provided
this.navMeshMaterial = navMeshMaterial || this.createDefaultMaterial();
this.navMeshMaterial =
params?.navMeshMaterial || this.createDefaultMaterial();

// Update the mesh
this.updateMesh(graphicsDevice);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
* Parameters for creating OffMeshConnectionsHelper.
*/
export type OffMeshConnectionsHelperParams = {
offMeshConnections?: OffMeshConnectionParams[];
entryCircleMaterial?: Material;
exitCircleMaterial?: Material;
lineMaterial?: Material;
Expand All @@ -40,13 +39,16 @@ export class OffMeshConnectionsHelper extends Entity {

lineMaterial: Material;

constructor(params: OffMeshConnectionsHelperParams) {
constructor(
offMeshConnections: OffMeshConnectionParams[],
params?: OffMeshConnectionsHelperParams
) {
super();

this.offMeshConnections = params.offMeshConnections || [];
this.offMeshConnections = offMeshConnections;

// Initialize materials
if (params.entryCircleMaterial) {
if (params?.entryCircleMaterial) {
this.entryCircleMaterial = params.entryCircleMaterial;
} else {
this.entryCircleMaterial = new StandardMaterial();
Expand All @@ -62,11 +64,11 @@ export class OffMeshConnectionsHelper extends Entity {
this.entryCircleMaterial.update();
}

if (params.exitCircleMaterial) {
if (params?.exitCircleMaterial) {
this.exitCircleMaterial = params.exitCircleMaterial;
} else {
this.exitCircleMaterial = new StandardMaterial();

if ('diffuse' in this.exitCircleMaterial) {
this.exitCircleMaterial.diffuse = new Color(0, 0, 1); // Blue
}
Expand All @@ -76,7 +78,7 @@ export class OffMeshConnectionsHelper extends Entity {
this.exitCircleMaterial.update();
}

if (params.lineMaterial) {
if (params?.lineMaterial) {
this.lineMaterial = params.lineMaterial;
} else {
this.lineMaterial = new StandardMaterial();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
* Parameters for creating TileCacheHelper.
*/
export type TileCacheHelperParams = {
tileCache: TileCache;
obstacleMaterial?: StandardMaterial;
};

Expand All @@ -23,14 +22,14 @@ export class TileCacheHelper extends Entity {
obstacleMeshes = new Map<Obstacle, Entity>();
obstacleMaterial: StandardMaterial;

constructor({ tileCache, obstacleMaterial }: TileCacheHelperParams) {
constructor(tileCache: TileCache, params?: TileCacheHelperParams) {
super();

this.tileCache = tileCache;

// Initialize obstacleMaterial
if (obstacleMaterial) {
this.obstacleMaterial = obstacleMaterial;
if (params?.obstacleMaterial) {
this.obstacleMaterial = params.obstacleMaterial;
} else {
this.obstacleMaterial = new StandardMaterial();
this.obstacleMaterial.diffuse = new Color(1, 0, 0); // Red color
Expand Down

0 comments on commit 48f0625

Please sign in to comment.