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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to create a 2D object that is based on a material, rather than a sprite?

Discussion in '2D' started by Diminished_9th, Dec 22, 2022.

  1. Diminished_9th

    Diminished_9th

    Joined:
    Jan 4, 2021
    Posts:
    1
    I want to create a 2D object with a visual component. However, I don't want it to have an underlying sprite. I'd rather have it just come from a material.

    (For example, a red rectangle that has a material based on a shader that returns red for every pixel. It doesn't have a sprite, because the material provides all the required visual information.)

    To my knowledge, a material can simply be applied to a 3D object, but not a 2D object?

    I am aware that this can be achieved with a 'dummy' sprite, with a custom material applied on top, but that seems like a bit of a hacky solution. It also leads to the sprite renderer complaining about the material not having a _MainTex texture property.

    I apologise if this question has been asked before - I couldn't find a solution.

    Thank you very much for your help. Let me know if I need to clarify anything.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    There's many parts to what you contemplate, but all parts are contained in some fashion in my MakeGeo project.

    For example, the
    makethicksprites
    scene and script might do almost exactly what you contemplate.

    Another example is
    Bitmap2Grid
    , which reads pixels to decide how to fabricate a level out of individual objects.

    MakeGeo is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/makegeo

    https://github.com/kurtdekker/makegeo

    https://gitlab.com/kurtdekker/makegeo

    https://sourceforge.net/p/makegeo
     
  3. FICinc

    FICinc

    Joined:
    Mar 6, 2022
    Posts:
    25
    Actually, this is very simple, long as you're fine with doing some work. Put a white square(or whatever shape you need) sprite in a SpriteRenderer on a game object, then go into the hierarchy, take the material, then just drag it to the gameObject.

    To make sure it doesn't yell about a missing variable, just make sure the material uses a shader with a variable with a reference to a "_MainTex" texture property.

    And btw, you could literally just use a 3D object with a 2D collider if you absolutely need to.

    And also, there is no such thing as "cheap" in game making, less you are doing some type of self inflicted challenge, in which case you have only yourself to blame.
     
    jesuspxp likes this.
  4. jesuspxp

    jesuspxp

    Joined:
    Dec 1, 2022
    Posts:
    1
    Yourreply of "there are no "cheap ways to do stuff in GameDev" " was actually the best. Ive ben trying to make a 2D shape have a texture but i think i wil just use 3D primituves because i just wont have to go though the unnecessary hustle of figuring this out.
    Id still like that it would be easier to apply materals just like with 3D objects tho.
     
  5. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,493
    Just for your information: Technically the GPU will see no difference :D

    GPUs nowadays do not differentiate between 2D and 3D in terms of the underlying data structure (only in terms of projection settings aka camera used). That used to be different when the word "sprite" (which means "ghost") was coined: An image that was ghosting in memory and could be copied into the output buffer any time with an offset to display something in 2D.

    In Unity however a sprite even has a mesh and the SpriteRenderer is a wrapper around the same functionality which the MeshRenderer uses too.
    This makes working with pure 2D a tad less intuitive but when you wanna do a 2.5D game or even have just a 3D effect via lights it saves a ton of hassle that sprites work seamlessly with almost all 3D features.