MB3_TestRenderTextureTestHarness.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using DigitalOpus.MB.Core;
  5. #if !UNITY_WEBPLAYER
  6. using System.IO;
  7. #endif
  8. public class MB3_TestRenderTextureTestHarness : MonoBehaviour {
  9. public Texture2D input;
  10. public bool doColor;
  11. public Color32 color;
  12. public Texture2D Create3x3Tex() {
  13. Texture2D t = new Texture2D(3, 3, TextureFormat.ARGB32, false);
  14. Color32[] cs = new Color32[t.width * t.height];
  15. for (int i =0; i < cs.Length; i++) {
  16. cs[i] = color;
  17. }
  18. t.SetPixels32(cs);
  19. t.Apply();
  20. return t;
  21. }
  22. public Texture2D Create3x3Clone() {
  23. Texture2D t = new Texture2D(3, 3, TextureFormat.ARGB32, false);
  24. Color32[] cs = new Color32[] {
  25. new Color32(54, 54, 201, 255), new Color32(128, 37, 218, 255), new Color32(201, 54, 201, 255),
  26. new Color32(37, 128, 218, 255), new Color32(128, 128, 255, 255), new Color32(218, 128, 218, 255),
  27. new Color32(54, 201, 201, 255), new Color32(128, 218, 218, 255), new Color32(201, 201, 201, 255),
  28. };
  29. t.SetPixels32(cs);
  30. t.Apply();
  31. return t;
  32. }
  33. #if UNITY_EDITOR
  34. // Use this for initialization
  35. void Start () {
  36. Texture2D t = input;
  37. if (doColor) {
  38. t = Create3x3Clone();
  39. }
  40. TestRender(t, null);
  41. }
  42. #endif
  43. public static void TestRender(Texture2D input, Texture2D output) {
  44. int numAtlases = 1;
  45. ShaderTextureProperty[] texPropertyNames = new ShaderTextureProperty[] {
  46. new ShaderTextureProperty("_BumpMap",false)
  47. };
  48. int atlasSizeX = input.width;
  49. int atlasSizeY = input.height;
  50. int _padding = 0;
  51. Rect[] uvRects = new Rect[] { new Rect(0f, 0f, 1f, 1f) };
  52. List<MB_TexSet> distinctMaterialTextures = new List<MB_TexSet>();
  53. MeshBakerMaterialTexture[] dmts = new MeshBakerMaterialTexture[] {
  54. new MeshBakerMaterialTexture(input)
  55. };
  56. MB_TexSet texSet = new MB_TexSet(dmts,Vector2.zero,Vector2.one, MB_TextureTilingTreatment.considerUVs);
  57. distinctMaterialTextures.Add(texSet);
  58. GameObject renderAtlasesGO = null;
  59. renderAtlasesGO = new GameObject("MBrenderAtlasesGO");
  60. MB3_AtlasPackerRenderTexture atlasRenderTexture = renderAtlasesGO.AddComponent<MB3_AtlasPackerRenderTexture>();
  61. renderAtlasesGO.AddComponent<Camera>();
  62. for (int i = 0; i < numAtlases; i++) {
  63. Texture2D atlas = null;
  64. Debug.Log("About to render " + texPropertyNames[i].name + " isNormal=" + texPropertyNames[i].isNormalMap);
  65. atlasRenderTexture.LOG_LEVEL = MB2_LogLevel.trace;
  66. atlasRenderTexture.width = atlasSizeX;
  67. atlasRenderTexture.height = atlasSizeY;
  68. atlasRenderTexture.padding = _padding;
  69. atlasRenderTexture.rects = uvRects;
  70. atlasRenderTexture.textureSets = distinctMaterialTextures;
  71. atlasRenderTexture.indexOfTexSetToRender = i;
  72. atlasRenderTexture.isNormalMap = texPropertyNames[i].isNormalMap;
  73. // call render on it
  74. atlas = atlasRenderTexture.OnRenderAtlas(null);
  75. Debug.Log("Created atlas " + texPropertyNames[i].name + " w=" + atlas.width + " h=" + atlas.height + " id=" + atlas.GetInstanceID());
  76. Debug.Log("Color " + atlas.GetPixel(5,5) + " " + Color.red);
  77. #if !UNITY_WEBPLAYER
  78. byte[] bytes = atlas.EncodeToPNG();
  79. File.WriteAllBytes(Application.dataPath + "/_Experiment/red.png", bytes);
  80. #endif
  81. }
  82. }
  83. }