forked from StrandedKitty/streets-gl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPass.ts
68 lines (50 loc) · 1.99 KB
/
Pass.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import Resource from "./Resource";
import Node from "./Node";
export enum InternalResourceType {
Input,
Output,
Local
}
export interface InternalResource {
type: InternalResourceType;
resource: Resource<any, any>;
}
export type ResourcePropMap = Record<string, InternalResource>;
export default abstract class Pass<T extends ResourcePropMap> extends Node {
public readonly isRenderable: boolean = true;
protected readonly internalResources: Map<keyof T, T[keyof T]>;
protected constructor(name: string, initialResources: T) {
super(name);
const internalResourcesMap = new Map();
for (const [name, resource] of Object.entries(initialResources)) {
internalResourcesMap.set(name, resource);
}
this.internalResources = internalResourcesMap;
}
public setResource<K extends keyof T>(name: K, resource: T[K]['resource']): void {
this.internalResources.get(name).resource = resource;
}
public getResource<K extends keyof T>(name: K): T[K]['resource'] {
return this.internalResources.get(name).resource;
}
public getPhysicalResource<K extends keyof T>(name: K): T[K]['resource']['physicalResourceBuilder']['type'] {
return this.getResource(name).attachedPhysicalResource;
}
public getOutputResourcesUsedExternally(): Set<Resource<any, any>> {
const resources: Set<Resource<any, any>> = new Set();
for (const internalResource of this.internalResources.values()) {
if (internalResource.type === InternalResourceType.Output && internalResource.resource.isUsedExternally) {
resources.add(internalResource.resource);
}
}
return resources;
}
public getAllResources(): Resource<any, any>[] {
return Array.from(this.internalResources.values()).map(r => r.resource).filter(r => r !== null);
}
public getAllResourcesOfType(type: InternalResourceType): Set<Resource<any, any>> {
const filtered = Array.from(this.internalResources.values()).filter(r => r.type === type);
return new Set(filtered.map(r => r.resource).filter(r => r !== null));
}
public abstract render(): void;
}