Search Unity

3D coin from 2D image

Discussion in 'Scripting' started by Roberdinho, Oct 21, 2016.

  1. Roberdinho

    Roberdinho

    Joined:
    Feb 28, 2015
    Posts:
    2
    Hello,

    I remember seeing a (youtube) video tutorial where someone reads a 2D image of a coin and by reading the pixel color and opacity values he instantiates 3D boxes to render a giant 3D coin. I can not find it anymore on youtube. Does anyone know this video and knows where this video can be found?

    Thanks in advance,
    Treborg
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    I have no idea what video you're talking about, but the process you describe wouldn't really be very difficult. Just make a new list of GameObjects, reference an image, and iterate over the image contents pixel by pixel and instantiate (or not, depending on the transparency value) box prefabs using the indices as positional offsets. You can use a second image as a kind of heightmap for in/out positional offsets as well, or use the range of transparency values in a given pixel for that sort of "height" offset.

    The real problem is that each box would need its own material so that you could color it based on the pixel color, and that could end up being a lot of different materials and cause more stress than the thousands of primitives do. There might be a more efficient way using a custom shader and vertex colors or something though.
     
  3. Roberdinho

    Roberdinho

    Joined:
    Feb 28, 2015
    Posts:
    2
    I have to reply to my old question because I found the video back:

    Maybe someone else finds this useful too. The video is called Texture to cubes.
     
  4. ChristmasEve

    ChristmasEve

    Joined:
    Apr 21, 2015
    Posts:
    45
    I want to do something like this too but not with a bunch of cubes. I want to generate a single mesh that looks like some texture on both sides (one side in reverse) but give it some thickness, like a coin, but it can have an irregular shape. Imagine tracing out the object, ignoring black pixels and making your polygon from that (and then the 3D mesh).
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,408
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,990
    We had already several threads on that topic. Over here I showed one of my approaches which simply used a cutout shader for the front and back faces and only generates the connecting faces between them. Of course when you use an image as source, it will never be smooth since an image consists of pixels. Likewise a mesh would not be smooth either since it consists of flat surfaces (triangles / quads) as well. So depending on the image quality, the quality of the edge has a huge influence on what approach would yield a good or bad result. If the image has a "smooth" edge (i.e. some blurred / alpha blended fade out) that would not really help much since you need to have a hard edge where to place the edge / rim of your coin.

    So there's no generally valid solution that "just works" with any kind of image. Just like in the minecraft example, having a clear edge is usually a requirement to get a proper result. Of course if the image is always a perfect coin, one could use a plain cylinder mesh with a certain vertex count and just do some texture mapping on it. Though that's not that trivial either, especially when the rim texture doesn't really match the front / back face.

    Though since the necro comment mentioned irregular shapes, the only viable solution is usually the cutout and hard edge approach on a per-pixel basis.