Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atlas Support #3

Open
Reahreic opened this issue Jun 24, 2022 · 0 comments
Open

Atlas Support #3

Reahreic opened this issue Jun 24, 2022 · 0 comments

Comments

@Reahreic
Copy link

Reahreic commented Jun 24, 2022

The current solution doesn't support sprites that are part of an atlas. AFAIK there's no shader-only way to resolve this, but the following will at least enable atlas sprite support.

Add a component to the SpriteRenderer, that sends the sprite's uv space Rect to the material. This can be triggered myltiple ways, with the easiest on Awake.

//Calculates the sprite's position in the atlas (in uv space) and sends that data to the material.
void PopulateAtlasInfo() {
      SpriteRenderer rend = GetComponent<SpriteRenderer>();
      Rect sprite = rend.sprite.textureRect;

      Vector4 spriteData = new Vector4(
            sprite.x / rend.sprite.texture.width,
            sprite.y / rend.sprite.texture.height,
            sprite.width / rend.sprite.texture.width,
            sprite.height / rend.sprite.texture.height
      );

      rend.material.SetVector("_SpriteData", spriteData);
}

Next, add a property to the shader to hold the sprite data.
_SpriteData("X, Y, Width, Height", Vector) = (1, 1, 1, 1)

Lastly, in the frag function after you sample the sprite calculate your (x,y) uv's for the atan using the sprite data.

//Convert UV coordinate space to cartesian coordinate space (0;1) to (-1;1) While taking into account the sprite's position in the texture and centering it around the cartesian origin.
float x = lerp(-1, 1, (IN.uv0.x - _SpriteData.x) / _SpriteData.z);
float y = lerp(-1, 1, (IN.uv0.y - _SpriteData.y) / _SpriteData.w);

//Calculate angle, convert to degrees
float angle = atan2(x, y) * 57.2958;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant