Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Why doesn't this script work with Imported Meshes?

Discussion in 'Scripting' started by Loki18000, Sep 9, 2019.

  1. Loki18000

    Loki18000

    Joined:
    Apr 1, 2015
    Posts:
    20
    Hello!

    I found this script from Git Hub, it helps set the Materials on models by re calculating them. So it gives off a more seamless effect. It works great on the standard assets from Unity but it does not work with imported meshes from Blender. Is there something I'm doing wrong? Is the mesh UV unwrapped incorrectly?

    This is the script.

    Code (CSharp):
    1. [ExecuteInEditMode]
    2. public class ReCalcCubeTexture : MonoBehaviour
    3. {
    4.     private Vector3 _currentScale;
    5.  
    6.     private void Start()
    7.     {
    8.         Calculate();
    9.     }
    10.  
    11.     private void Update()
    12.     {
    13.         Calculate();
    14.     }
    15.  
    16.     public void Calculate()
    17.     {
    18.         if (_currentScale == transform.localScale) return;
    19.         if (CheckForDefaultSize()) return;
    20.  
    21.         _currentScale = transform.localScale;
    22.         var mesh = GetMesh();
    23.         mesh.uv = SetupUvMap(mesh.uv);
    24.         mesh.name = "Cube Instance";
    25.  
    26.         if (GetComponent<Renderer>().sharedMaterial.mainTexture.wrapMode != TextureWrapMode.Repeat)
    27.         {
    28.             GetComponent<Renderer>().sharedMaterial.mainTexture.wrapMode = TextureWrapMode.Repeat;
    29.         }
    30.     }
    31.  
    32.     private Mesh GetMesh()
    33.     {
    34.         Mesh mesh;
    35.  
    36. #if UNITY_EDITOR
    37.  
    38.         var meshFilter = GetComponent<MeshFilter>();
    39.         var meshCopy = Instantiate(meshFilter.sharedMesh);
    40.         mesh = meshFilter.sharedMesh = meshCopy;
    41.  
    42. #else
    43.        
    44.         mesh = GetComponent<MeshFilter>().mesh;
    45.  
    46. #endif
    47.  
    48.         return mesh;
    49.     }
    50.  
    51.     private Vector2[] SetupUvMap(Vector2[] meshUVs)
    52.     {
    53.         var width = _currentScale.x;
    54.         var depth = _currentScale.z;
    55.         var height = _currentScale.y;
    56.  
    57.         //Front
    58.         meshUVs[2] = new Vector2(0, height);
    59.         meshUVs[3] = new Vector2(width, height);
    60.         meshUVs[0] = new Vector2(0, 0);
    61.         meshUVs[1] = new Vector2(width, 0);
    62.  
    63.         //Back
    64.         meshUVs[7] = new Vector2(0, 0);
    65.         meshUVs[6] = new Vector2(width, 0);
    66.         meshUVs[11] = new Vector2(0, height);
    67.         meshUVs[10] = new Vector2(width, height);
    68.  
    69.         //Left
    70.         meshUVs[19] = new Vector2(depth, 0);
    71.         meshUVs[17] = new Vector2(0, height);
    72.         meshUVs[16] = new Vector2(0, 0);
    73.         meshUVs[18] = new Vector2(depth, height);
    74.  
    75.         //Right
    76.         meshUVs[23] = new Vector2(depth, 0);
    77.         meshUVs[21] = new Vector2(0, height);
    78.         meshUVs[20] = new Vector2(0, 0);
    79.         meshUVs[22] = new Vector2(depth, height);
    80.  
    81.         //Top
    82.         meshUVs[4] = new Vector2(width, 0);
    83.         meshUVs[5] = new Vector2(0, 0);
    84.         meshUVs[8] = new Vector2(width, depth);
    85.         meshUVs[9] = new Vector2(0, depth);
    86.  
    87.         //Bottom
    88.         meshUVs[13] = new Vector2(width, 0);
    89.         meshUVs[14] = new Vector2(0, 0);
    90.         meshUVs[12] = new Vector2(width, depth);
    91.         meshUVs[15] = new Vector2(0, depth);
    92.  
    93.         return meshUVs;
    94.     }
    95.  
    96.     private bool CheckForDefaultSize()
    97.     {
    98.         if (_currentScale != Vector3.one) return false;
    99.  
    100.         var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    101.  
    102.         DestroyImmediate(GetComponent<MeshFilter>());
    103.         gameObject.AddComponent<MeshFilter>();
    104.         GetComponent<MeshFilter>().sharedMesh = cube.GetComponent<MeshFilter>().sharedMesh;
    105.  
    106.         DestroyImmediate(cube);
    107.  
    108.         return true;
    109.     }
    110. }
    As far as I can tell its got something to do with the unwrapping in Blender.

    Thank you.
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    837
    That script works specifically with the standard Unity cube, it's not a generic solution, and I don't think the general idea would work on anything else than a cube.
    I fail to see how you would get this to work on any other shape.
     
  3. Loki18000

    Loki18000

    Joined:
    Apr 1, 2015
    Posts:
    20
    It was my mistake, I was not reading the script correctly. I went another route involving Blender that fixed my issue.