Skip to content

Commit

Permalink
Merge pull request mrdoob#3 from homelane/feature/parallax_ref
Browse files Browse the repository at this point in the history
feature/parallax_ref
  • Loading branch information
satbirhl authored Mar 13, 2017
2 parents c554fb8 + 4a50714 commit 1f7d425
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 7 deletions.
18 changes: 16 additions & 2 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ function WebGLRenderer( parameters ) {
// progressiveESM
this.progressiveESMEnabled = true;

// parallax reflections
this.enableParallaxReflections = true;
// internal properties

var _this = this,
Expand Down Expand Up @@ -1816,8 +1818,20 @@ function WebGLRenderer( parameters ) {
}

// sao esm buffer
p_uniforms.set( _gl, _this, 'saoBuffer' );
p_uniforms.set( _gl, _this, 'bufferSize' );

if(_this.progressiveSAOEnabled) {
p_uniforms.set( _gl, _this, 'saoBuffer' );
}
if(_this.progressiveSAOEnabled || _this.progressiveESMEnabled) {
p_uniforms.set(_gl, _this, 'bufferSize');
}
//parallax reflection
if(_this.enableParallaxReflections) {
p_uniforms.set( _gl, _this, 'boundingBoxMinMax' );
p_uniforms.set( _gl, _this, 'cubeCameraPos' );
p_uniforms.set( _gl, _this, 'roomDimensions' );
p_uniforms.set( _gl, _this, 'NumBoundingBoxes' );
}

// skinning uniforms must be set even if material didn't change
// auto-setting of texture unit for bone texture must go before other textures
Expand Down
2 changes: 2 additions & 0 deletions src/renderers/shaders/ShaderChunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import gradientmap_pars_fragment from './ShaderChunk/gradientmap_pars_fragment.g
import lightmap_fragment from './ShaderChunk/lightmap_fragment.glsl';
import lightmap_pars_fragment from './ShaderChunk/lightmap_pars_fragment.glsl';
import lights_lambert_vertex from './ShaderChunk/lights_lambert_vertex.glsl';
import parallax_reflection from './ShaderChunk/parallax_reflection.glsl';
import lights_pars from './ShaderChunk/lights_pars.glsl';
import lights_phong_fragment from './ShaderChunk/lights_phong_fragment.glsl';
import lights_phong_pars_fragment from './ShaderChunk/lights_phong_pars_fragment.glsl';
Expand Down Expand Up @@ -145,6 +146,7 @@ export var ShaderChunk = {
lightmap_fragment: lightmap_fragment,
lightmap_pars_fragment: lightmap_pars_fragment,
lights_lambert_vertex: lights_lambert_vertex,
parallax_reflection: parallax_reflection,
lights_pars: lights_pars,
lights_phong_fragment: lights_phong_fragment,
lights_phong_pars_fragment: lights_phong_pars_fragment,
Expand Down
10 changes: 8 additions & 2 deletions src/renderers/shaders/ShaderChunk/lights_pars.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,14 @@ vec3 getAmbientLightIrradiance( const in vec3 ambientLightColor ) {

#elif defined( ENVMAP_TYPE_CUBE_UV )

vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
vec4 envMapColor = textureCubeUV(queryReflectVec, BlinnExponentToGGXRoughness(blinnShininessExponent));
#ifdef PARALLAX_REFLECTIONS_ENABLED
vec3 hitPoint = getClosestHitPoint(reflectVec, vertexWorldPosition);

reflectVec = normalize( hitPoint - cubeCameraPos );
#endif

vec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
vec4 envMapColor = textureCubeUV(queryReflectVec, BlinnExponentToGGXRoughness(blinnShininessExponent));

#elif defined( ENVMAP_TYPE_EQUIREC )

Expand Down
84 changes: 84 additions & 0 deletions src/renderers/shaders/ShaderChunk/parallax_reflection.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#define MAX_BOUNDING_BOXES 10

uniform vec3 boundingBoxMinMax[MAX_BOUNDING_BOXES * 2 ];

uniform vec3 cubeCameraPos;
uniform vec3 roomDimensions;
uniform int NumBoundingBoxes;

struct Ray {
vec3 origin;
vec3 direction;
vec3 inv_direction;
};

Ray makeRay(in vec3 origin, in vec3 direction) {
return Ray(origin, direction, vec3(1.0) / direction);
}

bool intersection(const in vec3 bbmin, const in vec3 bbmax, const in Ray r, inout vec3 hitPoint) {
float tx1 = (bbmin.x - r.origin.x)*r.inv_direction.x;
float tx2 = (bbmax.x - r.origin.x)*r.inv_direction.x;

float tmin = min(tx1, tx2);
float tmax = max(tx1, tx2);

float ty1 = (bbmin.y - r.origin.y) * r.inv_direction.y;
float ty2 = (bbmax.y - r.origin.y) * r.inv_direction.y;

tmin = max(tmin, min(ty1, ty2));
tmax = min(tmax, max(ty1, ty2));

float tz1 = (bbmin.z - r.origin.z) * r.inv_direction.z;
float tz2 = (bbmax.z - r.origin.z) * r.inv_direction.z;

tmin = max(tmin, min(tz1, tz2));
tmax = min(tmax, max(tz1, tz2));

if( tmax >= tmin && tmin > 0.0 ) {
hitPoint = r.origin + tmin * r.direction;
return true;
}
return false;
}

vec3 getClosestHitPoint (vec3 reflectionDir, vec3 vPositionW) {
Ray reflectedRay = makeRay(vPositionW, reflectionDir);

vec3 hitPoint = vec3(100000.0, 100000.0, 100000.0);
vec3 boundingBoxHitPoint = vec3(100000.0, 100000.0, 100000.0);

// Intersect reflected ray with all the bounding boxes and find hitPoint.
for(int i = 0; i < 2 * MAX_BOUNDING_BOXES; i+=2) {
if( i >= 2 * NumBoundingBoxes )
continue;
bool intersects = intersection(boundingBoxMinMax[i], boundingBoxMinMax[i+1], reflectedRay, boundingBoxHitPoint);
if(intersects) {
vec3 d1 = boundingBoxHitPoint - vPositionW;
hitPoint = dot(hitPoint, hitPoint) < dot( d1, d1) ? hitPoint : boundingBoxHitPoint;
}
}

// Intersect reflected ray with the room and compare with the hitPoint. This is needed because we need room relfection.
float roomLength = roomDimensions[0];
float roomWidth = roomDimensions[1];
float roomHeight = roomDimensions[2];

vec3 roomMin = vec3(-roomWidth*0.5, 0.0, -roomLength*0.5);
vec3 roomMax = vec3(roomWidth*0.5, roomHeight, roomLength*0.5);

vec3 rbmax = (roomMax - vPositionW) / reflectionDir;
vec3 rbmin = (roomMin - vPositionW) / reflectionDir;

vec3 rbminmax = max(rbmin, rbmax);

float fa = min(min(rbminmax.x, rbminmax.y), rbminmax.z);

vec3 d1 = hitPoint - vPositionW;
vec3 roomHitPoint = vPositionW + reflectionDir * fa;
vec3 d2 = roomHitPoint - vPositionW;
hitPoint = dot(d1, d1) < dot( d2, d2) ? hitPoint : roomHitPoint;

return hitPoint;

}
2 changes: 2 additions & 0 deletions src/renderers/shaders/ShaderLib/meshphysical_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ uniform float opacity;
#endif

varying vec3 vViewPosition;
varying vec3 vertexWorldPosition;

#ifndef FLAT_SHADED

Expand All @@ -33,6 +34,7 @@ varying vec3 vViewPosition;
#include <fog_pars_fragment>
#include <bsdfs>
#include <cube_uv_reflection_fragment>
#include <parallax_reflection>
#include <lights_pars>
#include <lights_physical_pars_fragment>
#include <shadowmap_pars_fragment>
Expand Down
2 changes: 2 additions & 0 deletions src/renderers/shaders/ShaderLib/meshphysical_vert.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#define PHYSICAL

varying vec3 vViewPosition;
varying vec3 vertexWorldPosition;

#ifndef FLAT_SHADED

Expand Down Expand Up @@ -47,6 +48,7 @@ void main() {
#include <clipping_planes_vertex>

vViewPosition = - mvPosition.xyz;
vertexWorldPosition = (modelMatrix * vec4(transformed, 1.0)).xyz;

#include <worldpos_vertex>
#include <shadowmap_vertex>
Expand Down
2 changes: 1 addition & 1 deletion src/renderers/webgl/WebGLProgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ function WebGLProgram( renderer, code, material, parameters ) {

parameters.progressiveSAOEnabled ? "#define PROGRESSIVE_SAO_ENABLED " : '',
parameters.progressiveESMEnabled ? "#define PROGRESSIVE_ESM_ENABLED " : '',

parameters.enableParallaxReflections ? "#define PARALLAX_REFLECTIONS_ENABLED " : '',
'\n'

].filter( filterEmptyLine ).join( '\n' );
Expand Down
5 changes: 3 additions & 2 deletions src/renderers/webgl/WebGLPrograms.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function WebGLPrograms( renderer, capabilities ) {
"numDirLights", "numPointLights", "numSpotLights", "numHemiLights", "numRectAreaLights",
"shadowMapEnabled", "shadowMapType", "toneMapping", 'physicallyCorrectLights',
"alphaTest", "doubleSided", "flipSided", "numClippingPlanes", "numClipIntersection", "depthPacking",
"progressiveSAOEnabled", "progressiveESMEnabled"
"progressiveSAOEnabled", "progressiveESMEnabled", 'enableParallaxReflections'
];


Expand Down Expand Up @@ -204,7 +204,8 @@ function WebGLPrograms( renderer, capabilities ) {

depthPacking: ( material.depthPacking !== undefined ) ? material.depthPacking : false,
progressiveSAOEnabled : renderer.progressiveSAOEnabled,
progressiveESMEnabled : renderer.progressiveESMEnabled
progressiveESMEnabled : renderer.progressiveESMEnabled,
enableParallaxReflections : renderer.enableParallaxReflections

};

Expand Down

0 comments on commit 1f7d425

Please sign in to comment.