【发布时间】:2020-03-04 00:47:09
【问题描述】:
致读者: “gman 的解决方案适用于该问题,但我自己的问题是代码的不同部分”。
我只是想将我的场景渲染到带有 DepthComponent_textuture 的帧缓冲区。并且想将此纹理渲染到屏幕上,以便我在我的屏幕上看到 DepthComponent。我想进入 Shadowmapping 并通过这将是一个很好的练习。
我做过的事情:
-set read/drawBuffer(s) 到 gl.NONE 因为我没有 colorAttachment(甚至可能不重要)。
- 创建深度纹理时,我尝试了gl.texImage2D,但无法让它工作。
- 当我使用 DepthAttachment 的 ColorAttachment 时,我得到一个正常的结果(颜色 Attachment_texture 只是有屏幕)[显然这里我还添加了一个 DepthRenderbuffer(DEPTH24STENCTIL8)]。
//creating the Framebuffer
id = gl.createFramebuffer();
gl.bindFramebuffer(gl.FRAMEBUFFER,id);
atex = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, atex);
gl.texStorage2D(gl.TEXTURE_2D,1,gl.DEPTH_COMPONENT16,width,height);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
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.framebufferTexture2D(gl.FRAMEBUFFER, gl.DEPTH_ATTACHMENT, gl.TEXTURE_2D, atex, 0);
gl.drawBuffers([gl.NONE]);
gl.readBuffer(gl.NONE);
gl.bindFramebuffer(gl.FRAMEBUFFER,null);
//----------------
//In the renderLoop
//renders the scene with the pbrShader to the Framebuffer
//The FragmentShader of the PBRShader is empty cause i dont have any
//ColorAttachments and the Vertex is just transforming the position and
//passing the uvs[...]
render(scene,pbrShader,framebuffer);
//renders a texture to the screen using a very simple vertex shader and
//a Fragment Shader that just takes the texture and mapps it to the Screen
renderScreen(framebuffer.atex);
//Some more Code for clarity:
renderScene(scene,shader,fbo=null){
if(fbo!=null){
fbo.activate();
}
this.gl.fClear();
let batches = new Map();
let modals = scene.getEntitiesByType("modal");
modals.forEach(modal=>{
if(batches.has(modal.mesh)){
batches.get(modal.mesh).push(modal);
}else{
batches.set(modal.mesh,[modal]);
}
})
shader.activate().prepareScene(scene);
batches.forEach((batch,mesh)=>{
shader.prepareVAO(mesh);
batch.forEach(modal=>{
shader.prepareInstance(modal);
shader.drawVAO(mesh);
});
shader.unbindVAO(mesh);
});
shader.deactivate();
if(fbo!=null){
fbo.deactivate();
}
}
【问题讨论】:
-
是的,我确实清除了缓冲区。现在我在绑定 Framebuffer 时也再次启用了 DepthTesting(我认为我真的不需要这样做,因为我在 gl 初始化时已经这样做了)。
-
是的,我在 renderScene() 中做了,我总是在开始渲染之前清除当前绑定的帧缓冲区
-
您是否在检查 JavaScript 控制台是否有错误?您是否检查并启用了深度纹理扩展?深度纹理不是 WebGL1 的默认功能。 gl.DEPTH_COMPONENT16 不是有效的纹理格式。 gl.DEPTH_COMPONENT 仅当您检查并启用了 WEBGL_depth_texture 扩展
-
我的控制台很干净,我第一次尝试使用 texImage2d 对 depthTexture 进行纹理处理,但控制台中的纹理格式无效。如何启用 WEBGL_depth_texture
-
你的意思是
let x = gl.getExtension('WEBGL_depth_texture');我应该在哪里添加这个
标签: javascript framebuffer webgl2