javascript - 如何将 Three.js 纹理作为 "WebGLTexture"传递给 WEBGL 代码?

标签 javascript three.js webgl textures

我试图将 Three.js 纹理传递给 gl.texSubImage WEBGL 函数,但出现以下错误:

在 Chrome 上:

Uncaught TypeError line

在火狐浏览器上:

enter image description here

代码如下 - 目标纹理2被传递到下面的gl.bindTexture并且不被识别。源texture1是加载图像的纹理。

我尝试过:texture2、texture2.image、texture2.image.data、 两者都具有数据纹理和普通纹理,并加载图像作为纹理2。 有什么想法吗?

var gl = renderer.getContext();
var position = new THREE.Vector2(0,0);

renderer.setTexture2D( texture2, 0 ); 

gl.bindTexture(gl.TEXTURE_2D, texture2.image.data); //<<< problem

gl.texSubImage2D( gl.TEXTURE_2D, 
                  0, 
                  position.x, 
                  position.y, 
                  gl.RGB, 
                  gl.UNSIGNED_BYTE, 
                  texture1.image);

最佳答案

我建议你看看WebGLRenderer.copyTextureToTexture() 。它在内部使用texSubImage。还有一个示例展示了该方法的用法:

https://threejs.org/examples/webgl_materials_texture_partialupdate.html

顺便说一句:在 bindTexture 中使用 image.data 不起作用,因为 API 调用需要 WebGLTexture 对象作为第二个参数。您可以像这样访问纹理的原始 WebGLTexture 对象。

var textureProperties = renderer.properties.get( texture );
console.log( textureProperties.__webglTexture );

关于javascript - 如何将 Three.js 纹理作为 "WebGLTexture"传递给 WEBGL 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51091253/

相关文章:

javascript - JS 打印 - CSS 文本颜色未显示

javascript - 将值转换为可读的小时数

javascript - 选择没有最后一个空格的潜水文本

javascript - 使用 iframe 检测不活动状态

javascript - Collada 到 JSON

javascript - highcharts IE8重绘图表问题

javascript - Three.js 多边形/多面体名称或标题

javascript - Three.js - 通过鼠标单击获取对象名称

WebGL 渲染缓冲区

javascript - 如何让 webglcontextrestored 被解雇