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

Crop texture of 3D-object

Discussion in 'Scripting' started by Hedge, Jan 8, 2014.

  1. Hedge

    Hedge

    Joined:
    May 29, 2013
    Posts:
    2
    $dive-meter.png

    I've got a diver with a depth finder which ranges from green to red (see picture).
    I'd like to crop that texture on the fly so I can determine the shown amount of the bar myself.

    How can I proceed to make this work?

    This code from the unity-documentation using SetPixels() didn't work at all.
    Code (csharp):
    1. exture2D sourceTex = renderer.material.mainTexture as Texture2D;
    2.         int x = 50;
    3.         int y = 30;
    4.         int width = 100;
    5.         int height = 100;
    6.         Color[] pix = sourceTex.GetPixels(x, y, width, height);
    7.         Texture2D destTex = new Texture2D(width, height);
    8.         destTex.SetPixels(pix);
    9.         destTex.Apply();
    10.         renderer.material.mainTexture = destTex;
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,617
    You probably want to tweak the tiling and offset values of the material, which determine what part of the texture are shown.
     
  3. Hedge

    Hedge

    Joined:
    May 29, 2013
    Posts:
    2
    That only amounts to moving or resizing the texture, not cropping though.