Skip to content

Commit 7c7803a

Browse files
committed
Add GData.POSITION
also added a way to declare GData dependencies
1 parent 70b13dd commit 7c7803a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

src/enums/GData.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export enum GData {
2525

2626
NORMAL = "normal",
2727

28+
/**
29+
* The view-space position.
30+
*/
31+
32+
POSITION = "position",
33+
2834
/**
2935
* Occlusion, roughness and metalness.
3036
*/

src/utils/EffectShaderData.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ export class EffectShaderData implements ShaderData {
252252

253253
if(regExpGData.test(fragmentShader)) {
254254

255+
for(const dependency of this.gBufferConfig.gDataDependencies.get(value) ?? []) {
256+
257+
this.gData.add(dependency);
258+
259+
}
260+
255261
this.gData.add(value);
256262

257263
}
@@ -350,10 +356,13 @@ export class EffectShaderData implements ShaderData {
350356

351357
createGDataStructInitialization(): string {
352358

359+
const gDataDependencies = this.gBufferConfig.gDataDependencies;
360+
353361
return [
354362
"\tGData gData;",
355363
...Array.from(this.gBufferConfig.gDataStructInitialization)
356364
.filter(x => this.gData.has(x[0]))
365+
.sort((a, b) => gDataDependencies.has(a[0]) && gDataDependencies.get(a[0])!.has(b[0]) ? 1 : -1)
357366
.map(x => `\t${x[1]}`)
358367
].join("\n");
359368

src/utils/GBufferConfig.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ export class GBufferConfig extends EventDispatcher<BaseEventMap> {
5151

5252
readonly gDataStructInitialization: Map<GData | string, string>;
5353

54+
/**
55+
* A collection that describes {@link GData} cross-dependencies.
56+
*/
57+
58+
readonly gDataDependencies: Map<GData | string, Set<GData | string>>;
59+
5460
/**
5561
* Constructs new a new G-Buffer config.
5662
*/
@@ -81,6 +87,7 @@ export class GBufferConfig extends EventDispatcher<BaseEventMap> {
8187
[GData.COLOR, "vec4 color;"],
8288
[GData.DEPTH, "float depth;"],
8389
[GData.NORMAL, "vec3 normal;"],
90+
[GData.POSITION, "vec3 position;"],
8491
[GData.ORM, "vec3 orm;"],
8592
[GData.EMISSION, "vec3 emission;"],
8693
[GData.LUMINANCE, "float luminance;"]
@@ -90,11 +97,16 @@ export class GBufferConfig extends EventDispatcher<BaseEventMap> {
9097
[GData.COLOR, "gData.color = texture(gBuffer.color, UV);"],
9198
[GData.DEPTH, "gData.depth = texture(gBuffer.depth, UV).r;"],
9299
[GData.NORMAL, "gData.normal = texture(gBuffer.normal, UV).xyz;"],
100+
[GData.POSITION, "gData.position = getViewPosition(UV, gData.depth);"],
93101
[GData.ORM, "gData.orm = texture(gBuffer.orm, UV).xyz;"],
94102
[GData.EMISSION, "gData.emission = texture(gBuffer.emission, UV).rgb;"],
95103
[GData.LUMINANCE, "gData.luminance = luminance(gData.color.rgb);"]
96104
]);
97105

106+
const gDataDependencies = new ObservableMap<GData | string, Set<GData | string>>([
107+
[GData.POSITION, new Set<GData>([GData.DEPTH])]
108+
]);
109+
98110
const listener = () => this.dispatchEvent({ type: GBufferConfig.EVENT_CHANGE });
99111
textureConfigs.addEventListener(ObservableMap.EVENT_CHANGE, listener);
100112
gBufferStructFields.addEventListener(ObservableMap.EVENT_CHANGE, listener);
@@ -107,6 +119,7 @@ export class GBufferConfig extends EventDispatcher<BaseEventMap> {
107119
this.gBufferStructDeclaration = gBufferStructDeclaration;
108120
this.gDataStructDeclaration = gDataStructDeclaration;
109121
this.gDataStructInitialization = gDataStructInitialization;
122+
this.gDataDependencies = gDataDependencies;
110123

111124
}
112125

0 commit comments

Comments
 (0)