/// <summary> /// Exports the textures and materials of the atlased objects /// </summary> /// <param name="realAssetPath">Real asset path used by System.IO</param> private void ExportTexturesAndMaterials(string realAssetPath) { foreach (Transform t in _hiddenCombinedObject.transform) { Renderer currentRenderer = t.GetComponentInChildren <Renderer>(); if (currentRenderer != null) { foreach (ShaderProperties property in TextureCombineUtility.GetShaderProperties(currentRenderer.sharedMaterial, _textureAtlasProperties)) { currentRenderer.sharedMaterial.SetTexture(property.propertyName, ExportTexture(currentRenderer.sharedMaterial.GetTexture(property.propertyName), currentRenderer.name + property.propertyName, realAssetPath, property.markAsNormal)); } currentRenderer.sharedMaterial = ExportMaterial(currentRenderer.sharedMaterial, currentRenderer.name + "_MAT"); } } }
/// <summary> /// Builds the combined meshes using data from the atlased dictionary /// </summary> void BuildAtlasedMeshes() { _numOfExportedMeshes = 0; foreach (KeyValuePair <string, Dictionary <Material, List <MeshInstance> > > firstPass in _dataTreeWithAtlasing) { List <Material> allMaterialTextures = new List <Material> (firstPass.Value.Keys); ReimportNonReadonlyTextures(allMaterialTextures); TextureCombineOutput textureCombineOutput = TextureCombineUtility.Combine(allMaterialTextures, _textureAtlasProperties, true); if (textureCombineOutput != null && textureCombineOutput.texturePositions != null) { List <MeshInstance> meshIntermediates = new List <MeshInstance> (); foreach (KeyValuePair <Material, List <MeshInstance> > kv in firstPass.Value) { TexturePosition refTexture = GetReferenceTexturePosition(textureCombineOutput.texturePositions, kv.Key.mainTexture.name); for (int i = 0; i < kv.Value.Count; i++) { OffsetUvs(kv.Value [i].mesh, refTexture.position); meshIntermediates.Add(kv.Value [i]); } } IList <Mesh> combinedMeshes = MeshCombineUtility.Combine(meshIntermediates); string objectName = firstPass.Key.Remove(firstPass.Key.LastIndexOf(' ')); GameObject parent = new GameObject("Combined " + objectName + " Mesh Parent"); parent.transform.parent = _hiddenCombinedObject.transform; parent.transform.position = _hiddenCombinedObject.transform.position; parent.transform.rotation = _hiddenCombinedObject.transform.rotation; for (int i = 0; i < combinedMeshes.Count; i++) { GameObject go = new GameObject("Combined " + objectName + " Mesh"); go.transform.parent = parent.transform; go.transform.localScale = Vector3.one; go.transform.localRotation = Quaternion.identity; go.transform.localPosition = Vector3.zero; MeshFilter filter = go.AddComponent <MeshFilter> (); go.AddComponent <MeshRenderer> ().sharedMaterial = textureCombineOutput.combinedMaterial; filter.mesh = combinedMeshes [i]; _numOfExportedMeshes++; } } } }
/// <summary> /// The main combining method. This calls combining methods and organizes game objects in the scene /// </summary> /// <param name="allMeshesAndMaterials">The dictionary containing all of the data</param> void CreateBatchedAtlasedObjects(IDictionary <string, Dictionary <Material, List <MeshInstance> > > allMeshesAndMaterials) { foreach (KeyValuePair <string, Dictionary <Material, List <MeshInstance> > > firstPass in allMeshesAndMaterials) { List <Material> allMaterialTextures = new List <Material>(firstPass.Value.Keys); TextureCombineOutput textureCombineOutput = TextureCombineUtility.Combine(allMaterialTextures, _textureAtlasProperties); if (textureCombineOutput != null && textureCombineOutput.texturePositions != null) { List <MeshInstance> meshIntermediates = new List <MeshInstance>(); foreach (KeyValuePair <Material, List <MeshInstance> > kv in firstPass.Value) { TexturePosition refTexture = GetReferenceTexturePosition(textureCombineOutput.texturePositions, kv.Key.mainTexture.name); for (int i = 0; i < kv.Value.Count; i++) { OffsetUvs(kv.Value[i].mesh, refTexture.position); meshIntermediates.Add(kv.Value[i]); } } IList <Mesh> combinedMeshes = MeshCombineUtility.Combine(meshIntermediates); GameObject parent = new GameObject("Combined " + gameObject.name + " " + firstPass.Key + " Mesh Parent"); parent.transform.position = transform.position; parent.transform.rotation = transform.rotation; for (int i = 0; i < combinedMeshes.Count; i++) { GameObject go = new GameObject("Combined " + gameObject.name + " Mesh"); go.transform.parent = parent.transform; go.tag = gameObject.tag; go.layer = gameObject.layer; go.transform.localScale = Vector3.one; go.transform.localRotation = Quaternion.identity; go.transform.localPosition = Vector3.zero; MeshFilter filter = go.AddComponent <MeshFilter>(); go.AddComponent <MeshRenderer>().sharedMaterial = textureCombineOutput.combinedMaterial; filter.mesh = combinedMeshes[i]; } } } }