Skip to content

Commit

Permalink
Z-Buffer made (Almost production ready!)
Browse files Browse the repository at this point in the history
  • Loading branch information
David-Orangemoon authored Aug 21, 2023
1 parent 5244b95 commit 8821d58
Showing 1 changed file with 63 additions and 7 deletions.
70 changes: 63 additions & 7 deletions extensions/obviousAlexC/penPlus.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

//?Make a function for updating the depth canvas to fit the scratch stage
const depthFrameBuffer = gl.createFramebuffer();
const depthColorBuffer = gl.createRenderbuffer();
const depthDepthBuffer = gl.createRenderbuffer();

let lastFB = gl.getParameter(gl.FRAMEBUFFER_BINDING);
Expand Down Expand Up @@ -55,12 +56,19 @@
gl.activeTexture(gl.TEXTURE0);

gl.bindFramebuffer(gl.FRAMEBUFFER, depthFrameBuffer);
gl.framebufferTexture2D(

gl.bindRenderbuffer(gl.RENDERBUFFER, depthColorBuffer);
gl.renderbufferStorage(
gl.RENDERBUFFER,
gl.RGBA8 || gl.RGBA4,
nativeSize[0],
nativeSize[1]
);
gl.framebufferRenderbuffer(
gl.FRAMEBUFFER,
gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D,
depthBufferTexture,
0
gl.RENDERBUFFER,
depthColorBuffer
);

gl.bindRenderbuffer(gl.RENDERBUFFER, depthDepthBuffer);
Expand All @@ -77,6 +85,14 @@
depthDepthBuffer
);

gl.framebufferTexture2D(
gl.FRAMEBUFFER,
gl.COLOR_ATTACHMENT0,
gl.TEXTURE_2D,
depthBufferTexture,
0
);

gl.enable(gl.DEPTH_TEST);

gl.bindFramebuffer(gl.FRAMEBUFFER, lastFB);
Expand All @@ -89,6 +105,15 @@
lastFB = gl.getParameter(gl.FRAMEBUFFER_BINDING);

gl.bindFramebuffer(gl.FRAMEBUFFER, depthFrameBuffer);

gl.bindRenderbuffer(gl.RENDERBUFFER, depthColorBuffer);
gl.renderbufferStorage(
gl.RENDERBUFFER,
gl.RGBA8 || gl.RGBA4,
nativeSize[0],
nativeSize[1]
);

gl.bindRenderbuffer(gl.RENDERBUFFER, depthDepthBuffer);
gl.renderbufferStorage(
gl.RENDERBUFFER,
Expand All @@ -97,17 +122,48 @@
nativeSize[1]
);

gl.activeTexture(gl.TEXTURE1);

gl.texImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
nativeSize[0],
nativeSize[1],
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
null
);

console.log(nativeSize)

gl.activeTexture(gl.TEXTURE0);

gl.bindFramebuffer(gl.FRAMEBUFFER, lastFB);
};

//?Call it to have it consistant
updateCanvasSize();

//?Call every frame because I don't know of a way to detect when the stage is resized
vm.runtime.on("BEFORE_EXECUTE", () => {

window.addEventListener("resize", updateCanvasSize);
vm.runtime.on("STAGE_SIZE_CHANGED", () => {
updateCanvasSize();
});

let lastURL = window.location.href;
let lastHQPen = renderer.useHighQualityRender;

vm.runtime.on("BEFORE_EXECUTE", () => {
if (lastURL != window.location.href || lastHQPen != renderer.useHighQualityRender) {
lastURL = window.location.href;
lastHQPen = renderer.useHighQualityRender;
updateCanvasSize();
}
});

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

Expand Down Expand Up @@ -191,7 +247,7 @@
gl_FragColor = v_color;
highp vec4 v_depthPart = texture2D(u_depthTexture,gl_FragCoord.xy/u_res);
highp float v_depthcalc = (v_depthPart.r+v_depthPart.g+v_depthPart.b)/3.0;
if (v_depth > v_depthcalc){
if (v_depth >= v_depthcalc - 0.075){
gl_FragColor.a = 0.0;
}
gl_FragColor.rgb *= gl_FragColor.a;
Expand Down Expand Up @@ -236,7 +292,7 @@
gl_FragColor = texture2D(u_texture, v_texCoord) * v_color;
highp vec4 v_depthPart = texture2D(u_depthTexture,gl_FragCoord.xy/u_res);
highp float v_depthcalc = (v_depthPart.r+v_depthPart.g+v_depthPart.b)/3.0;
if (v_depth > v_depthcalc){
if (v_depth >= v_depthcalc - 0.075){
gl_FragColor.a = 0.0;
}
gl_FragColor.rgb *= gl_FragColor.a;
Expand Down

0 comments on commit 8821d58

Please sign in to comment.