Skip to content

Commit

Permalink
Basic sampling of the IBL against the Disney BRDF.
Browse files Browse the repository at this point in the history
  • Loading branch information
ScatteredRay committed Jul 12, 2016
1 parent 974422c commit bfdb8bc
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
2 changes: 2 additions & 0 deletions shaders/disney.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ vec3 BRDF( vec3 L, vec3 V, vec3 N, vec3 X, vec3 Y )
float clearcoat = 0.0;
float clearcoatGloss = 1.0;

baseColor = mat_albedo();

float NdotL = dot(N,L);
float NdotV = dot(N,V);
if (NdotL < 0.0 || NdotV < 0.0) return vec3(0.0);
Expand Down
47 changes: 47 additions & 0 deletions shaders/montecarlo.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#if 0
float VDCRadicalInverse(int bits)
{
bits = (bits << 16) | (bits >> 16);
bits = ((bits & 0x55555555) << 1) | ((bits & 0xAAAAAAAA) >> 1);
bits = ((bits & 0x33333333) << 2) | ((bits & 0xCCCCCCCC) >> 2);
bits = ((bits & 0x0F0F0F0F) << 4) | ((bits & 0xF0F0F0F0) >> 4);
bits = ((bits & 0x00FF00FF) << 8) | ((bits & 0xFF00FF00) >> 8);
return float(bits) * 2.3283064365386963e-10;
}

float Hammersley2(int i, int n)
{
return vec2(float(i) / float(n), VDCRadicalInverse(i));
}
#endif

float rand(vec2 co)
{
return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453);
}

vec2 rand2(vec2 co)
{
return vec2(rand(co), rand(vec2(co.x, co.y * 24.7676)));
}

vec4 CosWeightedHemisphereTangentSpace(vec2 e)
{
float r = sqrt(e.x);
float th = 2.0 * PI * e.y;

float pdf = 0.0001 + r / PI;

return vec4(
r * cos(th),
r * sin(th),
sqrt(max(0.0, 1.0 - e.x)),
pdf);
}


vec4 CosWeightedHemisphere(vec2 e, vec3 normal, vec3 tangent, vec3 bitangent)
{
vec4 h = CosWeightedHemisphereTangentSpace(e);
return vec4(tangent * h.x + bitangent * h.y + normal * h.z, h.w);
}
23 changes: 22 additions & 1 deletion shaders/surface_frag.glsl
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#include "disney.glsl"
#include "montecarlo.glsl"
varying vec2 vUv;
varying vec3 vWorldNormal;
varying vec3 vScreenNormal;
varying vec3 vWorldTangent;
varying vec3 vWorldBitangent;
varying vec3 vWorldPos;
varying vec4 vScreenPos;
varying vec3 vReflect;

uniform samplerCube envMap;
uniform float instRand;

struct DirectionalLight
{
Expand All @@ -31,6 +34,24 @@ void main()
color += clamp(dot(lightVector, vWorldNormal), 0.0, 1.0) * directionalLights[i].color * albedo;
//color += BRDF(lightVector, vec3(0, 0, -1), vNormal, vec3(0, 1, 0), vec3(1, 0, 1)) * directionalLights[i].color;
}
color = textureCube(envMap, vReflect).rgb * albedo;
// We generate tangents for percision.
vec3 nz = normalize(vWorldNormal);
vec3 nx = normalize(cross(nz, vWorldTangent));
vec3 ny = normalize(cross(nx, nz));
color = vec3(0.0, 0.0, 0.0);
const int numSamples = 32;
for(int s = 0; s < numSamples; s++) {
vec2 r = rand2(vScreenPos.xy + vec2(float(s) / 17.456, instRand));
vec4 d = CosWeightedHemisphere(r, nz, nx, ny);
vec3 V = normalize(cameraPosition - vWorldPos);
float pdf = d.w;
vec3 l = BRDF(d.xyz, V, nz, nx, ny) / pdf;
//color += l * textureCube(envMap, d.xyz).rgb;// / float(numSamples);
color += textureCube(envMap, d.xyz).rgb * l / float(numSamples);
//color += dot(d.xyz, nz) / float(numSamples);
//break;
}

//color = textureCube(envMap, vReflect).rgb * albedo;
gl_FragColor = vec4(color, 1.0);
}
6 changes: 4 additions & 2 deletions shaders/surface_vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ varying vec3 vScreenNormal;
varying vec3 vWorldTangent;
varying vec3 vWorldBitangent;
varying vec3 vWorldPos;
varying vec4 vScreenPos;
varying vec3 vReflect;

void main()
Expand All @@ -15,8 +16,9 @@ void main()
vWorldNormal = (modelMatrix * vec4(normal, 0.0)).xyz;
vScreenNormal = normalize((normalMatrix * normal).xyz);
vWorldTangent = normalize((modelMatrix * vec4(tangent.xyz, 0.0)).xyz);
vWorldBitangent = cross(vWorldNormal, vWorldTangent);
vWorldBitangent = normalize(cross(vWorldNormal, vWorldTangent));
vWorldTangent = cross(vWorldTangent, vWorldNormal);
vec3 cameraToVertex = normalize(vWorldPos.xyz - cameraPosition);
vReflect = reflect(cameraToVertex, vWorldNormal);
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
gl_Position = vScreenPos = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
3 changes: 3 additions & 0 deletions webgl/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var camera;
var scene;
var renderer;
var controls;
var material;

var mouseX = 0;
var mouseY = 0;
Expand Down Expand Up @@ -42,6 +43,7 @@ function init() {

var material = create_materialx_shadermaterial("/data/Materials/default.mtlx", "default");
material.uniforms.envMap = {type: 't', value: IBL};
material.uniforms.instRand = {type: 'f', value: 0.0};

var loader = new THREE.OBJLoader(manager);
loader.load('/data/Meshes/Suzanne.obj', function(object) {
Expand Down Expand Up @@ -183,6 +185,7 @@ function onWindowResize() {
}*/

function animate() {
material.uniforms.instRand.value = Math.random();
//requestAnimationFrame(animate);
render();
}
Expand Down

0 comments on commit bfdb8bc

Please sign in to comment.