Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Combing Meshes - Keep The Texture Scale The Same?

Discussion in 'Scripting' started by Jakeiiii, Jun 10, 2017.

  1. Jakeiiii

    Jakeiiii

    Joined:
    Jul 7, 2014
    Posts:
    258
    So I'm using the CombineMeshes() script for a bunch of rocks; they're all the same prefab with the same material, just each in different positions, rotations and scales. So it combines them well and preserves their appearance, however the texture on the material becomes scaled down by 10x exactly.
    The script runs at Start() so in the scene view before playing the game the objects all look good, but after it runs I have to change the tiling scale (x and y) to 0.1 of the texture on the material so that it looks like it did before the script ran. I can deal with this for now, but when I go back to editing in the scene with the new offset, the textures are 10x too large on the rocks (of course this now looks fine in play mode but I would like to see everything look how it's meant to in both scene editing and play mode).
    How do I make it so that when they combine they keep the same texture scale in the shared material? Without having to manually change the offset on Play and Exit constantly? Please don't link me to an asset store page.

    This is the code I'm using:
    Code (CSharp):
    1. void Start(){
    2.         foreach(Transform child in transform)
    3.             child.position += transform.position;
    4.         transform.position =  Vector3.zero;
    5.         transform.rotation = Quaternion.identity;
    6.  
    7.         MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
    8.         CombineInstance[] combine = new CombineInstance[meshFilters.Length-1];
    9.         int index = 0;
    10.  
    11.         for(int i = 0; i < meshFilters.Length; i++){
    12.             if(meshFilters[i].sharedMesh == null) continue;
    13.             combine[index].mesh = meshFilters[i].sharedMesh;
    14.             combine[index++].transform = meshFilters[i].transform.localToWorldMatrix;
    15.             meshFilters[i].GetComponent<Renderer>().enabled = false;
    16.         }
    17.  
    18.         GetComponent<MeshFilter>().mesh = new Mesh();
    19.         GetComponent<MeshFilter>().mesh.CombineMeshes (combine);
    20.         GetComponent<Renderer>().material = meshFilters[1].GetComponent<Renderer>().sharedMaterial;
    21.     }
    EDIT: As a temporary workaround I've had it set the texture scale in Start() and then reverse it in OnApplicationQuit(); I should mention I am using the UV-free materials asset from the store so I don't know if that's part of the issue.
     
    Last edited: Jun 10, 2017
unityunity