public static void TestRender(Texture2D input, Texture2D output) { int numAtlases = 1; ShaderTextureProperty[] texPropertyNames = new ShaderTextureProperty[] { new ShaderTextureProperty("_BumpMap", false) }; int atlasSizeX = input.width; int atlasSizeY = input.height; int _padding = 0; Rect[] uvRects = new Rect[] { new Rect(0f, 0f, 1f, 1f) }; List <MB3_TextureCombiner.MB_TexSet> distinctMaterialTextures = new List <MB3_TextureCombiner.MB_TexSet>(); MB3_TextureCombiner.MeshBakerMaterialTexture[] dmts = new MB3_TextureCombiner.MeshBakerMaterialTexture[] { new MB3_TextureCombiner.MeshBakerMaterialTexture(input) }; MB3_TextureCombiner.MB_TexSet texSet = new MB3_TextureCombiner.MB_TexSet(dmts, Vector2.zero, Vector2.one); distinctMaterialTextures.Add(texSet); GameObject renderAtlasesGO = null; renderAtlasesGO = new GameObject("MBrenderAtlasesGO"); MB3_AtlasPackerRenderTexture atlasRenderTexture = renderAtlasesGO.AddComponent <MB3_AtlasPackerRenderTexture>(); renderAtlasesGO.AddComponent <Camera>(); for (int i = 0; i < numAtlases; i++) { Texture2D atlas = null; Debug.Log("About to render " + texPropertyNames[i].name + " isNormal=" + texPropertyNames[i].isNormalMap); atlasRenderTexture.LOG_LEVEL = MB2_LogLevel.trace; atlasRenderTexture.width = atlasSizeX; atlasRenderTexture.height = atlasSizeY; atlasRenderTexture.padding = _padding; atlasRenderTexture.rects = uvRects; atlasRenderTexture.textureSets = distinctMaterialTextures; atlasRenderTexture.indexOfTexSetToRender = i; atlasRenderTexture.isNormalMap = texPropertyNames[i].isNormalMap; // call render on it atlas = atlasRenderTexture.OnRenderAtlas(null); Debug.Log("Created atlas " + texPropertyNames[i].name + " w=" + atlas.width + " h=" + atlas.height + " id=" + atlas.GetInstanceID()); Debug.Log("Color " + atlas.GetPixel(5, 5) + " " + Color.red); #if !UNITY_WEBPLAYER byte[] bytes = atlas.EncodeToPNG(); File.WriteAllBytes(Application.dataPath + "/_Experiment/red.png", bytes); #endif } }
private void CopyScaledAndTiledToAtlas(MB3_TextureCombiner.MB_TexSet texSet, MB3_TextureCombiner.MeshBakerMaterialTexture source, Vector2 obUVoffset, Vector2 obUVscale, Rect rec, ShaderTextureProperty texturePropertyName, TextureBlender resultMatTexBlender) { Rect r = rec; if (resultMatTexBlender != null) { myCamera.backgroundColor = resultMatTexBlender.GetColorIfNoTexture(texSet.mats[0].mat, texturePropertyName); } else { myCamera.backgroundColor = MB3_TextureCombiner.GetColorIfNoTexture(texturePropertyName); } if (source.t == null) { source.t = combiner._createTemporaryTexture(16, 16, TextureFormat.ARGB32, true); } r.y = 1f - (r.y + r.height); // DrawTexture uses topLeft 0,0, Texture2D uses bottomLeft 0,0 r.x *= _destinationTexture.width; r.y *= _destinationTexture.height; r.width *= _destinationTexture.width; r.height *= _destinationTexture.height; Rect rPadded = r; rPadded.x -= _padding; rPadded.y -= _padding; rPadded.width += _padding * 2; rPadded.height += _padding * 2; Rect srcPrTex = source.matTilingRect.GetRect(); Rect targPr = new Rect(); if (_fixOutOfBoundsUVs) { Rect ruv = new Rect(obUVoffset.x, obUVoffset.y, obUVscale.x, obUVscale.y); srcPrTex = MB3_UVTransformUtility.CombineTransforms(ref srcPrTex, ref ruv); if (LOG_LEVEL >= MB2_LogLevel.trace) { Debug.Log("Fixing out of bounds UVs for tex " + source.t); } } Texture2D tex = source.t; /* * if (_considerNonTextureProperties && resultMatTexBlender != null) * { * if (LOG_LEVEL >= MB2_LogLevel.trace) Debug.Log(string.Format("Blending texture {0} mat {1} with non-texture properties using TextureBlender {2}", tex.name, texSet.mats[0].mat, resultMatTexBlender)); * * resultMatTexBlender.OnBeforeTintTexture(texSet.mats[0].mat, texturePropertyName.name); * //combine the tintColor with the texture * tex = combiner._createTextureCopy(tex); * for (int i = 0; i < tex.height; i++) * { * Color[] cs = tex.GetPixels(0, i, tex.width, 1); * for (int j = 0; j < cs.Length; j++) * { * cs[j] = resultMatTexBlender.OnBlendTexturePixel(texturePropertyName.name, cs[j]); * } * tex.SetPixels(0, i, tex.width, 1, cs); * } * tex.Apply(); * } */ //main texture TextureWrapMode oldTexWrapMode = tex.wrapMode; if (srcPrTex.width == 1f && srcPrTex.height == 1f && srcPrTex.x == 0f && srcPrTex.y == 0f) { //fixes bug where there is a dark line at the edge of the texture tex.wrapMode = TextureWrapMode.Clamp; } else { tex.wrapMode = TextureWrapMode.Repeat; } if (LOG_LEVEL >= MB2_LogLevel.trace) { Debug.Log("DrawTexture tex=" + tex.name + " destRect=" + r + " srcRect=" + srcPrTex + " Mat=" + mat); } //fill the padding first Rect srcPr = new Rect(); //top margin srcPr.x = srcPrTex.x; srcPr.y = srcPrTex.y + 1 - 1f / tex.height; srcPr.width = srcPrTex.width; srcPr.height = 1f / tex.height; targPr.x = r.x; targPr.y = rPadded.y; targPr.width = r.width; targPr.height = _padding; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //bot margin srcPr.x = srcPrTex.x; srcPr.y = srcPrTex.y; srcPr.width = srcPrTex.width; srcPr.height = 1f / tex.height; targPr.x = r.x; targPr.y = r.y + r.height; targPr.width = r.width; targPr.height = _padding; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //left margin srcPr.x = srcPrTex.x; srcPr.y = srcPrTex.y; srcPr.width = 1f / tex.width; srcPr.height = srcPrTex.height; targPr.x = rPadded.x; targPr.y = r.y; targPr.width = _padding; targPr.height = r.height; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //right margin srcPr.x = srcPrTex.x + 1f - 1f / tex.width; srcPr.y = srcPrTex.y; srcPr.width = 1f / tex.width; srcPr.height = srcPrTex.height; targPr.x = r.x + r.width; targPr.y = r.y; targPr.width = _padding; targPr.height = r.height; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //top left corner srcPr.x = srcPrTex.x; srcPr.y = srcPrTex.y + 1 - 1f / tex.height; srcPr.width = 1f / tex.width; srcPr.height = 1f / tex.height; targPr.x = rPadded.x; targPr.y = rPadded.y; targPr.width = _padding; targPr.height = _padding; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //top right corner srcPr.x = srcPrTex.x + 1f - 1f / tex.width; srcPr.y = srcPrTex.y + 1 - 1f / tex.height; srcPr.width = 1f / tex.width; srcPr.height = 1f / tex.height; targPr.x = r.x + r.width; targPr.y = rPadded.y; targPr.width = _padding; targPr.height = _padding; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //bot left corner srcPr.x = srcPrTex.x; srcPr.y = srcPrTex.y; srcPr.width = 1f / tex.width; srcPr.height = 1f / tex.height; targPr.x = rPadded.x; targPr.y = r.y + r.height; targPr.width = _padding; targPr.height = _padding; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //bot right corner srcPr.x = srcPrTex.x + 1f - 1f / tex.width; srcPr.y = srcPrTex.y; srcPr.width = 1f / tex.width; srcPr.height = 1f / tex.height; targPr.x = r.x + r.width; targPr.y = r.y + r.height; targPr.width = _padding; targPr.height = _padding; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); //now the texture Graphics.DrawTexture(r, tex, srcPrTex, 0, 0, 0, 0, mat); tex.wrapMode = oldTexWrapMode; }
public static void TestRender(Texture2D input, Texture2D output) { int num = 1; ShaderTextureProperty[] array = new ShaderTextureProperty[] { new ShaderTextureProperty("_BumpMap", false) }; int width = input.width; int height = input.height; int padding = 0; Rect[] rects = new Rect[] { new Rect(0f, 0f, 1f, 1f) }; List <MB3_TextureCombiner.MB_TexSet> list = new List <MB3_TextureCombiner.MB_TexSet>(); MB3_TextureCombiner.MeshBakerMaterialTexture[] tss = new MB3_TextureCombiner.MeshBakerMaterialTexture[] { new MB3_TextureCombiner.MeshBakerMaterialTexture(input) }; MB3_TextureCombiner.MB_TexSet item = new MB3_TextureCombiner.MB_TexSet(tss); list.Add(item); GameObject gameObject = new GameObject("MBrenderAtlasesGO"); MB3_AtlasPackerRenderTexture mb3_AtlasPackerRenderTexture = gameObject.AddComponent <MB3_AtlasPackerRenderTexture>(); gameObject.AddComponent <Camera>(); for (int i = 0; i < num; i++) { Debug.Log(string.Concat(new object[] { "About to render ", array[i].name, " isNormal=", array[i].isNormalMap })); mb3_AtlasPackerRenderTexture.LOG_LEVEL = MB2_LogLevel.trace; mb3_AtlasPackerRenderTexture.width = width; mb3_AtlasPackerRenderTexture.height = height; mb3_AtlasPackerRenderTexture.padding = padding; mb3_AtlasPackerRenderTexture.rects = rects; mb3_AtlasPackerRenderTexture.textureSets = list; mb3_AtlasPackerRenderTexture.indexOfTexSetToRender = i; mb3_AtlasPackerRenderTexture.isNormalMap = array[i].isNormalMap; Texture2D texture2D = mb3_AtlasPackerRenderTexture.OnRenderAtlas(null); Debug.Log(string.Concat(new object[] { "Created atlas ", array[i].name, " w=", texture2D.width, " h=", texture2D.height, " id=", texture2D.GetInstanceID() })); Debug.Log(string.Concat(new object[] { "Color ", texture2D.GetPixel(5, 5), " ", Color.red })); byte[] bytes = texture2D.EncodeToPNG(); File.WriteAllBytes(Application.dataPath + "/_Experiment/red.png", bytes); } }