如何在着色器,Android的旋转纹理 [英] How to rotate a texture in a shader, Android

查看:256
本文介绍了如何在着色器,Android的旋转纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个纹理我想旋转指定的角度旋转。这里是我的UV坐标

So I have a texture I want to rotate given the angle rotation. Here are my UV coordinates

float[] landscapeVerts = {
    // X, Y, Z, U, V
    -ratio, -1.0f, z_0, 1.0f, 0.0f,
    ratio, -1.0f, z_0, 0.0f, 0.0f,
    -ratio,  1.0f, z_0, 1.0f, 1.0f,
    ratio,  1.0f, z_0, 0.0f, 1.0f,
};

我想旋转就在紫外线的一部分。所以我建立以下矩阵在着色器来实现。

I want to rotate just the UV part. So I built the following matrix to accomplish this in the shader.

    Matrix.setIdentityM(mProjMatrix, 0); //start with identity matrix
    Matrix.translateM(mProjMatrix,0,-0.5f,-0.5f,0.0f); // move center to center [0 1] : [-.5 .5]
    Matrix.rotateM(mProjMatrix, 0, rotation, 0.0f,0.0f,-1.0f); // rotate about z axis
    Matrix.translateM(mProjMatrix,0,0.5f,0.5f,0.0f); // move back to [0 1] range

然后我加载矩阵到着色器为 uSTMatrix 和繁殖纹理由它协调

"uniform mat4 uMVPMatrix;\n" +
"uniform mat4 uSTMatrix;\n" +
"attribute vec4 aPosition;\n" +
"attribute vec4 aTextureCoord;\n" +
"varying vec2 vTextureCoord;\n" +
"varying vec2 calcTexCoord;\n" +
"void main() {\n" +
"  gl_Position = uMVPMatrix * aPosition;\n" +
"  vTextureCoord = (uSTMatrix * aTextureCoord).xy;\n" +
"  calcTexCoord = aTextureCoord.xy;\n" +
"}\n";

但是,这是不是做我想做的事情。它应该很容易在着色器来实现这一点,我缺少什么?或者这可怎么容易完成。

But this is not doing what I want it to do. It should be easy to accomplish this in the shader, what am I missing? or how can this be accomplished easily.

推荐答案

我想通了。只需要改变经营秩序。

I figured it out. Just need to change the order of operation.

float[] identityMatrix = new float[16];
Matrix.setIdentityM(identityMatrix, 0); //start with identity matrix
Matrix.translateM(mProjMatrix,0,identityMatrix, 0, 0.5f,0.5f,0.0f); // move back to [0 1] range
Matrix.rotateM(identityMatrix, 0, mProjMatrix, 0, rotation, 0.0f,0.0f,1.0f); // rotate about z axis
Matrix.translateM(mProjMatrix,0, identityMatrix, 0, -0.5f,-0.5f,0.0f); // move center to center [0 1] : [-.5 .5]

相反移动到中心,旋转和向后移动的;我们建立逆矩阵 - >回迁,旋转,然后移动到中心

Instead of moving to the center, rotating, and moving back; we build the matrix in reverse -> move back, rotate, and then move to center.

这篇关于如何在着色器,Android的旋转纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆