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 } }
public void OnRenderObject() { if (_doRenderAtlas) { //assett rs must be same length as textureSets; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); for (int i = 0; i < rs.Length; i++) { MB3_TextureCombiner.MeshBakerMaterialTexture texInfo = textureSets[i].ts[indexOfTexSetToRender]; if (LOG_LEVEL >= MB2_LogLevel.trace && texInfo.t != null) { Debug.Log("Added " + texInfo.t + " to atlas w=" + texInfo.t.width + " h=" + texInfo.t.height + " offset=" + texInfo.matTilingRect.min + " scale=" + texInfo.matTilingRect.size + " rect=" + rs[i] + " padding=" + _padding); _printTexture(texInfo.t); } CopyScaledAndTiledToAtlas(textureSets[i], texInfo, textureSets[i].obUVoffset, textureSets[i].obUVscale, rs[i], _texPropertyName, _resultMaterialTextureBlender); } sw.Stop(); sw.Start(); if (LOG_LEVEL >= MB2_LogLevel.debug) { Debug.Log("Total time for Graphics.DrawTexture calls " + (sw.ElapsedMilliseconds).ToString("f5")); } if (LOG_LEVEL >= MB2_LogLevel.debug) { Debug.Log("Copying RenderTexture to Texture2D."); } //Convert the render texture to a Texture2D Texture2D tempTexture; tempTexture = new Texture2D(_destinationTexture.width, _destinationTexture.height, TextureFormat.ARGB32, true); int xblocks = _destinationTexture.width / 512; int yblocks = _destinationTexture.height / 512; if (xblocks == 0 || yblocks == 0) { if (LOG_LEVEL >= MB2_LogLevel.trace) { Debug.Log("Copying all in one shot"); } RenderTexture.active = _destinationTexture; tempTexture.ReadPixels(new Rect(0, 0, _destinationTexture.width, _destinationTexture.height), 0, 0, true); RenderTexture.active = null; } else { if (IsOpenGL()) { if (LOG_LEVEL >= MB2_LogLevel.trace) { Debug.Log("OpenGL copying blocks"); } for (int x = 0; x < xblocks; x++) { for (int y = 0; y < yblocks; y++) { RenderTexture.active = _destinationTexture; tempTexture.ReadPixels(new Rect(x * 512, y * 512, 512, 512), x * 512, y * 512, true); RenderTexture.active = null; } } } else { if (LOG_LEVEL >= MB2_LogLevel.trace) { Debug.Log("Not OpenGL copying blocks"); } for (int x = 0; x < xblocks; x++) { for (int y = 0; y < yblocks; y++) { RenderTexture.active = _destinationTexture; tempTexture.ReadPixels(new Rect(x * 512, _destinationTexture.height - 512 - y * 512, 512, 512), x * 512, y * 512, true); RenderTexture.active = null; } } } } tempTexture.Apply(); //if (LOG_LEVEL >= MB2_LogLevel.trace) { // _printTexture(tempTexture); //} myCamera.targetTexture = null; RenderTexture.active = null; targTex = tempTexture; if (LOG_LEVEL >= MB2_LogLevel.debug) { Debug.Log("Total time to copy RenderTexture to Texture2D " + (sw.ElapsedMilliseconds).ToString("f5")); } } }
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 void OnRenderObject() { if (this._doRenderAtlas) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < this.rs.Length; i++) { MB3_TextureCombiner.MeshBakerMaterialTexture meshBakerMaterialTexture = this.textureSets[i].ts[this.indexOfTexSetToRender]; if (this.LOG_LEVEL >= MB2_LogLevel.trace && meshBakerMaterialTexture.t != null) { UnityEngine.Debug.Log(string.Concat(new object[] { "Added ", meshBakerMaterialTexture.t, " to atlas w=", meshBakerMaterialTexture.t.width, " h=", meshBakerMaterialTexture.t.height, " offset=", meshBakerMaterialTexture.offset, " scale=", meshBakerMaterialTexture.scale, " rect=", this.rs[i], " padding=", this._padding })); this._printTexture(meshBakerMaterialTexture.t); } this.CopyScaledAndTiledToAtlas(meshBakerMaterialTexture, this.rs[i]); } stopwatch.Stop(); stopwatch.Start(); if (this.LOG_LEVEL >= MB2_LogLevel.debug) { UnityEngine.Debug.Log("Total time for Graphics.DrawTexture calls " + stopwatch.ElapsedMilliseconds.ToString("f5")); } if (this.LOG_LEVEL >= MB2_LogLevel.debug) { UnityEngine.Debug.Log("Copying RenderTexture to Texture2D."); } Texture2D texture2D = new Texture2D(this._destinationTexture.width, this._destinationTexture.height, TextureFormat.ARGB32, true); int num = this._destinationTexture.width / 512; int num2 = this._destinationTexture.height / 512; if (num == 0 || num2 == 0) { if (this.LOG_LEVEL >= MB2_LogLevel.trace) { UnityEngine.Debug.Log("Copying all in one shot"); } RenderTexture.active = this._destinationTexture; texture2D.ReadPixels(new Rect(0f, 0f, (float)this._destinationTexture.width, (float)this._destinationTexture.height), 0, 0, true); RenderTexture.active = null; } else if (this.IsOpenGL()) { if (this.LOG_LEVEL >= MB2_LogLevel.trace) { UnityEngine.Debug.Log("OpenGL copying blocks"); } for (int j = 0; j < num; j++) { for (int k = 0; k < num2; k++) { RenderTexture.active = this._destinationTexture; texture2D.ReadPixels(new Rect((float)(j * 512), (float)(k * 512), 512f, 512f), j * 512, k * 512, true); RenderTexture.active = null; } } } else { if (this.LOG_LEVEL >= MB2_LogLevel.trace) { UnityEngine.Debug.Log("Not OpenGL copying blocks"); } for (int l = 0; l < num; l++) { for (int m = 0; m < num2; m++) { RenderTexture.active = this._destinationTexture; texture2D.ReadPixels(new Rect((float)(l * 512), (float)(this._destinationTexture.height - 512 - m * 512), 512f, 512f), l * 512, m * 512, true); RenderTexture.active = null; } } } texture2D.Apply(); if (this.LOG_LEVEL >= MB2_LogLevel.trace) { this._printTexture(texture2D); } this.myCamera.targetTexture = null; RenderTexture.active = null; this.targTex = texture2D; if (this.LOG_LEVEL >= MB2_LogLevel.debug) { UnityEngine.Debug.Log("Total time to copy RenderTexture to Texture2D " + stopwatch.ElapsedMilliseconds.ToString("f5")); } } }
private void CopyScaledAndTiledToAtlas(MB3_TextureCombiner.MeshBakerMaterialTexture source, Rect rec) { Rect rect = rec; this.myCamera.backgroundColor = source.colorIfNoTexture; if (source.t == null) { source.t = this.combiner._createTemporaryTexture(16, 16, TextureFormat.ARGB32, true); UnityEngine.Debug.Log(string.Concat(new object[] { "Creating texture with color ", source.colorIfNoTexture, " isNormal", this._isNormalMap })); MB_Utility.setSolidColor(source.t, source.colorIfNoTexture); } rect.y = 1f - (rect.y + rect.height); rect.x *= (float)this._destinationTexture.width; rect.y *= (float)this._destinationTexture.height; rect.width *= (float)this._destinationTexture.width; rect.height *= (float)this._destinationTexture.height; Rect rect2 = rect; rect2.x -= (float)this._padding; rect2.y -= (float)this._padding; rect2.width += (float)(this._padding * 2); rect2.height += (float)(this._padding * 2); Rect rect3 = default(Rect); Rect screenRect = default(Rect); rect3.width = source.scale.x; rect3.height = source.scale.y; rect3.x = source.offset.x; rect3.y = source.offset.y; if (this._fixOutOfBoundsUVs) { rect3.width *= source.obUVscale.x; rect3.height *= source.obUVscale.y; rect3.x += source.obUVoffset.x; rect3.y += source.obUVoffset.y; if (this.LOG_LEVEL >= MB2_LogLevel.trace) { UnityEngine.Debug.Log("Fixing out of bounds UVs for tex " + source.t); } } Texture t = source.t; TextureWrapMode wrapMode = t.wrapMode; if (rect3.width == 1f && rect3.height == 1f && rect3.x == 0f && rect3.y == 0f) { t.wrapMode = TextureWrapMode.Clamp; } else { t.wrapMode = TextureWrapMode.Repeat; } if (this.LOG_LEVEL >= MB2_LogLevel.trace) { UnityEngine.Debug.Log(string.Concat(new object[] { "DrawTexture tex=", t.name, " destRect=", rect, " srcRect=", rect3, " Mat=", this.mat })); } Rect sourceRect = default(Rect); sourceRect.x = rect3.x; sourceRect.y = rect3.y + 1f - 1f / (float)t.height; sourceRect.width = rect3.width; sourceRect.height = 1f / (float)t.height; screenRect.x = rect.x; screenRect.y = rect2.y; screenRect.width = rect.width; screenRect.height = (float)this._padding; Graphics.DrawTexture(screenRect, t, sourceRect, 0, 0, 0, 0, this.mat); sourceRect.x = rect3.x; sourceRect.y = rect3.y; sourceRect.width = rect3.width; sourceRect.height = 1f / (float)t.height; screenRect.x = rect.x; screenRect.y = rect.y + rect.height; screenRect.width = rect.width; screenRect.height = (float)this._padding; Graphics.DrawTexture(screenRect, t, sourceRect, 0, 0, 0, 0, this.mat); sourceRect.x = rect3.x; sourceRect.y = rect3.y; sourceRect.width = 1f / (float)t.width; sourceRect.height = rect3.height; screenRect.x = rect2.x; screenRect.y = rect.y; screenRect.width = (float)this._padding; screenRect.height = rect.height; Graphics.DrawTexture(screenRect, t, sourceRect, 0, 0, 0, 0, this.mat); sourceRect.x = rect3.x + 1f - 1f / (float)t.width; sourceRect.y = rect3.y; sourceRect.width = 1f / (float)t.width; sourceRect.height = rect3.height; screenRect.x = rect.x + rect.width; screenRect.y = rect.y; screenRect.width = (float)this._padding; screenRect.height = rect.height; Graphics.DrawTexture(screenRect, t, sourceRect, 0, 0, 0, 0, this.mat); sourceRect.x = rect3.x; sourceRect.y = rect3.y + 1f - 1f / (float)t.height; sourceRect.width = 1f / (float)t.width; sourceRect.height = 1f / (float)t.height; screenRect.x = rect2.x; screenRect.y = rect2.y; screenRect.width = (float)this._padding; screenRect.height = (float)this._padding; Graphics.DrawTexture(screenRect, t, sourceRect, 0, 0, 0, 0, this.mat); sourceRect.x = rect3.x + 1f - 1f / (float)t.width; sourceRect.y = rect3.y + 1f - 1f / (float)t.height; sourceRect.width = 1f / (float)t.width; sourceRect.height = 1f / (float)t.height; screenRect.x = rect.x + rect.width; screenRect.y = rect2.y; screenRect.width = (float)this._padding; screenRect.height = (float)this._padding; Graphics.DrawTexture(screenRect, t, sourceRect, 0, 0, 0, 0, this.mat); sourceRect.x = rect3.x; sourceRect.y = rect3.y; sourceRect.width = 1f / (float)t.width; sourceRect.height = 1f / (float)t.height; screenRect.x = rect2.x; screenRect.y = rect.y + rect.height; screenRect.width = (float)this._padding; screenRect.height = (float)this._padding; Graphics.DrawTexture(screenRect, t, sourceRect, 0, 0, 0, 0, this.mat); sourceRect.x = rect3.x + 1f - 1f / (float)t.width; sourceRect.y = rect3.y; sourceRect.width = 1f / (float)t.width; sourceRect.height = 1f / (float)t.height; screenRect.x = rect.x + rect.width; screenRect.y = rect.y + rect.height; screenRect.width = (float)this._padding; screenRect.height = (float)this._padding; Graphics.DrawTexture(screenRect, t, sourceRect, 0, 0, 0, 0, this.mat); Graphics.DrawTexture(rect, t, rect3, 0, 0, 0, 0, this.mat); t.wrapMode = wrapMode; }
private void CopyScaledAndTiledToAtlas(MB3_TextureCombiner.MeshBakerMaterialTexture source, Vector2 obUVoffset, Vector2 obUVscale, Rect rec) { Rect r = rec; myCamera.backgroundColor = source.colorIfNoTexture; if (source.t == null) { source.t = combiner._createTemporaryTexture(16, 16, TextureFormat.ARGB32, true); Debug.Log("Creating texture with color " + source.colorIfNoTexture + " isNormal" + _isNormalMap); MB_Utility.setSolidColor(source.t, source.colorIfNoTexture); } 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); } } Texture tex = source.t; //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); } }
public void OnRenderObject() { Debug.Log("FastRenderAtlas.OnRenderObject num rects=" + rs.Length); if (_doRenderAtlas) { //assett rs must be same length as textureSets; for (int i = 0; i < rs.Length; i++) { MB3_TextureCombiner.MeshBakerMaterialTexture texInfo = textureSets[i].ts[indexOfTexSetToRender]; // texInfo.t = textureSets[i].ts[indexOfTexSetToRender].t; // texInfo.offset.x = 0f; // texInfo.offset.y = 0f; // texInfo.scale.x = 1f; // texInfo.scale.y = 1f;] Debug.Log("Added " + texInfo.t + " to atlas w=" + texInfo.t.width + " h=" + texInfo.t.height + " offset=" + texInfo.offset + " scale=" + texInfo.scale + " rect=" + rs[i]); CopyScaledAndTiledToAtlas(texInfo, rs[i], _padding, false, _destinationTexture.width, false); } //Convert the render texture to a Texture2D Texture2D tempTexture; tempTexture = new Texture2D(_destinationTexture.width, _destinationTexture.height, TextureFormat.ARGB32, true); int xblocks = _destinationTexture.width / 512; int yblocks = _destinationTexture.height / 512; if (xblocks == 0 || yblocks == 0) { RenderTexture.active = _destinationTexture; tempTexture.ReadPixels(new Rect(0, 0, _destinationTexture.width, _destinationTexture.height), 0, 0, true); RenderTexture.active = null; } else { // figures that ReadPixels works differently on OpenGL and DirectX, someday this code will break because Unity fixes this bug! if (IsOpenGL()) { for (int x = 0; x < xblocks; x++) { for (int y = 0; y < yblocks; y++) { RenderTexture.active = _destinationTexture; tempTexture.ReadPixels(new Rect(x * 512, y * 512, 512, 512), x * 512, y * 512, true); RenderTexture.active = null; } } } else { for (int x = 0; x < xblocks; x++) { for (int y = 0; y < yblocks; y++) { RenderTexture.active = _destinationTexture; tempTexture.ReadPixels(new Rect(x * 512, _destinationTexture.height - 512 - y * 512, 512, 512), x * 512, y * 512, true); RenderTexture.active = null; } } } } tempTexture.Apply(); myCamera.targetTexture = null; RenderTexture.active = null; targTex = tempTexture; Debug.Log("in Render"); } }
private void CopyScaledAndTiledToAtlas(MB3_TextureCombiner.MeshBakerMaterialTexture source, Rect rec, int _padding, bool _fixOutOfBoundsUVs, int maxSize, bool isNormalMap) { Rect r = rec; r.x += _padding; r.y += _padding; r.width -= _padding * 2; r.height -= _padding * 2; //fill the _padding Rect srcPrTex = new Rect(); Rect targPr = new Rect(); srcPrTex.width = source.scale.x; srcPrTex.height = source.scale.y; srcPrTex.x = source.offset.x; srcPrTex.y = source.offset.y; if (_fixOutOfBoundsUVs) { srcPrTex.width *= source.obUVscale.x; srcPrTex.height *= source.obUVscale.y; srcPrTex.x += source.obUVoffset.x; srcPrTex.y += source.obUVoffset.y; } Texture tex = source.t; //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; } Graphics.DrawTexture(r, tex, srcPrTex, 0, 0, 0, 0, mat); //TODO fill the _padding Rect srcPr = new Rect(); //top margin srcPr.x = srcPrTex.x; srcPr.y = srcPrTex.y + 1f - 1f / tex.height; srcPr.width = srcPrTex.width; srcPr.height = 1f / tex.height; targPr.x = rec.x + _padding; targPr.y = rec.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 = rec.x + _padding; targPr.y = rec.y + r.height + _padding; 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 = rec.x; targPr.y = rec.y + _padding; 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 = rec.x + r.width + _padding; targPr.y = rec.y + _padding; 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; srcPr.width = 1f / tex.width; srcPr.height = 1f / tex.height; targPr.x = rec.x; targPr.y = rec.y; targPr.width = _padding; targPr.height = _padding; Graphics.DrawTexture(targPr, tex, srcPr, 0, 0, 0, 0, mat); tex.wrapMode = oldTexWrapMode; }