Skip to content

Commit

Permalink
Add GData.POSITION
Browse files Browse the repository at this point in the history
also added a way to declare GData dependencies
  • Loading branch information
vanruesc committed Apr 1, 2024
1 parent 70b13dd commit 7c7803a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/enums/GData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export enum GData {

NORMAL = "normal",

/**
* The view-space position.
*/

POSITION = "position",

/**
* Occlusion, roughness and metalness.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/utils/EffectShaderData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ export class EffectShaderData implements ShaderData {

if(regExpGData.test(fragmentShader)) {

for(const dependency of this.gBufferConfig.gDataDependencies.get(value) ?? []) {

this.gData.add(dependency);

}

this.gData.add(value);

}
Expand Down Expand Up @@ -350,10 +356,13 @@ export class EffectShaderData implements ShaderData {

createGDataStructInitialization(): string {

const gDataDependencies = this.gBufferConfig.gDataDependencies;

return [
"\tGData gData;",
...Array.from(this.gBufferConfig.gDataStructInitialization)
.filter(x => this.gData.has(x[0]))
.sort((a, b) => gDataDependencies.has(a[0]) && gDataDependencies.get(a[0])!.has(b[0]) ? 1 : -1)
.map(x => `\t${x[1]}`)
].join("\n");

Expand Down
13 changes: 13 additions & 0 deletions src/utils/GBufferConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export class GBufferConfig extends EventDispatcher<BaseEventMap> {

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

/**
* A collection that describes {@link GData} cross-dependencies.
*/

readonly gDataDependencies: Map<GData | string, Set<GData | string>>;

/**
* Constructs new a new G-Buffer config.
*/
Expand Down Expand Up @@ -81,6 +87,7 @@ export class GBufferConfig extends EventDispatcher<BaseEventMap> {
[GData.COLOR, "vec4 color;"],
[GData.DEPTH, "float depth;"],
[GData.NORMAL, "vec3 normal;"],
[GData.POSITION, "vec3 position;"],
[GData.ORM, "vec3 orm;"],
[GData.EMISSION, "vec3 emission;"],
[GData.LUMINANCE, "float luminance;"]
Expand All @@ -90,11 +97,16 @@ export class GBufferConfig extends EventDispatcher<BaseEventMap> {
[GData.COLOR, "gData.color = texture(gBuffer.color, UV);"],
[GData.DEPTH, "gData.depth = texture(gBuffer.depth, UV).r;"],
[GData.NORMAL, "gData.normal = texture(gBuffer.normal, UV).xyz;"],
[GData.POSITION, "gData.position = getViewPosition(UV, gData.depth);"],
[GData.ORM, "gData.orm = texture(gBuffer.orm, UV).xyz;"],
[GData.EMISSION, "gData.emission = texture(gBuffer.emission, UV).rgb;"],
[GData.LUMINANCE, "gData.luminance = luminance(gData.color.rgb);"]
]);

const gDataDependencies = new ObservableMap<GData | string, Set<GData | string>>([
[GData.POSITION, new Set<GData>([GData.DEPTH])]
]);

const listener = () => this.dispatchEvent({ type: GBufferConfig.EVENT_CHANGE });
textureConfigs.addEventListener(ObservableMap.EVENT_CHANGE, listener);
gBufferStructFields.addEventListener(ObservableMap.EVENT_CHANGE, listener);
Expand All @@ -107,6 +119,7 @@ export class GBufferConfig extends EventDispatcher<BaseEventMap> {
this.gBufferStructDeclaration = gBufferStructDeclaration;
this.gDataStructDeclaration = gDataStructDeclaration;
this.gDataStructInitialization = gDataStructInitialization;
this.gDataDependencies = gDataDependencies;

}

Expand Down

0 comments on commit 7c7803a

Please sign in to comment.