Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Converting sprite to mesh. "lighting information" not converted upon convertion

Discussion in 'Global Illumination' started by jimsell, Aug 26, 2017.

  1. jimsell

    jimsell

    Joined:
    Jan 9, 2016
    Posts:
    3
    I am trying to convert a sprite to a mesh because I want my mesh to have the shape and texture of the sprite.

    When I do this (see code below) the new mesh has the mesh, orientation and texture of the original sprite but that what holds the information of the lighting effects (such as light bounce and shadows) are still that of the original mesh. I have a directional light shining like a sun over the scene but only when I rotate the object perpendicular to the directional light, I see light bounce. So for the object, it's like the light is coming from the side instead of above the object (see images). So something is not carried over to the new mesh.

    I am bit of a noob. So if anything is unclear or if I am asking in the wrong place. Please let me know!!

    Image 1: https://ibb.co/jQYNxQ
    Image 2: https://ibb.co/gF8uBk


    Code (CSharp):
    1. Mesh SpriteToMesh(Sprite sprite)
    2.     {
    3.         Mesh mesh = new Mesh();
    4.         mesh.vertices = Array.ConvertAll(sprite.vertices, i => (Vector3)i);
    5.         mesh.uv = sprite.uv;
    6.         mesh.triangles = Array.ConvertAll(sprite.triangles, i => (int)i);
    7.  
    8.         return mesh;
    9.     }
     
    Last edited: Aug 26, 2017
  2. jimsell

    jimsell

    Joined:
    Jan 9, 2016
    Posts:
    3
    Hello again!

    I decided to read up on meshes and I found a solution. I had to recalculate the bounds and normals.

    Code (CSharp):
    1. Mesh SpriteToMesh(Sprite sprite)
    2.     {
    3.         Mesh mesh = new Mesh();
    4.         mesh.vertices = Array.ConvertAll(sprite.vertices, i => (Vector3)i);
    5.         mesh.uv = sprite.uv;
    6.         mesh.triangles = Array.ConvertAll(sprite.triangles, i => (int)i);
    7.         mesh.RecalculateBounds();
    8.         mesh.RecalculateNormals();
    9.  
    10.         return mesh;
    11.     }