[英]Webgl - Create texture in program 1 and send it to program 2. No texture bound to unit 0
我試圖在我的第一個程序中創建一個Alpha貼圖,以在我的第二個程序中使用。 我覺得我已經正確地設置了所有東西,但是我收到了一個錯誤:
[.Offscreen-For-WebGL-0x7fc3281a4200]RENDER WARNING: there is no texture bound to the unit 0
我為此使用了兩個單獨的類,因為第二個程序有時會在沒有第一個掩蓋的情況下運行。
我的兩個程序都在相同的gl上下文中一個又一個地運行。 我先按第一個程序的順序運行設置代碼,然后按第二個順序運行,然后當然以相同的順序運行繪圖函數。
程序1的設置(創建Alpha貼圖):
// Get gl, add blending ect, then...
// The webgl variable below just holds webgl constants from https://google.github.io/closure-library/api/goog.webgl.html
this.targetTexture_ = this.gl_.createTexture();
this.gl_.activeTexture(webgl.TEXTURE0);
this.gl_.bindTexture(webgl.TEXTURE_2D, this.targetTexture_);
this.gl_.texImage2D(webgl.TEXTURE_2D, 0, webgl.RGBA, this.gl_.canvas.width,
this.gl_.canvas.height, 0, webgl.RGBA, webgl.UNSIGNED_BYTE, null);
this.gl_.texParameteri(webgl.TEXTURE_2D, webgl.TEXTURE_MIN_FILTER, webgl.LINEAR);
this.gl_.texParameteri(webgl.TEXTURE_2D, webgl.TEXTURE_WRAP_S, webgl.CLAMP_TO_EDGE);
this.gl_.texParameteri(webgl.TEXTURE_2D, webgl.TEXTURE_WRAP_T, webgl.CLAMP_TO_EDGE);
this.textureFrameBuffer_ = this.gl_.createFramebuffer();
this.gl_.bindFramebuffer(webgl.FRAMEBUFFER, this.textureFrameBuffer_);
this.gl_.framebufferTexture2D(webgl.FRAMEBUFFER, webgl.COLOR_ATTACHMENT0, webgl.TEXTURE_2D, this.targetTexture_, 0);
// Now i set the maskTexture on the second programs class.
this.secondProgramClass.maskTexture = this.targetTexture_;
// Now i compile & attach the shaders and link the program
然后設置程序2:
// Compile, attach, link up the program...
// This is the texture location to use.
this.maskTextureLocation_ =
this.gl_.getUniformLocation(this.program_, LocationName.MASK);
程序1的繪制功能:
this.gl_.bindFramebuffer(webgl.FRAMEBUFFER, this.textureFrameBuffer_);
this.gl_.bindTexture(webgl.TEXTURE_2D, this.targetTexture_);
this.gl_.viewport(0, 0, this.gl_.canvas.width, this.gl_.canvas.height);
// Clear to transparent
this.gl_.clearColor(0, 0, 0, 0);
this.gl_.clear(webgl.COLOR_BUFFER_BIT | webgl.DEPTH_BUFFER_BIT);
this.gl_.useProgram(this.program_);
// A helper function that runs: enableVertexAttribArray, bindBuffer, vertexAttribPointer on the objects location and buffer.
// In this case its just a square the size of the viewport.
renderBufferAttribute(this.gl_, this.position_);
// Draw triangles
this.gl_.drawArrays(webgl.TRIANGLES, 0, 6);
程序2的繪制功能:
現在,此類已從第一類設置中設置了maskTexture,並且在第一類將其draw函數運行到紋理之后,我應該能夠通過傳遞的紋理來繪制該程序,對嗎?
this.gl_.bindFramebuffer(webgl.FRAMEBUFFER, null);
this.gl_.clearColor(0, 0, 0, 0);
this.gl_.clear(webgl.COLOR_BUFFER_BIT);
this.gl_.useProgram(this.program_);
// A helper function that runs: enableVertexAttribArray, bindBuffer, vertexAttribPointer on the objects location and buffer.
renderBufferAttribute(this.gl_, this.position_);
if (this.maskTexture) {
this.gl_.activeTexture(this.gl_.TEXTURE0);
this.gl_.bindTexture(webgl.TEXTURE_2D, this.maskTexture);
this.gl_.uniform1i(this.maskTextureLocation_, 0);
}
// Draw triangles
this.gl_.drawArrays(webgl.TRIANGLES, 0,
this.particleCount_.total * PARTICLE_ARRAY_COUNT / 2);
此刻的着色器可能與這個問題無關,我們可以假設它們都渲染了一個塊,而第二個塊則期望:
// Stored in LocationName.MASK above.
uniform sampler2D u_mask;
我不認為我會錯過任何東西,但任何方向都將不勝感激。 我在每個繪制周期都收到上述錯誤。 必要時,病情會更新,並提供更多詳細信息。
謝謝!
事實證明,我什至不需要將紋理發送到任何地方,因為它會自動綁定到該webgl上下文中的紋理單元0。 因此,一旦第一個程序繪制到紋理0,第二個程序也已經在紋理0上具有相同的數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.