Skip to content

Commit

Permalink
examples: postprocessing material - use gl_FragCoord.xy for map lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
Rabbid76 committed Dec 31, 2023
1 parent 4ae2369 commit 7819ad8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions examples/jsm/materials/MeshPostProcessingMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ class MeshPostProcessingMaterial extends MeshPhysicalMaterial {
constructor( parameters ) {

const aoPassMap = parameters.aoPassMap;
const aoPassMapScale = parameters.aoPassMapScale || 1.0;
delete parameters.aoPassMap;
delete parameters.aoPassMapScale;

super( parameters );

this.onBeforeCompile = this._onBeforeCompile;
this._aoPassMap = aoPassMap;
this.aoPassMapScale = aoPassMapScale;

}

Expand All @@ -53,44 +57,46 @@ class MeshPostProcessingMaterial extends MeshPhysicalMaterial {

if ( this._aoPassMap !== undefined ) {

shader.vertexShader = shader.vertexShader.replace(
'#include <common>',
`varying vec4 vClipSpacePosition;
#include <common>`
);
shader.vertexShader = shader.vertexShader.replace(
'#include <fog_vertex>',
`#include <fog_vertex>
vClipSpacePosition = gl_Position;`
);
shader.fragmentShader = shader.fragmentShader.replace(
'#include <common>',
`varying vec4 vClipSpacePosition;
uniform sampler2D tAoPassMap;
#include <common>`
'#include <aomap_pars_fragment>',
aomap_pars_fragment_replacement
);
shader.fragmentShader = shader.fragmentShader.replace(
'#include <aomap_fragment>',
aomap_fragment_replacement
);
shader.uniforms.tAoPassMap = { value: this._aoPassMap };
shader.uniforms.aoPassMapScale = { value: this.aoPassMapScale };

}

}

}

const aomap_pars_fragment_replacement = /* glsl */`
#ifdef USE_AOMAP
uniform sampler2D aoMap;
uniform float aoMapIntensity;
#endif
uniform sampler2D tAoPassMap;
uniform float aoPassMapScale;
`;

const aomap_fragment_replacement = /* glsl */`
#ifndef AOPASSMAP_SWIZZLE
#define AOPASSMAP_SWIZZLE r
#endif
float ambientOcclusion = texture2D( tAoPassMap, vClipSpacePosition.xy / vClipSpacePosition.w * 0.5 + 0.5 ).AOPASSMAP_SWIZZLE;
float ambientOcclusion = texelFetch( tAoPassMap, ivec2( gl_FragCoord.xy * aoPassMapScale ), 0 ).AOPASSMAP_SWIZZLE;
#ifdef USE_AOMAP
// reads channel R, compatible with a combined OcclusionRoughnessMetallic (RGB) texture
ambientOcclusion *= ( texture2D( aoMap, vAoMapUv ).r - 1.0 ) * aoMapIntensity + 1.0;
ambientOcclusion = min( ambientOcclusion, texture2D( aoMap, vAoMapUv ).r );
ambientOcclusion *= ( ambientOcclusion - 1.0 ) * aoMapIntensity + 1.0;
#endif
Expand Down
Binary file modified examples/screenshots/webgl_postprocessing_material_ao.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7819ad8

Please sign in to comment.