TSL and Deferred Shading

Hello everyone,

I’m experimenting with TSL and deferred shading, but I’m not quite sure how to properly compute lighting using the built-in functionality.

Let’s say I’m generating the GBuffer textures like this:

  const scenePass = pass(scene, camera, { minFilter: Core.NearestFilter, magFilter: Core.NearestFilter });
  scenePass.setMRT(TSL.mrt({
      output: TSL.output,
      diffuse: TSL.diffuseColor,
      normal: TSL.transformedNormalView,
      position: TSL.positionView,
  }));
  
  const diffuseNode = scenePass.getTextureNode('diffuse');
  const normalNode = scenePass.getTextureNode('normal');
  const posNode = scenePass.getTextureNode('position');

Now, theoretically, I could pass these into a custom node for composing the final image, like this:

class ComposeNode extends Core.TempNode {

    static get type() {
        return 'ComposeNode';
    }

    constructor(diffuseNode, normalNode, positionNode) {
        super('vec4');
        this.diffuseNode = diffuseNode;
        this.normalNode = normalNode;
        this.positionNode = positionNode;
    }

    setup() {
        // test
        const test = TSL.Fn(() => {
            const positionView = this.positionNode.xyz;
            const normalView = this.normalNode.xyz;
            return TSL.vec4(normalView, 1.0);
        });

        return test();
    }
}

export const compose = TSL.nodeProxy(ComposeNode);

Of course, I could compute lighting manually using light() and custom formulas, but it looks like there are already existing solutions such as PhongLightingModel.

So my question is:

Is there a way to reuse existing lighting models (like PhongLightingModel) in this deferred context, or better yet, use something like MeshPhongNodeMaterial to compute the final lighting — while feeding it the position/normal data from my buffer instead of using forward rendering?

Any guidance or examples would be highly appreciated!

Thanks

Pinging @sunag in case you have any insight — since this seems closely related to your work on three-nodes and TSL.

1 Like

I think you will also need to create a rendere.lighting similar to what was done in webgpu_lights_tiled in this case to disable all lights during the forward renderer and do the light calculation in post-production.