Skip to content

Commit

Permalink
fix depth issue
Browse files Browse the repository at this point in the history
  • Loading branch information
colintrinity committed Dec 22, 2017
1 parent a0e73fb commit cad18bd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/S0.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class S0 {
canvas.height = window.innerHeight;
document.body.appendChild(canvas);

let gl = canvas.getContext('webgl2', { antialias: true });
let gl = canvas.getContext('webgl2', { antialias: false });
let isWebGL2 = !!gl;
if (!isWebGL2) {
console.warn('WebGL 2 is not available. See https://www.khronos.org/webgl/wiki/Getting_a_WebGL_Implementation How to get a WebGL 2 implementation');
Expand Down
90 changes: 59 additions & 31 deletions src/renderers/DeferredRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,31 @@ export default class DeferredRenderer extends Renderer {
}

gl.bindTexture(gl.TEXTURE_2D, null);
gl.bindRenderbuffer(gl.RENDERBUFFER, null);
// gl.bindRenderbuffer(gl.RENDERBUFFER, null);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);

this._compositeBuffer = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER, this._compositeBuffer);
this._compositeTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, this._compositeTexture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA16F,
this._viewWith,
this._viewHeight,
0,
gl.RGBA,
gl.FLOAT,
null
);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, this._compositeTexture, 0);
gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, this._depthTexture, 0);
gl.bindTexture(gl.TEXTURE_2D, null);
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}

Expand All @@ -157,28 +181,32 @@ export default class DeferredRenderer extends Renderer {

if (scenes.length && IBLManager.isReady) {
this.geometryPass(scenes, camera);

gl.bindFramebuffer(gl.FRAMEBUFFER, this._compositeBuffer);
gl.disable(gl.DEPTH_TEST);
//gl.clear(gl.COLOR_BUFFER_BIT);
this.lightingPass();

// gl.enable(gl.DEPTH_TEST);
// gl.depthFunc(gl.LEQUAL);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);

// let MVP = mat4.create();
// mat4.copy(MVP, camera.view);
// MVP[12] = 0.0;
// MVP[13] = 0.0;
// MVP[14] = 0.0;
// MVP[15] = 1.0;
// mat4.mul(MVP, camera.projection, MVP);
// this._shaderSkybox.use();
// this._shaderSkybox.setMat4('uMVP', MVP);
// this._shaderSkybox.setInt('u_environment', 0);
// // IBLManager.activeAndBindTextures();
// gl.activeTexture(gl.TEXTURE0);
// gl.bindTexture(gl.TEXTURE_CUBE_MAP, IBLManager.specularEnvSampler);
// this._cube.draw();
// }
let MVP = mat4.create();
mat4.copy(MVP, camera.view);
MVP[12] = 0.0;
MVP[13] = 0.0;
MVP[14] = 0.0;
MVP[15] = 1.0;
mat4.mul(MVP, camera.projection, MVP);
this._shaderSkybox.use();
this._shaderSkybox.setMat4('uMVP', MVP);
this._shaderSkybox.setInt('u_environment', 0);
// IBLManager.activeAndBindTextures();
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_CUBE_MAP, IBLManager.specularEnvSampler);
this._cube.draw();
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
}
// this.composite();
this.composite();
}

geometryPass(scenes, camera) {
Expand Down Expand Up @@ -229,21 +257,21 @@ export default class DeferredRenderer extends Renderer {
composite() {
this._shaderCompositePass.use();

this._shaderCompositePass.setInt("gPosition", 0);
this._shaderCompositePass.setInt("gNormal", 1);
this._shaderCompositePass.setInt("gAlbedoSpec", 2);
this._shaderCompositePass.setInt("gMetallicRoughness", 3);
this._shaderCompositePass.setInt("depthTexture", 4);
this._shaderCompositePass.setInt("compositeTexture", 0);
this._shaderCompositePass.setInt("depthTexture", 1);
// this._shaderCompositePass.setInt("gAlbedoSpec", 2);
// this._shaderCompositePass.setInt("gMetallicRoughness", 3);
// this._shaderCompositePass.setInt("depthTexture", 4);

gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, this._gPosition);
gl.bindTexture(gl.TEXTURE_2D, this._compositeTexture);
gl.activeTexture(gl.TEXTURE1);
gl.bindTexture(gl.TEXTURE_2D, this._gNormal);
gl.activeTexture(gl.TEXTURE2);
gl.bindTexture(gl.TEXTURE_2D, this._gAlbedoSpec);
gl.activeTexture(gl.TEXTURE3);
gl.bindTexture(gl.TEXTURE_2D, this._gMetallicRoughness);
gl.activeTexture(gl.TEXTURE4);
// gl.bindTexture(gl.TEXTURE_2D, this._gNormal);
// gl.activeTexture(gl.TEXTURE2);
// gl.bindTexture(gl.TEXTURE_2D, this._gAlbedoSpec);
// gl.activeTexture(gl.TEXTURE3);
// gl.bindTexture(gl.TEXTURE_2D, this._gMetallicRoughness);
// gl.activeTexture(gl.TEXTURE4);
gl.bindTexture(gl.TEXTURE_2D, this._depthTexture);

this._quad.draw();
Expand Down
12 changes: 6 additions & 6 deletions src/shaders/deferred/composite.fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
precision highp float;
precision highp int;

in vec2 vTexCoords;
in vec2 vTexCoord;

layout(location = FRAG_COLOR_LOCATION) out vec4 color;

uniform sampler2D gPosition;
uniform sampler2D gNormal;
uniform sampler2D gAlbedoSpec;
uniform sampler2D gMetallicRoughness;
uniform sampler2D compositeTexture;
// uniform sampler2D gNormal;
// uniform sampler2D gAlbedoSpec;
// uniform sampler2D gMetallicRoughness;
uniform sampler2D depthTexture;

void main()
{
color = vec4(vec3(texture(gNormal, vTexCoords)), 1.0);
color = vec4(vec3(texture(compositeTexture, vTexCoord)), 1.0);
}
6 changes: 3 additions & 3 deletions src/shaders/deferred/composite.vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ precision highp float;
precision highp int;

layout(location = POSITION_LOCATION) in vec3 aPosition;
layout(location = TEXCOORD_LOCATION) in vec2 aTexCoords;
layout(location = TEXCOORD_LOCATION) in vec2 aTexCoord;

out vec2 vTexCoords;
out vec2 vTexCoord;

void main()
{
gl_Position = vec4(aPosition, 1.0) ;
vTexCoords = aTexCoords;
vTexCoord = aTexCoord;
}

0 comments on commit cad18bd

Please sign in to comment.