Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Sprite Extrude geometry shader works - need help to improve it

Discussion in 'Shaders' started by Bloxfan, Mar 6, 2021.

  1. Bloxfan

    Bloxfan

    Joined:
    Feb 14, 2020
    Posts:
    12
    Hi!

    After looking for a long long time, I finally found an amazing geometry shader that allows to extrude sprites: https://github.com/MurabitoB/UnitySpriteExtrudeShader

    This geometry shader is very complex to me, I hope someone can help me understand 2 issues I have:

    1/Transparency
    - As we can see below, the transparent part inside a sprite doesnt work
    - Weirdly enough, if I cut the sprite so that the transparent part isnt ''closed'', it works

    2/ Outline
    - If the size (in pixel) of the sprite is too small, there's a weird outline around the sprite
    - Currently I need to resize the sprite to 1280*1024pixel to avoid the outline

    I hope to find a solution so I dont need to resize all my sprites + transparency works properly :)
    Thanks!

    upload_2021-3-6_23-35-47.png
    upload_2021-3-6_23-36-1.png

    upload_2021-3-6_23-39-56.png

    Extrude Sprite example
    upload_2021-3-6_23-35-20.png
     
    Last edited: Mar 10, 2021
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    First, please use
    [code][/code]
    tags if you're going to be posting code.

    Second, there was no need to post the code since you already linked to the shaders, and they're much easier to read on github that here in the forums.

    Third, and perhaps most importantly, the shader code is completely unimportant here. It has no bearing on the problem you have, nor can it be modified to fix it.

    I don't blame you for thinking it's the problem, but just know it's not really part of the conversation. The shader isn't doing anything particularly fancy, it's just extruding the mesh that's passed to it. It doesn't look at the sprite texture at all when generating the geometry.

    The problem lies entirely with how Unity's sprite system generates tight sprite meshes. By default Unity generates a unique mesh for each sprite that tightly confines to the outside of the sprite's visible area. However there's almost nothing you can do to control how this is generated. It has some internal minimum per pixel detail size which it won't go below meaning low resolution sprites get a "chunkier" mesh sprite than a higher resolution one. It generates the mesh based on the areas that are fully transparent and with the assumption of bilinear filtering meaning there mesh is always about 1/2 a pixel than what you might expect it to be and any areas that aren't fully transparent, either due to some sloppiness in the texture or due to texture compression, won't get chopped. And it does not generate holes in the mesh for sprites that holes in the texture.

    This explains all of the issues you're seeing above. The solution is to use better sprite meshes. There used to be an excellent tool on the asset store called Sprite Sharp that did all of this better. But it's since been abandoned. Luckily someone else took up the mantle and released a version of the tool for free here:
    https://github.com/sr4dev/Unity-SpriteAssist

    It even includes a way to convert sprites into extruded meshes, which removes the need for the geometry shader entirely (which might be handy if you want to support mobile devices as most do not support geometry shaders).
     
    RascarCapack, Seromu and Bloxfan like this.
  3. Bloxfan

    Bloxfan

    Joined:
    Feb 14, 2020
    Posts:
    12
    Hi Bgolus, thanks for taking the time to explain, appreciate!
    Tbh, I didnt expect a reply as I found few posts regarding sprite extrude haha

    Well noted regarding the code

    SpriteAssist looks amazing, unfortunately I can't use it (I'm limited to using Unity 2018 for various reasons + the extrude effect use MeshRenderer, and I need to use SpriteRenderer for the animation).

    But thanks to you, I understand the real problem so will look into Sprite Optimizer tools
    (TexturePacker seems like a good asset too)

    I sorta suspected that unity sprite mesh could be a problem, as I found other shaders that use Polygon Collider2D to extrude sprite, and the shape of the collider was similar to the outline issue I have.

    Sidestory:
    - My first choice was to convert sprites to voxel + use Unity animator
    - After some research I found out that Unity animator isn't great performance-wise. And mesh swapping was bad too.
    - So decided to go with sprite extrude + simple sprite animation script
    - If interested, an early trailer of my game:


    Thanks again for your help man!
     
    Last edited: Mar 10, 2021