用于将纹理投影到任意四边形的像素着色器

时间:2023-02-09 10:49:42

Just need to figure out a way, using Pixel Shader, to project a texture to an arbitary user-defined quadrilateral.

只需要找出一种方法,使用Pixel Shader将纹理投影到用户定义的任意四边形。

Will be accepting coordinates of the four sides of a quadrilateral:

将接受四边形四边的坐标:

/// <defaultValue>0,0</defaultValue>
float2 TopLeft : register(c0);

/// <defaultValue>1,0</defaultValue>
float2 TopRight : register(c1);

/// <defaultValue>0,1</defaultValue>
float2 BottomLeft : register(c2);

/// <defaultValue>1,1</defaultValue>
float2 BottomRight : register(c3);

Tried couple of interpolation algorithm, but couldn't manage to get it right.

尝试了几种插值算法,但无法设法使其正确。

Is there any sample you guys think which I might be able to modify to get the desired result?

你们认为我有什么样的样本可以修改以获得理想的结果吗?

2 个解决方案

#1


1  

The issue here is that all modern 3D graphics hardware rasterizes triangles, not quadrilaterals. So if you ask Direct3D or OpenGL to render a quad, the API will internally split the quad into two triangles. This is not usually a problem since all modern rendering is perspective-correct, so the viewer should not be able to tell the difference between one quad and two triangles - the interpolation will be seamless.

这里的问题是所有现代3D图形硬件都会栅格化三角形,而不是四边形。因此,如果您要求Direct3D或OpenGL渲染四边形,API将在内部将四边形分割为两个三角形。这通常不是问题,因为所有现代渲染都是透视校正的,因此观察者不应该能够分辨出一个四边形和两个三角形之间的区别 - 插值将是无缝的。

The correct way to ask the API to perform this interpolation is to pass it the texture coordinates as per-vertex data, which the API/hardware will interpolate across each pixel. Your pixel shader will then have access to this per-pixel texture coordinate. I assume that when you use the term 'project' you mean simply mapping a rectangular texture onto a quad, and you are not referring to texture projection (which is like a spotlight shining a texture onto surfaces).

要求API执行此插值的正确方法是将纹理坐标作为每顶点数据传递给它,API /硬件将在每个像素上插值。然后,您的像素着色器将可以访问此每像素纹理坐标。我假设当你使用术语“项目”时,你的意思是简单地将矩形纹理映射到四边形,而你并不是指纹理投影(这就像聚光灯将纹理照射到表面上)。

The bottom line here is that passing the quad's texture coordinates to the pixel shader is the wrong approach (unless you have some special reason why this is required).

这里的底线是将四边形纹理坐标传递给像素着色器是错误的方法(除非你有一些特殊的原因,为什么这是必需的)。

#2


1  

There's a nice paper here describing your options (or this .ppt). Basically you need to define some barycentric coordinates across the quad, then interpolate fragment values as BC-weighted sums of the given vertex values.

这里有一篇很好的论文描述了你的选择(或者这个.ppt)。基本上,您需要在四边形中定义一些重心坐标,然后将片段值插值为给定顶点值的BC加权和。

Sorry, don't know of any code; the lack of direct support for quads on modern triangle-oriented HW (see voidstar69's answer) means they've rather gone out of fashion.

对不起,不知道任何代码;在现代三角形硬件上缺乏对四边形的直接支持(参见voidstar69的答案)意味着它们已经过时了。

#1


1  

The issue here is that all modern 3D graphics hardware rasterizes triangles, not quadrilaterals. So if you ask Direct3D or OpenGL to render a quad, the API will internally split the quad into two triangles. This is not usually a problem since all modern rendering is perspective-correct, so the viewer should not be able to tell the difference between one quad and two triangles - the interpolation will be seamless.

这里的问题是所有现代3D图形硬件都会栅格化三角形,而不是四边形。因此,如果您要求Direct3D或OpenGL渲染四边形,API将在内部将四边形分割为两个三角形。这通常不是问题,因为所有现代渲染都是透视校正的,因此观察者不应该能够分辨出一个四边形和两个三角形之间的区别 - 插值将是无缝的。

The correct way to ask the API to perform this interpolation is to pass it the texture coordinates as per-vertex data, which the API/hardware will interpolate across each pixel. Your pixel shader will then have access to this per-pixel texture coordinate. I assume that when you use the term 'project' you mean simply mapping a rectangular texture onto a quad, and you are not referring to texture projection (which is like a spotlight shining a texture onto surfaces).

要求API执行此插值的正确方法是将纹理坐标作为每顶点数据传递给它,API /硬件将在每个像素上插值。然后,您的像素着色器将可以访问此每像素纹理坐标。我假设当你使用术语“项目”时,你的意思是简单地将矩形纹理映射到四边形,而你并不是指纹理投影(这就像聚光灯将纹理照射到表面上)。

The bottom line here is that passing the quad's texture coordinates to the pixel shader is the wrong approach (unless you have some special reason why this is required).

这里的底线是将四边形纹理坐标传递给像素着色器是错误的方法(除非你有一些特殊的原因,为什么这是必需的)。

#2


1  

There's a nice paper here describing your options (or this .ppt). Basically you need to define some barycentric coordinates across the quad, then interpolate fragment values as BC-weighted sums of the given vertex values.

这里有一篇很好的论文描述了你的选择(或者这个.ppt)。基本上,您需要在四边形中定义一些重心坐标,然后将片段值插值为给定顶点值的BC加权和。

Sorry, don't know of any code; the lack of direct support for quads on modern triangle-oriented HW (see voidstar69's answer) means they've rather gone out of fashion.

对不起,不知道任何代码;在现代三角形硬件上缺乏对四边形的直接支持(参见voidstar69的答案)意味着它们已经过时了。