【问题标题】:Webgl texture flip horizontallyWebgl 纹理水平翻转
【发布时间】:2020-01-22 03:52:18
【问题描述】:

我想知道有没有webgl纹理水平翻转的功能。

我已经在谷歌上搜索过这个问题。 但是,如您所知,我使用 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true); 得到了垂直翻转纹理的答案。

我还尝试编写一个简单的函数来切换像素值以获得我想要的翻转纹理。 但是我还是想知道有什么方法可以直接水平翻转webgl纹理?

【问题讨论】:

  • 向后画?翻转纹理坐标?没有简单的方法来翻转数据本身
  • 我只是尝试 gl.readPixels 先获取纹理数据,然后使用 for 循环交换像素值。只是想知道有没有什么方法可以快速翻转纹理。
  • 或者我可以尝试先翻转图像然后绑定到纹理。

标签: javascript textures webgl


【解决方案1】:

更好的问题是你想做什么。你说你想翻转纹理。为什么?你想完成什么?

有很多方法可以绘制翻转的纹理。绘制某个方向不需要翻转纹理的数据。假设您正在绘制一个带有纹理的矩形。你可以

  • 绘制翻转的矩形。
  • 在 X 中按 -1 缩放。
  • 更改数据中的纹理坐标
  • 在着色器中操作纹理坐标

所有这些都可以说比尝试实际翻转纹理数据更快、更有用。今天你问如何x-flip数据。明天你会问如何旋转数据。您通常不会 x 翻转或旋转纹理,而是操纵顶点和/或纹理坐标。

再说一遍,你认为 x 翻转纹理数据会解决什么问题?

旧的固定函数 OpenGL 有一个纹理矩阵,对应于上面的最后一个想法。你可能有这样的顶点着色器

attribute vec4 position;
attribute vec2 texcoord;

uniform mat4 matrix;

varying vec2 v_texcoord;

void main() {
  gl_Position = matrix * position;

  // pass through to fragment shader
  v_texcoord = texcoord;  
}

你可以像这样添加一个纹理矩阵

attribute vec4 position;
attribute vec2 texcoord;

uniform mat4 matrix;
uniform mat4 textureMatrix;  // ADDED!!

varying vec2 v_texcoord;

void main() {
  gl_Position = matrix * position;

  // pass through to fragment shader after
  // multiplying by texture matrix
  v_texcoord = (textureMatrix * vec4(texcoord, 0, 1)).xy;  // CHANGED !!
}

这是一个使用纹理矩阵以多种方式绘制相同纹理的示例

var m4 = twgl.m4;
var gl = twgl.getWebGLContext(document.getElementById("c"));
var programInfo = twgl.createProgramInfo(gl, ["vs", "fs"]);

var arrays = {
  position: { 
    numComponents: 2,
    data: [
      0, 0, 
      1, 0, 
      0, 1, 
      0, 1, 
      1, 0, 
      1, 1,
    ],
  },
  texcoord: [
      0, 0, 
      1, 0, 
      0, 1, 
      0, 1, 
      1, 0, 
      1, 1,
  ],
};
var bufferInfo = twgl.createBufferInfoFromArrays(gl, arrays);
  
// make a texture from a 2d canvas. We'll make an F so we can see it's orientation
var ctx = document.createElement("canvas").getContext("2d");
ctx.width = 64;
ctx.height = 64;
ctx.fillStyle = "red";
ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height / 2);
ctx.fillStyle = "blue";
ctx.fillRect(0, ctx.canvas.height / 2, ctx.canvas.width, ctx.canvas.height / 2);
ctx.fillStyle = "yellow";
ctx.font = "100px sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("F", ctx.canvas.width / 2, ctx.canvas.height / 2);
  
var tex = twgl.createTexture(gl, { src: ctx.canvas });

var uniforms = {
  matrix: m4.identity(),
  textureMatrix: m4.identity(),
  texture: tex,
};

gl.useProgram(programInfo.program);
twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);

for (var ii = 0; ii < 10; ++ii) {
  var flipX = ii & 0x1;
  var flipY = ii & 0x2;
  var swap  = ii & 0x4;
  var rot   = ii & 0x8;
  var x     = ii % 5;
  var y     = ii / 5 | 0;
 
  var m = uniforms.matrix;
  m4.ortho(0, gl.canvas.width, gl.canvas.height, 0, -1, 1, m);
  m4.translate(m, [x * 59 + 2, y * 59 + 2, 0], m);
  m4.scale(m, [58, 58, 1], m);
  
  var tm = uniforms.textureMatrix;
  m4.identity(tm);
  if (flipX) {
    m4.translate(tm, [1, 0, 0], tm);
    m4.scale(tm, [-1, 1, 1], tm);
  }
  if (flipY) {
    m4.translate(tm, [0, 1, 0], tm);
    m4.scale(tm, [1, -1, 1], tm);
  }
  if (swap) {
    m4.multiply(tm, [
      0, 1, 0, 0,
      1, 0, 0, 0,
      0, 0, 1, 0,
      0, 0, 0, 1,
    ], tm);
  }
  if (rot) {
   m4.translate(tm, [0.5, 0.5, 0], tm);
   m4.rotateZ(tm, Math.PI * 0.25, tm);
   m4.translate(tm, [-0.5, -0.5, 0], tm);
  }  
  twgl.setUniforms(programInfo, uniforms);
  twgl.drawBufferInfo(gl, gl.TRIANGLES, bufferInfo);
}
canvas { border: 1px solid black; }
  <script id="vs" type="notjs">
attribute vec4 position;
attribute vec2 texcoord;

uniform mat4 matrix;
uniform mat4 textureMatrix;

varying vec2 v_texcoord;

void main() {
  gl_Position = matrix * position;
  v_texcoord = (textureMatrix * vec4(texcoord, 0, 1)).xy;
}
  </script>
  <script id="fs" type="notjs">
precision mediump float;

uniform sampler2D texture;

varying vec2 v_texcoord;

void main() {
  gl_FragColor = texture2D(texture, v_texcoord);
}
  </script>
<script src="https://twgljs.org/dist/twgl-full.min.js"></script>
<canvas id="c"></canvas>

【讨论】:

    【解决方案2】:

    其实更常见的做法,就是减一减你要翻转的轴:

    TexCoord.y = 1.0 - TexCoord.y;
    

    【讨论】:

    • 这非常有效 - 请注意,您可以在着色器内部或外部执行此操作
    猜你喜欢
    • 2013-09-07
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多