-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: framework for lambertian importance sampler
- Loading branch information
Showing
10 changed files
with
237 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,13 @@ | ||
precision highp float; | ||
|
||
uniform mat4 viewToWorld; | ||
uniform mat4 screenToView; | ||
varying vec3 v_viewPosition; | ||
varying vec3 v_viewNormal; | ||
|
||
uniform sampler2D equirectangularMap; | ||
|
||
varying vec4 v_homogeneousVertexPosition; | ||
|
||
#pragma include <color/spaces/srgb> | ||
#pragma include <cubemaps/equirectangular> | ||
uniform samplerCube cubeMap; | ||
|
||
void main() { | ||
|
||
// step one, convert from screen space to ray. | ||
vec3 viewPosition = ( viewToWorld * screenToView * v_homogeneousVertexPosition ).xyz; | ||
vec3 viewDirection = normalize( viewPosition ); | ||
|
||
vec2 equirectangularUv = directionToLatLongUV( viewDirection ); | ||
|
||
vec3 outputColor = vec3(0.); | ||
outputColor += sRGBToLinear( texture2D( equirectangularMap, equirectangularUv ).rgb ); | ||
|
||
gl_FragColor.rgb = linearTosRGB( outputColor ); | ||
gl_FragColor.a = 1.0; | ||
vec3 reflectDir = reflect( normalize( v_viewPosition ),normalize( v_viewNormal ) ); | ||
gl_FragColor = textureCube( cubeMap, reflectDir ); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ShaderMaterial } from "../../../../lib/materials/ShaderMaterial"; | ||
import fragmentSource from "./fragment.glsl"; | ||
import vertexSource from "./vertex.glsl"; | ||
|
||
export const samplerMaterial = new ShaderMaterial(vertexSource, fragmentSource); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
precision highp float; | ||
|
||
uniform vec3 color; | ||
|
||
void main() { | ||
|
||
gl_FragColor.rgb = color; | ||
gl_FragColor.a = 1.0; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
attribute vec3 position; | ||
|
||
void main() { | ||
|
||
gl_Position = vec4( position, 1. ); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
attribute vec3 position; | ||
attribute vec3 normal; | ||
|
||
varying vec4 v_homogeneousVertexPosition; | ||
uniform mat4 localToWorld; | ||
uniform mat4 worldToView; | ||
uniform mat4 viewToScreen; | ||
|
||
void main() { | ||
varying vec3 v_viewPosition; | ||
varying vec3 v_viewNormal; | ||
|
||
// homogeneous vertex position | ||
gl_Position.xy = position.xy; | ||
gl_Position.z = -1.; // position at near clipping plane. (set to 1. for far clipping plane.) | ||
gl_Position.w = 1.; // nortmalized | ||
void main() { | ||
|
||
v_homogeneousVertexPosition = gl_Position; | ||
v_viewNormal = normalize( ( worldToView * localToWorld * vec4( normalize( position ), 0. ) ).xyz ); | ||
v_viewPosition = ( worldToView * localToWorld * vec4( position, 1. ) ).xyz; | ||
gl_Position = viewToScreen * vec4( v_viewPosition, 1. ); | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
src/lib/shaders/includes/brdfs/diffuse/lambertSampler.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <math/sampling/hammersley> | ||
#include <normals/tangentSpace> | ||
|
||
vec3 BRDF_Diffuse_Lambert_SampleDirection( vec2 sampleUv ) { | ||
float phi = PI2 * sampleUv.x; | ||
float cosTheta = 0.; | ||
float sinTheta = 0.; | ||
|
||
cosTheta = 1. - sampleUv.y; | ||
sinTheta = sqrt( 1. - cosTheta*cosTheta ); | ||
|
||
return normalize( vec3( sinTheta * cos( phi ), sinTheta * sin( phi ), cosTheta ) ); | ||
} | ||
|
||
float BRDF_Diffuse_Lambert_PDF( | ||
const in vec3 normal, | ||
const in vec3 lightDirection ) { | ||
|
||
float dotNL = saturate( dot( normal, lightDirection ) ); | ||
return dotNL * RECIPROCAL_PI; | ||
} | ||
|
||
vec3 sampleIBL( vec3 direction, float lod ); | ||
|
||
vec3 BRDF_Diffuse_Lambert_Filter(vec3 N, float filterWidth ) { | ||
|
||
vec4 color; | ||
const float solidAngleTexel = 4. * PI / (6. * pow2( filterWidth ); | ||
|
||
for( uint i = 0; i < NUM_SAMPLES; i++ ) { | ||
|
||
vec2 sampleUv = hammersley2( sampleIndex, NUM_SAMPLES ); | ||
vec3 tangentSpaceSampleDirection = BRDF_Diffuse_Lambert_SampleDirection( sampleUv ); | ||
vec3 surfaceSampleDirection = tangentToViewFromNormal( N ) * tangentSpaceSampleDirection; | ||
vec3 H = surfaceSampleDirection; | ||
|
||
// Note: reflect takes incident vector. | ||
// Note: N = V | ||
vec3 V = N; | ||
vec3 L = normalize( reflect( -V, H ) ); | ||
|
||
float dotNL = dot( N, L ); | ||
|
||
if ( dotNL > 0. ) { | ||
float lod = 0.; | ||
|
||
// Mipmap Filtered Samples | ||
// see https://github.com/derkreature/IBLBaker | ||
// see https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch20.html | ||
float pdf = BRDF_Diffuse_Lambert_PDF( N, L ); | ||
|
||
float solidAngleSample = 1.0 / ( float( NUM_SAMPLES ) * pdf ); | ||
|
||
lod = 0.5 * log2( solidAngleSample / solidAngleTexel ); | ||
lod += LOD_BIAS; | ||
|
||
color += vec4( sampleIBL( H, lod ), 1.0 ); | ||
} | ||
} | ||
|
||
return color.rgb / color.w; | ||
} |
Oops, something went wrong.