使用着色器中的索引引用数据纹理中的纹素 [英] Referencing texels in a data texture using indexes in the shader

查看:32
本文介绍了使用着色器中的索引引用数据纹理中的纹素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用着色器中的索引访问 DataTexture 的纹素中的值.索引[0, 1, 2, 3, 4, 5, 6... 62, 63] 是连续的,而数据纹理有高度和宽度(uTextureDimension) of 8. 经过一番研究,我写了这个函数来获取特定的索引值,并引用相应的纹素:

I have values in the texels of a DataTexture that I am trying to access using indexes in my shader. The indexes [0, 1, 2, 3, 4, 5, 6... 62, 63] are continuous, while the data texture has a height and width (uTextureDimension) of 8. After some research I wrote this function to take a particular index value, and reference the corresponding texel:

vec2 customUV = vec2( mod(aIndex, uTextureDimension) / uTextureDimension, floor(aIndex / uTextureDimension) / uTextureDimension );
vec4 texelValues = texture2D( tDataTexture, customUV ).xyzw;

我也试过这个版本从它的中心点引用纹素.也没有骰子:

I also tried this version to reference the texel from its center point. Also no dice:

vec2 perMotifUV = vec2( mod( aIndex, uTextureDimension ) * (( 1.0 / uTextureDimension )/2.0), floor( aIndex / uTextureDimension ) * (( 1.0 / uTextureDimension )/2.0) );
vec4 texelValues = texture2D( tDataTexture, customUV ).xyzw;

从昨天下午开始处理这个问题,在这里和那里编辑它,并四处寻找其他解决方案,我仍然没有得到预期的结果.我应该说在使用 Three.js 时,着色器被设置为高精度浮点数 - 这是问题的一部分吗?任何人都可以在这里推动我走上正轨吗?谢谢!

After working with this since yesterday afternoon, editing it here and there, and looking around for other solutions, I'm still not getting the expected results. I should say that in using Three.js, the shaders are set to high precision float - is this part of the problem? Can anyone nudge me on track here? Thanks!

推荐答案

第一个似乎是对的,如果一切都是浮动的.您是否使用 NEAREST 过滤器模式?从技术上讲,您还应该添加一个半纹素大小的偏移量:

The first one seems right, if everything is a float. Are you using NEAREST filter mode? Technically you should also add a half-texel-size offset too:

vec2 texelSize = 1.0 / uTextureDimension;
vec2 customUV = vec2( mod(aIndex, uTextureDimension)*texelSize, floor(aIndex / uTextureDimension)*texelSize );
customUV += vec2(0.5*texelSize);

编辑 - 你的指数也应该是 0-63 而不是 0-64

EDIT - Also your indices should go 0-63 not 0-64

这篇关于使用着色器中的索引引用数据纹理中的纹素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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