#In-shader texture crop

6 messages · Page 1 of 1 (latest)

fluid hound
#

So, as some might know, I'm porting a raycasting engine to Unity

All my texture coordinates are stored as world units (IN.uv_MainTex)

The original engine does some things when drawing these textures:

-It first crops the original texture on a given rect area (_X0, _Y0, _X1, _Y1) <- min and max rect values

-Then it draws N pixels from this rect in the destination triangle, but it only draws N texels per world unit (_SCALEX, _SCALEY)
So when we have _SCALEX = 32 and _SCALEY = 32, it means the shader has to draw 32x32 texels inside a world unit UV area
It also has an _OFFSETX, and _OFFSETY parameters that simply offsets the texture inside the destination triangle

Almost all that is working fine, but the cropping code is not implemented yet, and I'm still trying to understand how to do that

So far the shader looks like that:

float2 rectMin = float2(_OFFSETX + (_X0 * _SCALEX), _OFFSETY + (_Y0 * _SCALEY));
float2 rectMax = float2(rectMin.x + _SCALEX, rectMin.y + _SCALEY);
float2 uv = lerp(rectMin, rectMax, IN.uv_MainTex);
uv *= _MainTex_TexelSize.xy;
float4 c = tex2D (_MainTex, uv);```
Some textures like this one need to be cropped using `(_X0,_Y0,_X1,_Y1)` before being applied:
#

The original texture has 320x256, but the cropped red area (_X0,_Y0,_X1,_Y1) is (0,0,256,256)

#

It has to be cropped + repeated, to be more specific
_MainTex_TexelSize.xy contains the texture texel resolution (1 / res)
_MainTex_TexelSize.zw contains the texture pixel resolution

fluid hound
#

Almost