在CG / HLSL中计算屏幕纹理坐标 [英] Calculating screen texture coordinates in CG/HLSL

查看:277
本文介绍了在CG / HLSL中计算屏幕纹理坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OpenGL中,有时在进行多遍渲染和后期处理时,我需要将图素应用于原始图元的汇编片段,这些片段是全屏纹理合成的一部分。当当前遍历来自FBO纹理要实现此目的,我在SCREEN SPACE中计算对象的UV坐标。在GLSL中,我按以下方式计算它:

In OpenGL , sometimes when doing multi-pass rendering and post-processing I need to apply texels to the primitive's assembly fragments which are part of full screen texture composition.That is usually the case when the current pass comes from FBO texture to which the screen quad had been rendered during previous pass.To achieve this I calculate objects UV coordinates in SCREEN SPACE .In GLSL I calculate it like this:

     vec2 texelSize = 1.0 / vec2(textureSize(TEXTURE, 0));
 vec2 screenTexCoords = gl_FragCoord.xy * texelSize;

现在,我正在尝试使用 CG / HLSL。这些语言的文档很差。如何在CG / HLSL中计算屏幕uv坐标?

Now I am experimenting with Unity3D which uses CG/HLSL.The docs for these languages are poor.How can I calculate screen uv coordinates in CG/HLSL?

推荐答案

要计算屏幕texcoords,通常需要通过屏幕分辨率或其倒数( res rcpres )到着色器,然后使用全屏四边形的texcoords(假设范围为 [0,1] )被那些修改。

To calculate screen texcoords, you typically pass the screen resolution or reciprocal thereof (res or rcpres) to the shader, then use the texcoords from your fullscreen quad (assuming a range of [0,1]) modified by those.

编辑:请注意,在许多情况下,您都希望同时使用 res rcpres 可用。 res 对于计算当前位置很有用(您不希望使用 rcpres ,因为那样会需要除法),但是 rcpres 可以在CPU上计算一次,对于移动单个纹理元素很有用((res * uv)+ rcpres) 是来自原点的另一个纹理元素。)

Note that in many cases, you want both res and rcpres available. res is useful for calculating the current position (you wouldn't want to use rcpres for that, since it would then require division), but rcpres can be calculated once on the CPU and is useful for moving by a single texel ((res * uv) + rcpres) is one more texel from origin).

类似的东西:

float2 res; // contains the screen res, ie {640, 480}

void postEffect(in float2 uv : TEXCOORD, ...)
{
    float2 pos = uv * res; // pos now contains your current position

逻辑上,来自fs quad的坐标是 0,0 在texel 0,0 1,1 在texel maxX,maxY ,因此知道尺寸后,就可以相乘。

The logic being that the coords coming from the fs quad are 0,0 at texel 0,0 and 1,1 at texel maxX,maxY, so knowing the dimensions, you can just multiply.

您必须提供值每个效果的 res 大小,这取决于您的着色器系统(Unity3D可能已经做到了)。

You have to provide the value of res to each effect, however, which depends on your shader system (Unity3D may do it already).

这篇关于在CG / HLSL中计算屏幕纹理坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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