Find color of pixel from sampler2D using pixel coord or Finding the dimension of sampler2D

Is there anyway to find a pixel value of a sampler2D using pixel coord? Or is there a way to get the dimension of the texture?

Right now all I can do is tex2D(sampler2D, UV). However, I need to find the pixel next to the current pixel of the UV coordinate.

As far as I know the shaders don’t know the size of your textures but you can pass it with a simple script like that:

material.SetVector("_MyTexResolution", new Vector2(myTexture.width, myTexture.height));

Then in the shader you can get the color of a single pixel like this (assuming your uv is a planar mapping):

float2 pixelPos = float2(48, 22);
float4 myPixelColor = tex2D(_MainTex, pixelPos / _MyTexResolution.xy);

To get the color of the “pixel next to the current pixel” you can do something like that:

float4 currentPixelColor = tex2D(_MainTex, i.texcoord.xy);
float2 nextPixelOffset = float2(1/ _MyTexResolution.xy, 0);
float4 nextPixelColor =  tex2D(_MainTex, i.texcoord.xy + nextPixelOffset);