public static void Log(MBLogLevel l, String msg, MBLogLevel currentThreshold) { if (l <= currentThreshold) { if (l == MBLogLevel.error) { Debug.LogError(msg); } if (l == MBLogLevel.warn) { Debug.LogWarning(String.Format("frm={0} WARN {1}", Time.frameCount, msg)); } if (l == MBLogLevel.info) { Debug.Log(String.Format("frm={0} INFO {1}", Time.frameCount, msg)); } if (l == MBLogLevel.debug) { Debug.Log(String.Format("frm={0} DEBUG {1}", Time.frameCount, msg)); } if (l == MBLogLevel.trace) { Debug.Log(String.Format("frm={0} TRACE {1}", Time.frameCount, msg)); } } }
public static Vector2[] GetMeshUV1s(Mesh m, MBLogLevel LOG_LEVEL) { Vector2[] uv; if (LOG_LEVEL >= MBLogLevel.warn) { MBLog.LogDebug("UV1 does not exist in Unity 5+"); } uv = m.uv; if (uv.Length == 0) { if (LOG_LEVEL >= MBLogLevel.debug) { MBLog.LogDebug("Mesh " + m + " has no uv1s. Generating"); } if (LOG_LEVEL >= MBLogLevel.warn) { Debug.LogWarning("Mesh " + m + " didn't have uv1s. Generating uv1s."); } uv = new Vector2[m.vertexCount]; for (int i = 0; i < uv.Length; i++) { uv[i] = _HALF_UV; } } return(uv); }
public static Vector2[] GetMeshUV3orUV4(Mesh m, bool get3, MBLogLevel LOG_LEVEL) { Vector2[] uvs; if (get3) { uvs = m.uv3; } else { uvs = m.uv4; } if (uvs.Length == 0) { if (LOG_LEVEL >= MBLogLevel.debug) { MBLog.LogDebug("Mesh " + m + " has no uv" + (get3 ? "3" : "4") + ". Generating"); } uvs = new Vector2[m.vertexCount]; for (int i = 0; i < uvs.Length; i++) { uvs[i] = _HALF_UV; } } return(uvs); }
public bool isDirty = false; //needs apply public CombinedMesh(int maxNumVertsInMesh, GameObject resultSceneObject, MBLogLevel ll) { combinedMesh = new MeshCombinerSingle(); combinedMesh.resultSceneObject = resultSceneObject; combinedMesh.LOG_LEVEL = ll; extraSpace = maxNumVertsInMesh; numVertsInListToDelete = 0; numVertsInListToAdd = 0; gosToAdd = new List <GameObject>(); gosToDelete = new List <int>(); gosToUpdate = new List <GameObject>(); }
//a material can appear more than once in an atlas if using fixOutOfBoundsUVs. //in this case need to use the UV rect of the mesh to find the correct rectangle. public bool TryMapMaterialToUVRect(Material mat, Mesh m, int submeshIdx, MeshCombinerSingle.MeshChannelsCache meshChannelCache, Dictionary <int, MeshBakerUtility.MeshAnalysisResult[]> meshAnalysisCache, out Rect rectInAtlas, //out Rect subrectInAtlasMatTiling, out Rect encapsulatingRect, out Rect sourceMaterialTilingOut, ref String errorMsg, MBLogLevel logLevel) { if (tbr.materialsAndUVRects.Length == 0 && tbr.materials.Length > 0) { errorMsg = "The 'Material Bake Result' needs to be re-baked to be compatible with this version of Mesh Baker. Please re-bake using the MB3_TextureBaker."; rectInAtlas = new Rect(); //subrectInAtlasMatTiling = new Rect(); encapsulatingRect = new Rect(); sourceMaterialTilingOut = new Rect(); return(false); } if (mat == null) { rectInAtlas = new Rect(); //subrectInAtlasMatTiling = new Rect(); encapsulatingRect = new Rect(); sourceMaterialTilingOut = new Rect(); errorMsg = String.Format("Mesh {0} Had no material on submesh {1} cannot map to a material in the atlas", m.name, submeshIdx); return(false); } if (submeshIdx >= m.subMeshCount) { errorMsg = "Submesh index is greater than the number of submeshes"; rectInAtlas = new Rect(); //subrectInAtlasMatTiling = new Rect(); encapsulatingRect = new Rect(); sourceMaterialTilingOut = new Rect(); return(false); } //find the first index of this material int idx = -1; for (int i = 0; i < matsAndSrcUVRect.Length; i++) { if (mat == matsAndSrcUVRect[i].material) { idx = i; break; } } // if couldn't find material if (idx == -1) { rectInAtlas = new Rect(); //subrectInAtlasMatTiling = new Rect(); encapsulatingRect = new Rect(); sourceMaterialTilingOut = new Rect(); errorMsg = String.Format("Material {0} could not be found in the Material Bake Result", mat.name); return(false); } if (!tbr.fixOutOfBoundsUVs) { if (numTimesMatAppearsInAtlas[idx] != 1) { Debug.LogError("There is a problem with this TextureBakeResults. FixOutOfBoundsUVs is false and a material appears more than once."); } rectInAtlas = matsAndSrcUVRect[idx].atlasRect; //subrectInAtlasMatTiling = matsAndSrcUVRect[idx].atlasSubrectMaterialOnly; encapsulatingRect = matsAndSrcUVRect[idx].samplingEncapsulatinRect; sourceMaterialTilingOut = matsAndSrcUVRect[idx].sourceMaterialTiling; return(true); } else { //todo what if no UVs //Find UV rect in source mesh MeshBakerUtility.MeshAnalysisResult[] mar; if (!meshAnalysisCache.TryGetValue(m.GetInstanceID(), out mar)) { mar = new MeshBakerUtility.MeshAnalysisResult[m.subMeshCount]; for (int j = 0; j < m.subMeshCount; j++) { Vector2[] uvss = meshChannelCache.GetUv0Raw(m); MeshBakerUtility.hasOutOfBoundsUVs(uvss, m, ref mar[j], j); } meshAnalysisCache.Add(m.GetInstanceID(), mar); } //this could be a mesh that was not used in the texture baking that has huge UV tiling too big for the rect that was baked //find a record that has an atlas uvRect capable of containing this bool found = false; if (logLevel >= MBLogLevel.trace) { Debug.LogWarning(String.Format("Trying to find a rectangle in atlas capable of holding tiled sampling rect for mesh {0} using material {1}", m, mat)); } for (int i = idx; i < matsAndSrcUVRect.Length; i++) { if (matsAndSrcUVRect[i].material == mat) { //calculate what the UV rect in the atlas would be if we combined the material tiling of this rect with the UV tiling of this submesh Rect potentialRect = new Rect(); Rect uvR = mar[submeshIdx].uvRect; Rect matR = matsAndSrcUVRect[i].sourceMaterialTiling; // test to see if this would fit in what was baked in the atlas Rect rr = matsAndSrcUVRect[i].samplingEncapsulatinRect; UVTransformUtility.Canonicalize(ref rr, 0, 0); potentialRect = UVTransformUtility.CombineTransforms(ref uvR, ref matR); if (logLevel >= MBLogLevel.trace) { Debug.Log("uvR=" + uvR.ToString("f5") + " matR=" + matR.ToString("f5") + "Potential Rect " + potentialRect.ToString("f5") + " encapsulating=" + rr.ToString("f5")); } UVTransformUtility.Canonicalize(ref potentialRect, rr.x, rr.y); if (logLevel >= MBLogLevel.trace) { Debug.Log("Potential Rect Cannonical " + potentialRect.ToString("f5") + " encapsulating=" + rr.ToString("f5")); } if (UVTransformUtility.RectContains(ref rr, ref potentialRect)) { if (logLevel >= MBLogLevel.trace) { Debug.Log("Found rect in atlas capable of containing tiled sampling rect for mesh " + m + " at idx=" + i); } idx = i; found = true; break; } } } if (found) { rectInAtlas = matsAndSrcUVRect[idx].atlasRect; //subrectInAtlasMatTiling = matsAndSrcUVRect[idx].atlasSubrectMaterialOnly; encapsulatingRect = matsAndSrcUVRect[idx].samplingEncapsulatinRect; sourceMaterialTilingOut = matsAndSrcUVRect[idx].sourceMaterialTiling; return(true); } else { rectInAtlas = new Rect(); //subrectInAtlasMatTiling = new Rect(); encapsulatingRect = new Rect(); sourceMaterialTilingOut = new Rect(); errorMsg = String.Format("Could not find a tiled rectangle in the atlas capable of containing the uv and material tiling on mesh {0}", m.name); return(false); } } }