Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Generate texture at runtime

Discussion in 'Project Tiny' started by atimurn, Mar 11, 2020.

  1. atimurn

    atimurn

    Joined:
    Apr 22, 2013
    Posts:
    7
    Hello,

    I'm trying to do the following:
    Convert some existing Texture2Ds to entities. Load them at runtime and generate another texture/entity.

    I found this in documentation:
    https://docs.unity3d.com/Packages/com.unity.tiny@0.22/api/Unity.Tiny.Image2DLoadFromFile.html
    https://docs.unity3d.com/Packages/com.unity.tiny@0.22/api/Unity.Tiny.Image2DMemorySource.html

    And can't figure out how does this work. I've tried to load texture but it doesn't work. Image2D component has Invalid status and 0x0 size. No errors though.
    Code (CSharp):
    1.  
    2. dstManager.AddComponent<Image2D>(entity);
    3. dstManager.AddBuffer<Image2DMemorySource>(entity)
    4.           .Reinterpret<byte>()
    5.           .CopyFrom(texture.GetRawTextureData());
    6.  
    Is this possible to do in Project Tiny? Are there any example?
     
  2. sebastianm_unity

    sebastianm_unity

    Unity Technologies

    Joined:
    May 3, 2018
    Posts:
    21
    Here is an example on how to upload textures from code in Tiny.
    You can not read back textures loaded in tiny yet.

    Code (CSharp):
    1.      
    2. Entity MakeTestTexture(int w, int h, bool srgb)
    3.         {
    4.             var eimg = EntityManager.CreateEntity();
    5.             Image2D img = new Image2D();
    6.             img.imagePixelWidth = w;
    7.             img.imagePixelHeight = h;
    8.             img.status = ImageStatus.Loaded;
    9.             img.flags = TextureFlags.UVRepeat | TextureFlags.MimapEnabled | TextureFlags.Trilinear;
    10.             if (srgb)
    11.                 img.flags |= TextureFlags.Srgb;
    12.             EntityManager.AddComponentData(eimg, img);
    13.             var buf = EntityManager.AddBuffer<Image2DMemorySource>(eimg);
    14.             buf.ResizeUninitialized(w * h * 4);
    15.             unsafe
    16.             {
    17.                 byte* dest = (byte*)buf.GetUnsafePtr();
    18.                 int o = 0;
    19.                 Random r = new Random((uint)(w+h+(srgb?10000:2)));
    20.                 for ( int y=0; y<h; y++ )
    21.                 {
    22.                     for (int x = 0; x < w; x++)
    23.                     {
    24.                         dest[o++] = (byte)r.NextUInt();
    25.                         dest[o++] = (byte)r.NextUInt();
    26.                         dest[o++] = (byte)r.NextUInt();
    27.                         dest[o++] = (byte)0xff;
    28.                     }
    29.                 }
    30.             }
    31.             return eimg;
    32.         }
    33.  
     
    Sarkahn, NotaNaN, Maras and 3 others like this.
  3. atimurn

    atimurn

    Joined:
    Apr 22, 2013
    Posts:
    7
    Thank you Sebastian. Really appreciate your help.
     
  4. chena_cpp

    chena_cpp

    Joined:
    Oct 27, 2014
    Posts:
    24
    Thanks.
    Also want know how to do that.