0
\$\begingroup\$

I am writing Mandelbrot set shader for Unity.

I wrote an Image Effect shader and it works.

Unfortunately, each point "shimmers" on translate scale. This is because, for each shader call I compute value for only one mathematical point, which is infinitesimal. On even small translation, the value can change drastically.

So, I would like to compute math value not for one point, but for several. But for this I need to know, which aother points belong to my pixel, and for this I need to know the distance to neighbor pixel. Unfortunately, my incoming values are float and I don't know, how to step to next pixel.

How to accomplish?

\$\endgroup\$
1
\$\begingroup\$

Somewhere in your shader you'll have a declaration of your texture sampler, something like this:

sampler2D _MainTex; // This is the sampler.
float4 _MainTex_ST; // This is the scale/offset parameters set in the material widget.

Add one extra line below these:

float4 _MainTex_TexelSize;

This gives you a variable you can access in your shader, that Unity will pre-populate as described in the docs:

Texture size {TextureName}_TexelSize - a float4 property contains texture size information:

  • x contains 1.0/width
  • y contains 1.0/height
  • z contains width
  • w contains height

Now you can use this in your shader. Where the "current" pixel being drawn is...

float2 thisPixel = i.uv;

We can shift this to get...

float2 rightNeighbour  = i.uv + float2(_MainTex_TexelSize.x, 0);
float2 leftNeighbour   = i.uv - float2(_MainTex_TexelSize.x, 0);
float2 topNeighbour    = i.uv + float2(0, _MainTex_TexelSize.y);
float2 bottomNeighbour = i.uv - float2(0, _MainTex_TexelSize.y);

And of course you can scale this by a scalar to reach over only half a pixel, or two pixels, or combine both x & y to access a neighbouring point in any diagonal direction.

\$\endgroup\$
7
  • \$\begingroup\$ Sorry, but this didn't work by some reason, don't understand why yet \$\endgroup\$
    – Dims
    Nov 3 '20 at 21:33
  • \$\begingroup\$ What symptoms are you observing? How are you using this shader at present? The more information you can share, the better we can help you solve the problem. \$\endgroup\$
    – DMGregory
    Nov 3 '20 at 21:34
  • \$\begingroup\$ Image started to duplicate with several "ghosts", as if docs about width and height is either incorrect or incomplete. Probably width and height are just 100 or something. Unfortunately, I don't know how to display the values from within shader. \$\endgroup\$
    – Dims
    Nov 3 '20 at 21:38
  • \$\begingroup\$ The docs are correct, but the way you're using them might be incorrect. Try editing your question to walk us through all the steps needed to reproduce this problem in a new empty project. \$\endgroup\$
    – DMGregory
    Nov 3 '20 at 21:39
  • \$\begingroup\$ Sure. Why is it saying about "texture" width and height? I have no texture at all. \$\endgroup\$
    – Dims
    Nov 4 '20 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.