这个着色器在正交投影中的居中缩放效果有什么问题?
我创建了一个基本的正交着色器,用于显示纹理中的精灵。效果很好。
我添加了一个“缩放”因子,以允许精灵缩放变得更大或更小。假设纹理的原点锚定在“左下角”,它所做的就是向该原点收缩,或者从该原点向右上角扩展。我真正想要的是“就地”缩小或扩大以保持居中。
因此,实现这一目标的一种方法是计算出要缩小或扩展多少像素,并进行补偿。我不太确定该怎么做,而且我也知道这不是最好的方法。我愚弄了翻译和缩放的顺序,认为我可以先缩放然后放置,但我只是得到了各种不好的结果。我无法想出解决问题的方法。
这是我的着色器:
// Set up orthographic projection (960 x 640)
mat4 projectionMatrix = mat4( 2.0/960.0, 0.0, 0.0, -1.0,
0.0, 2.0/640.0, 0.0, -1.0,
0.0, 0.0, -1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
void main()
{
// Set position
gl_Position = a_position;
// Translate by the uniforms for offsetting
gl_Position.x += translateX;
gl_Position.y += translateY;
// Apply our (pre-computed) zoom factor to the X and Y of our matrix
projectionMatrix[0][0] *= zoomFactorX;
projectionMatrix[1][1] *= zoomFactorY;
// Translate
gl_Position *= projectionMatrix;
// Pass right along to the frag shader
v_texCoord = a_texCoord;
}
I've created a basic orthographic shader that displays sprites from textures. It works great.
I've added a "zoom" factor to it to allow the sprite to scale to become larger or smaller. Assuming that the texture is anchored with its origin in the "lower left", what it does is shrink towards that origin point, or expand from it towards the upper right. What I actually want is to shrink or expand "in place" to stay centered.
So, one way of achieving that would be to figure out how many pixels I'll shrink or expand, and compensate. I'm not quite sure how I'd do that, and I also know that's not the best way. I fooled with order of my translates and scales, thinking I can scale first and then place, but I just get various bad results. I can't wrap my head around a way to solve the issue.
Here's my shader:
// Set up orthographic projection (960 x 640)
mat4 projectionMatrix = mat4( 2.0/960.0, 0.0, 0.0, -1.0,
0.0, 2.0/640.0, 0.0, -1.0,
0.0, 0.0, -1.0, 0.0,
0.0, 0.0, 0.0, 1.0);
void main()
{
// Set position
gl_Position = a_position;
// Translate by the uniforms for offsetting
gl_Position.x += translateX;
gl_Position.y += translateY;
// Apply our (pre-computed) zoom factor to the X and Y of our matrix
projectionMatrix[0][0] *= zoomFactorX;
projectionMatrix[1][1] *= zoomFactorY;
// Translate
gl_Position *= projectionMatrix;
// Pass right along to the frag shader
v_texCoord = a_texCoord;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GLSL 中的矩阵是按列构造的。对于
mat4
,前 4 个值是第一列,然后接下来的 4 个值是第二列,依此类推。你转置了你的矩阵。
另外,那些-1 是做什么用的?
对于您问题的其余部分,缩放不是投影矩阵应该处理的问题。不是你说的那种缩放。在将它们与投影矩阵相乘之前,应将比例应用于位置。就像 3D 对象一样。
您没有发布精灵的顶点数据是什么,因此无法确定。但它的工作方式应该是精灵的顶点位置应以精灵的中心为中心(即您定义它的位置)。
因此,如果您有一个 16x24 的精灵,并且您希望精灵的中心向右偏移 8 像素,向上偏移 8 像素,那么您的精灵矩形应该是 (-8, -8) 到 (8, 16)(从底部开始) -左坐标系)。
然后,如果缩放它,它将围绕精灵坐标系的中心缩放。
Matrices in GLSL are constructed column-wise. For a
mat4
, the first 4 values are the first column, then the next 4 values are the second column and so on.You transposed your matrix.
Also, what are those -1's for?
For the rest of your question, scaling is not something the projection matrix should be dealing with. Not the kind of scaling you're talking about. Scales should be applied to the positions before you multiply them with the projection matrix. Just like for 3D objects.
You didn't post what your sprite's vertex data is, so there's no way to know for sure. But the way it ought to work is that the vertex positions for the sprite should be centered at the sprite's center (which is wherever you define it to be).
So if you have a 16x24 sprite, and you want the center of the sprite to be offset 8 pixels right and 8 pixels up, then your sprite rectangle should be (-8, -8) to (8, 16) (from a bottom-left coordinate system).
Then, if you scale it, it will scale around the center of the sprite's coordinate system.