webgl glsl 改变投影
我正在使用 webgl 在屏幕上绘制 2d 计划。我想稍微旋转一下平面图以给出 3D 效果。 当前的:
我的第一个方法是使用消失点,例如透视图,但我不知道如何更改 y 坐标,所以我没有到达终点。有没有更简单的方法来旋转输出?
这是我的代码:
uniform float scale;
uniform vec2 ratio;
uniform vec2 center;
in vec3 fillColor;
in vec2 position;
out vec3 color;
void main() {
color = fillColor;
gl_Position = vec4((position - center) * ratio, 0.0, scale);
}
I'm drawing a 2d plan on the screen using webgl. I would like to rotate the plan a bit to give a 3d impression.
current:
My first approach was to use vanishing points like drawing in perspective but I didn't know how to change the y coordinate and I didn't get to the end. Is there an easier way to rotate the output?
Here is my code:
uniform float scale;
uniform vec2 ratio;
uniform vec2 center;
in vec3 fillColor;
in vec2 position;
out vec3 color;
void main() {
color = fillColor;
gl_Position = vec4((position - center) * ratio, 0.0, scale);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想构建整个游戏引擎或复杂的动画,您将需要深入研究透视投影矩阵。
但如果您只是想实现这个小效果并尝试了解它是如何工作的,您可以使用
gl_Position
的w
坐标。例如,该坐标对于告诉 GPU 如何以有效的 3D 方式插值 UV 纹理至关重要。它将被分为x
、y
和z
。因此,我们假设您要显示一个矩形。你将需要两个三角形。
如果您使用 TRIANGLE_STRIP 模式,4 个顶点就足够了。我们可以只使用一个属性,但为了教程的目的,我将使用两个:
所有逻辑都将在顶点着色器中:
attUV.y
行0.5? uniScale : 1.0
表示uniScale
1.0
attUV
属性允许您使用纹理如果你想。在这个例子中,我只是用这个片段着色器模拟一个棋盘:
您可以在这个 CopePen 中看到所有这些操作:
https://codepen.io/tolokoban/full/oNpBRyO
If you want to build a whole game engine or a complex animation, you will need to dig into perspective projection matrices.
But if you just want to achieve this little effect and try to understand how it works, you can just use the
w
coord ofgl_Position
. This coordinate is essential to tell the GPU how to interpolate UV textures in a valid 3D way, for example. And it will be divided tox
,y
andz
.So let's assume you want to display a rectangle. You will need two triangles.
4 vertices will suffice if you use TRIANGLE_STRIP mode. We could use only one attribute, but for the sake of tutorial, I will use two:
And all the logic will be in the vertex shader:
The line
attUV.y < 0.5 ? uniScale : 1.0
meansuniScale
1.0
The
attUV
attribute let's you use a texture if you want. In this example,I just simulate a checkboard with this fragment shader:
You can see all this in action in this CopePen:
https://codepen.io/tolokoban/full/oNpBRyO