Search Unity

atlasToCube, or how to texture standard cube

Discussion in 'Assets and Asset Store' started by nicloay, Oct 9, 2012.

  1. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    540
    This simple plugin, allow you to use any image from your atlas as a texture for any of your face of standard cube.

    How it works.

    1. I have simple atlas 512x512px with equidistant rows and columns (4 rows and 4 columns)
      $Fish copy.png
    2. Just create a simple cube (Game Object/Create Other/Cube
    3. Attach CubeAtlasUVManager script on to it
    4. use inspector to adjust cube faces
      $Screen Shot 2012-10-11 at 4.48.57 PM.png
    5. Just change Row count and column count to what you need
    6. Position - means position in atlas (0 is top left corner 15 is bottom right, 3 is top right corner, and then we go to the next line, and 4 is a left image on 2d row)
    7. Rotation - this number multiplied to 90 degrees
    8. If you are working in editor, and you are having several instances of the cube, in this case you need to create instances of shared mesh, use Instantiate Shared Mesh in inspector. (this issue is related to how unity works with meshes, by default all primitives use shared mesh, and if you have 10 cubes on scene in editor mode, all of them has the same shared mesh that's mean that if you change one face on one cube, all another cubes will change face as well. You can't work with MeshFilter.mesh in Editor mode, you can work only with shared mesh, to prevent objects leaks, so to fix this problem we just need to create separated instances of this mesh and assign this mesh to sharedMesh)

    There is a scene with a small demo inside this plugin (Assets/Demo/Scene)
    $bg.large.png

    source code available at github
    art created by gekatarina

    updates

    • v1.001 (10/10/2012)
      • Updated event system
      • Mesh instantiation in editor mode (In Unity in Editor mode we can work only with shared mesh, so if you have 2 cubes on the scene, now, you can modify faces on theme independently)
    • v1.002 (10/10/2012)
      • fixed tangents for instantiated meshes
      • fixed prefab creation (when you create a prefab, mesh asset create and link to object automaticaly)
    • v1.003 (11/10/2012)
      • CRITICAL bug fix, "back face share image with front face" (thanks mbolt from this forum who posted it)
    • v1.004 (11/10/2012)
      • fixed "rotation/position"
      • provided documentation in README.txt
    • v1.005 (28/01/2012)
      • Unity4 compatibility (different vertex order in 4 and 3 version)
    • v1.006 (28/01/2012) [Released on Asset Store]
      • fixed reflected image for some faces

    download atlasToCube.unitypackage:
     
    Last edited: Jan 29, 2013
  2. mbolt

    mbolt

    Joined:
    Oct 10, 2012
    Posts:
    20
    First of all, thank you so much for providing this - it's very helpful!

    Not sure if you've caught this or not yet, but you have the following in your updateMesh() method:

    Code (csharp):
    1. updateUVs(ref uvs, backIndexes, _front); // <--- Should be _back
    2. updateUVs(ref uvs, topIndexes, _top);
    3. updateUVs(ref uvs, frontIndexes, _front);
    4. updateUVs(ref uvs, bottomIndexes, _bottom);
    5. updateUVs(ref uvs, leftIndexes, _left);
    6. updateUVs(ref uvs, rightIndexes, _right);
    Also, unrelated to the output, there's a small type in "bottom" for the editor portion -- it reads "botoom"

    Thanks again!

    Matt Bolt
     
  3. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    540
    mbolt, Thanks, i'm pretty happy that this plugin helped someone else. And thanks for this bug, i fixed it and updated the package.
     
    Last edited: Oct 12, 2012
  4. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    540
  5. Logiquefloue

    Logiquefloue

    Joined:
    Jun 6, 2013
    Posts:
    21
    Hello and thanks Nicloay

    I have 100 cubes, I want that when the player clicks on one side, the texture of the face changes. Do you think it's possible to do it in real time?

    thank you
     
  6. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    540
  7. Logiquefloue

    Logiquefloue

    Joined:
    Jun 6, 2013
    Posts:
    21
    Thank you very much. I'll watch it all
     
  8. schiehll

    schiehll

    Joined:
    Jul 11, 2013
    Posts:
    3
    Firstly, thanks for sharing this script, Nicolay.

    I dont know if is just me, but I have unity 4.1.3 and the changes that you did in script are not working well.

    Faces "back", "top" and "front" are messed up.

    So I changed the script to unity 3.5 version (in # if UNITY_3_5 statement) discarding the changes made​​, and then everything works fine.
     
    divyansh87 likes this.
  9. bager

    bager

    Joined:
    Oct 26, 2013
    Posts:
    1
    Great asset helped a lot but i am having problem as mentioned above.
     
  10. peplopezsanchez

    peplopezsanchez

    Joined:
    Jan 26, 2013
    Posts:
    1
    Yours scripts helped me a lot, thanks. I have detected an important memory leak in getMesh() method at CubeAtlasUVManager class and I want yo share it with you.
    You don't need to use "new" in this method. I had performance problems and with this change i have solve it.


    Mesh getMesh ()
    {
    Mesh result; // not = new Mesh();
    if (Application.isPlaying){
    result = gameObject.GetComponent<MeshFilter>().mesh;
    }else{
    result=gameObject.GetComponent<MeshFilter>().sharedMesh;
    }
    return result;
    }
     
  11. falsaform

    falsaform

    Joined:
    Dec 22, 2012
    Posts:
    1
    Hey this is exactly what im looking for a project that im working on. If there is any chance you can revisit this and update it for unity 4.3? the demo textures are warped on certain faces. Also if you make modifications and save as prefabs, on reloading the prefab, the texture is also skewed
     
  12. divyansh87

    divyansh87

    Joined:
    Aug 8, 2015
    Posts:
    1
    thanks man changing # if UNITY_3_5 statement helped me to overcome the same problem
     
  13. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699
    Is this still a valid plugin??? Will it allow me to use one material on many objects??? Cloud, grass, ball...

    Excited to try when I get home!
    Thanks
     
  14. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    540
    I didn't test it it for a long time but the concept there is very simple so with a good chance it will work out of the box.

    What it does is just assign proper UV coordinates to the cube mesh vertices.
     
    renman3000 likes this.
  15. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,699

    Ok,
    I look forward to trying it.
    Cheers!
     
  16. j2l2

    j2l2

    Joined:
    Mar 14, 2016
    Posts:
    14
    So, we are in 2016 and it's still a challenge to apply texture on a cube, at least for me :)
    In Unity 5.4, I tried to instantiate the cube of the demo scene and I got front, back, top OK, but left, right, and bottom scrambled like this:

    Note that your instantiated cube in the back is perfect. Only "new" cube (2016) seems to be affected.
    As falsaform noted, a prefabed cube got the same result with skewed UV applied.

    For another imported cube (OBJ) I get even weirder results where front is linked to top (both faces change when slider for front position is changed):

    But I get better results (skewed on 3 faces like internal cube) when tangents are not calculated at import.

    What is going on?
     

    Attached Files:

    Last edited: Aug 18, 2016
  17. philmzo

    philmzo

    Joined:
    Dec 2, 2015
    Posts:
    7
    Here are the changes in CubeAtlasUVManager for new cube:
    public static int[] bottomIndexes=new int[]{15,12,14,14,12,13}; // {14,12,13,13,12,15};
    public static int[] leftIndexes=new int[]{19,16,18,18,16,17}; // {18,16,17,17,16,19}
    public static int[] rightIndexes=new int[]{21,20,22,22,20,23}; // {22,20,21,21,20,23}