private static string folderToSavePrefabs = "";//set on OptimizeShader(); //creates a new optimizable shader and initializes it with an initial object. //the initial object is needed as we need to parse the standard shader in case there is one. public OptimizableShader(string name, OptimizableObject initialObject, bool combineObject) { optimizedObjects = new List <Tuple <GameObject, int> >(); shaderName = "";//by default objectsToOptimize = new List <OptimizableObject>(); combineMeshesFlags = new List <bool>(); cacheAtlasSizeNoReuseTextures = NO_CACHED; cacheAtlasSizeReuseTextures = NO_CACHED; //if the initial object is not null this means we have to check if the initial object has a standard shader if (initialObject != null) { AddObject(initialObject, combineObject); if (Utils.IsShaderStandard(name)) { standardShader = true; shaderName = Utils.ParseStandardShaderName(initialObject.ObjectMaterial); } else//shader is a normal shader, no need to parse its name { standardShader = false; shaderName = name; } } }
private uint GetKey(OptimizableObject opt) { uint textureKey; string text = GetUniqueIdentifier(opt); textureKey = Utils.CalculateMD5Hash(text); return(textureKey); }
//adds an object to the objects list. //WARNING: this doesnt care if the optimizable object obj matches the shader name // of this object. // Later on the ObjSorter.cs->SortObjects() organizes them to match public void AddObject(OptimizableObject obj) { /*if(shaderName != "" && obj != null && shaderName != obj.ShaderName) { * Debug.LogWarning("ERROR: Adding obj to a not matching optimizable shader"); * return; * }*/ objects.Add(obj); }
public void RemoveObject(OptimizableObject obj) { if (!objects.Remove(obj)) { Debug.LogError("Couldnt remove object"); } // else // Debug.Log("Added obj"); }
//this is used when we want to know if a texture exists already or not (for calculating atlases sizes on the GUI) public void AddTextureRef(OptimizableObject opt) { uint textureKey = GetKey(opt); Rect dummyPosition = new Rect(0, 0, 0, 0); if (!knownTextureRefs.ContainsKey(textureKey)) { knownTextureRefs.Add(textureKey, dummyPosition); } }
public void AddTextureRef(OptimizableObject opt, Rect positionInAtlas, int indexOfTexture) { uint textureKey = GetKey(opt); if (!knownTextureRefs.ContainsKey(textureKey)) { knownTextureRefs.Add(textureKey, positionInAtlas); textureRefsIndexes.Add(indexOfTexture); } }
public bool TextureRefExists(OptimizableObject opt) { uint textureKey = GetKey(opt); if (knownTextureRefs.ContainsKey(textureKey)) { return(true); } else { return(false); } }
private uint GetKey(OptimizableObject opt) { uint textureKey; if (opt.MainTexture == null) { textureKey = Utils.CalculateMD5Hash(opt.GetColor().ToString()); } else { textureKey = Utils.CalculateMD5Hash(AssetDatabase.GetAssetPath(opt.MainTexture)); //TODO ADD OFFSET + SCALE, but thinkg same texture, diff tiles on any of the materials, what would happen... } return(textureKey); }
//Quick sort for organizing textures sizes. //ascending order private static void SortTexturesBySize(int left, int right, int position) { int leftHold = left; int rightHold = right; OptimizableObject pivotObj = optimizableShaders[position].Objects[left]; int pivot = pivotObj.TextureArea; while (left < right) { while ((optimizableShaders[position].Objects[right].TextureArea >= pivot) && (left < right)) { right--; } if (left != right) { optimizableShaders[position].Objects[left] = optimizableShaders[position].Objects[right]; left++; } while ((optimizableShaders[position].Objects[left].TextureArea <= pivot) && (left < right)) { left++; } if (left != right) { optimizableShaders[position].Objects[right] = optimizableShaders[position].Objects[left]; right--; } } optimizableShaders[position].Objects[left] = pivotObj; pivot = left; left = leftHold; right = rightHold; if (left < pivot) { SortTexturesBySize(left, pivot - 1, position); } if (right > pivot) { SortTexturesBySize(pivot + 1, right, position); } }
//gets a concatenated string of paths of the different textures the object has. //TODO ADD OFFSET + SCALE, but thinkg same texture, diff tiles on any of the materials, what would happen... private string GetUniqueIdentifier(OptimizableObject opt) { string uniqueStringID = ""; List <Texture2D> objTextures = ShaderManager.Instance.GetTexturesForObject(opt.ObjectMaterial, opt.ShaderName); for (int i = 0; i < objTextures.Count; i++) { if (objTextures[i] != null) { uniqueStringID += AssetDatabase.GetAssetPath(objTextures[i]); } } if (uniqueStringID == "") { uniqueStringID = opt.GetColor().ToString(); } return(uniqueStringID); }
//Adds an object to optimizableShaders[0], later on SortObjects() re organizes the objects inside. // Maybe SortObjects() can dissapear and AddObjects adds objects to the correct shader. think about this.. public static void AddObject(GameObject g) { OptimizableObject optObj = new OptimizableObject(g); optimizableShaders[0].AddObject(optObj); }
public Rect GetTextureRefPosition(OptimizableObject opt) { uint textureKey = GetKey(opt); return(knownTextureRefs[textureKey]); }
public void SetObjectAtIndex(int index, OptimizableObject obj) { objects[index] = obj; }
public void SetObjectAtIndex(int index, OptimizableObject obj, bool combineObj) { objectsToOptimize[index] = obj; combineMeshesFlags[index] = combineObj; }
// adds an object to the objects list. // WARNING: this doesnt care if the optimizable object obj matches the shader name // of this object. // Later on the ObjSorter.cs->SortObjects() organizes them to match public void AddObject(OptimizableObject obj, bool combineObj) { objectsToOptimize.Add(obj); combineMeshesFlags.Add(combineObj); }