public Texture Get(string name) { var bytes = store.Get(name); var unityTexture = new UnityEngine.Texture2D(0, 0); UnityEngine.ImageConversion.LoadImage(unityTexture, bytes); if (setWrapModeClamp) { unityTexture.wrapMode = UnityEngine.TextureWrapMode.Clamp; } if (setFilterModePoint) { unityTexture.filterMode = UnityEngine.FilterMode.Point; } if (transform8BitPng) { var pixels = unityTexture.GetPixels32(); for (int i = 0, len = pixels.Length; i < len; i += 1) { pixels[i].a = pixels[i].r; pixels[i].r = 255; pixels[i].g = 255; pixels[i].b = 255; } unityTexture.SetPixels32(pixels); unityTexture.Apply(); } return(new Texture(name, unityTexture)); }
/// <summary> /// Create texture from pixel array /// </summary> public void LoadImage(Color4[] pixels, int width, int height, bool generateMips = false) { Dispose(); unityTexture = new UnityEngine.Texture2D(width, height); SetTextureDefaultParameters(); var c = new UnityEngine.Color32[pixels.Length]; for (int i = 0; i < pixels.Length; i++) { c[i].a = pixels[i].A; c[i].r = pixels[i].R; c[i].g = pixels[i].G; c[i].b = pixels[i].B; } unityTexture.SetPixels32(c); unityTexture.Apply(generateMips); }
public unsafe UnityEngine.Texture2D RenderIntoTexture(char c) { int code; FT.FT_FaceRec facerec = FT.HandleToRecord <FT.FT_FaceRec>(face_); IntPtr slot = facerec.glyph; code = FT.FT_Load_Char(face_, c, FT.FT_LOAD_RENDER /*|FT.FT_LOAD_TARGET_NORMAL*/); if (code != 0) { return(null); } FT.FT_GlyphSlotRec slotrec = FT.HandleToRecord <FT.FT_GlyphSlotRec>(slot); FTSharp.FT.FT_Bitmap ftbm = slotrec.bitmap; UnityEngine.Texture2D texbuffer = new UnityEngine.Texture2D((int)ftbm.width, (int)ftbm.rows, UnityEngine.TextureFormat.ARGB32, false); if (((int)ftbm.pixel_mode) != 2) { UnityEngine.Debug.LogError("Unsupported bitmap depth :" + ((int)ftbm.pixel_mode).ToString()); return(null); } // FT_PIXEL_MODE_MONO, //FT_PIXEL_MODE_GRAY, UnityEngine.Color32 [] clrs = texbuffer.GetPixels32(); int i = 0; // UNSAFE DOES NOT WORK IN THE WEB PLAYER ! for (int y = 0; y < ftbm.rows; y++) { long j = (((ftbm.rows - (1 + y)) * ftbm.pitch)); byte *bo = ((byte *)ftbm.buffer); for (int x = 0; x < ftbm.width; x++) { clrs[i].a = clrs[i].r = clrs[i].g = clrs[i].b = (bo[j + x]); i++; } } texbuffer.SetPixels32(clrs); texbuffer.Apply(); return(texbuffer); }
/// <summary> /// Load subtexture from pixel array /// Warning: this method doesn't support automatic texture reload after restoring graphics context /// </summary> public void LoadSubImage(Color4[] pixels, int x, int y, int width, int height) { // This mindblowing code: // 1) Rearranges pixels in bottom to top layout; // 2) Clips pixels outside the texture rectangle. y = unityTexture.height - y - height; var x0 = Math.Max(0, x); var y0 = Math.Max(0, y); var x1 = Math.Min(unityTexture.width, x + width); var y1 = Math.Min(unityTexture.height, y + height); if (x1 <= x0 || y1 <= y0) { return; } var stride = width; width = x1 - x0; height = y1 - y0; var c = new UnityEngine.Color32[width * height]; int k = 0; for (var i = y1 - 1; i >= y0; i--) { var p = (i - y) * stride + (x0 - x); for (var j = x0; j < x1; j++) { c[k].a = pixels[p].A; c[k].r = pixels[p].R; c[k].g = pixels[p].G; c[k].b = pixels[p].B; k++; p++; } } unityTexture.SetPixels32(x0, y0, width, height, c); unityTexture.Apply(); }
public void Bake() { var tSize = TextureSize; _bakedTexture = new Bitmap(tSize.Width, tSize.Height); // Clear baked texture with transparent color. for (int y = 0; y < _bakedTexture.Height; y++) { for (int x = 0; x < _bakedTexture.Width; x++) { _bakedTexture.SetPixel(x, y, Color.Transparent); } } int xOffset = 0; for (int i = 0; i < Text.Length; i++) { if (_font.textureList.ContainsKey(Text[i]) == false) { continue; } var textC = _font.textureList[Text[i]]; if (textC == null) { continue; } float _scale = _charSettings[i].Scale; Color _foreColor = _charSettings[i].ForeColor; float cX = xOffset + textC.OffsetX * _scale; float cY = GetCursor() - textC.OffsetY * _scale; float cW = textC.Texture.Width * _scale; float cH = textC.Texture.Height * _scale; float cA = textC.Advance * _scale; if (cW <= 0 || cH <= 0) { // Skip } else { var origPixels = textC.Texture.uTexture.GetPixels32(); var newTexture = new UnityEngine.Texture2D(textC.Texture.Width, textC.Texture.Height); newTexture.name = "backedGlyphTexture"; newTexture.SetPixels32(origPixels); newTexture.Apply(); // Scale texture if needed. if ((int)cW != newTexture.width || (int)cH != newTexture.height) { TextureScaler.scale(newTexture, (int)cW, (int)cH); //TextureScale.Bilinear(newTexture, (int)cW, (int)cH); newTexture.Apply(); } var newImagePixels = newTexture.GetPixels32(); for (int p = 0; p < newImagePixels.Length; p++) { // BlendMode: Multiply var origColor = Color.FromUColor(newImagePixels[p]); var blendColorA = (float)(origColor.A * _foreColor.A) / 255; var blendColorR = (float)(origColor.R * _foreColor.R) / 255; var blendColorG = (float)(origColor.G * _foreColor.G) / 255; var blendColorB = (float)(origColor.B * _foreColor.B) / 255; var blendColor = Color.FromArgb((int)blendColorA, (int)blendColorR, (int)blendColorG, (int)blendColorB); newImagePixels[p] = blendColor.ToUColor(); } _bakedTexture.uTexture.SetPixels32((int)cX, (int)cY, newTexture.width, newTexture.height, newImagePixels); } xOffset += (int)cA; } _bakedTexture.Apply(); if (AutoSize) { Size = tSize; } }
public unsafe UnityEngine.Texture2D RenderIntoTexture(char c) { int code; FT.FT_FaceRec facerec = FT.HandleToRecord<FT.FT_FaceRec>(face_); IntPtr slot = facerec.glyph; code = FT.FT_Load_Char(face_, c, FT.FT_LOAD_RENDER/*|FT.FT_LOAD_TARGET_NORMAL*/); if (code != 0) { return null; } FT.FT_GlyphSlotRec slotrec = FT.HandleToRecord<FT.FT_GlyphSlotRec>(slot); FTSharp.FT.FT_Bitmap ftbm=slotrec.bitmap; UnityEngine.Texture2D texbuffer=new UnityEngine.Texture2D(ftbm.width,ftbm.rows,UnityEngine.TextureFormat.ARGB32,false); if (((int)ftbm.pixel_mode)!=2) { UnityEngine.Debug.LogError("Unsupported bitmap depth :"+((int)ftbm.pixel_mode).ToString()); return null; } // FT_PIXEL_MODE_MONO, //FT_PIXEL_MODE_GRAY, UnityEngine.Color32 [] clrs=texbuffer.GetPixels32(); int i=0; // UNSAFE DOES NOT WORK IN THE WEB PLAYER ! for (int y=0;y<ftbm.rows;y++) { int j=(((ftbm.rows-(1+y))*ftbm.pitch)); byte * bo=((byte *)ftbm.buffer); for (int x=0;x<ftbm.width;x++) { clrs[i].a=clrs[i].r=clrs[i].g=clrs[i].b=(bo[j+x]); i++; } } texbuffer.SetPixels32(clrs); texbuffer.Apply(); return texbuffer; }
public void Bake() { var tSize = TextureSize; _bakedTexture = new Bitmap(tSize.Width, tSize.Height); // Clear baked texture with transparent color. for (int y = 0; y < _bakedTexture.Height; y++) for (int x = 0; x < _bakedTexture.Width; x++) _bakedTexture.SetPixel(x, y, Color.Transparent); int xOffset = 0; for (int i = 0; i < Text.Length; i++) { if (_font.textureList.ContainsKey(Text[i]) == false) continue; var textC = _font.textureList[Text[i]]; if (textC == null) continue; float _scale = _charSettings[i].Scale; Color _foreColor = _charSettings[i].ForeColor; float cX = xOffset + textC.OffsetX * _scale; float cY = GetCursor() - textC.OffsetY * _scale; float cW = textC.Texture.Width * _scale; float cH = textC.Texture.Height * _scale; float cA = textC.Advance * _scale; if (cW <= 0 || cH <= 0) { // Skip } else { var origPixels = textC.Texture.uTexture.GetPixels32(); var newTexture = new UnityEngine.Texture2D(textC.Texture.Width, textC.Texture.Height); newTexture.name = "backedGlyphTexture"; newTexture.SetPixels32(origPixels); newTexture.Apply(); // Scale texture if needed. if ((int)cW != newTexture.width || (int)cH != newTexture.height) { TextureScaler.scale(newTexture, (int)cW, (int)cH); //TextureScale.Bilinear(newTexture, (int)cW, (int)cH); newTexture.Apply(); } var newImagePixels = newTexture.GetPixels32(); for (int p = 0; p < newImagePixels.Length; p++) { // BlendMode: Multiply var origColor = Color.FromUColor(newImagePixels[p]); var blendColorA = (float)(origColor.A * _foreColor.A) / 255; var blendColorR = (float)(origColor.R * _foreColor.R) / 255; var blendColorG = (float)(origColor.G * _foreColor.G) / 255; var blendColorB = (float)(origColor.B * _foreColor.B) / 255; var blendColor = Color.FromArgb((int)blendColorA, (int)blendColorR, (int)blendColorG, (int)blendColorB); newImagePixels[p] = blendColor.ToUColor(); } _bakedTexture.uTexture.SetPixels32((int)cX, (int)cY, newTexture.width, newTexture.height, newImagePixels); } xOffset += (int)cA; } _bakedTexture.Apply(); if (AutoSize) Size = tSize; }
private BitmapImplementation(UnityEngine.Color32[] data, int width, int height) { Bitmap = new UnityEngine.Texture2D(width, height); Bitmap.SetPixels32(data); }
static int _m_SetPixels32(RealStatePtr L) { try { ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L); UnityEngine.Texture2D gen_to_be_invoked = (UnityEngine.Texture2D)translator.FastGetCSObj(L, 1); int gen_param_count = LuaAPI.lua_gettop(L); if (gen_param_count == 2 && translator.Assignable <UnityEngine.Color32[]>(L, 2)) { UnityEngine.Color32[] _colors = (UnityEngine.Color32[])translator.GetObject(L, 2, typeof(UnityEngine.Color32[])); gen_to_be_invoked.SetPixels32(_colors); return(0); } if (gen_param_count == 3 && translator.Assignable <UnityEngine.Color32[]>(L, 2) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 3)) { UnityEngine.Color32[] _colors = (UnityEngine.Color32[])translator.GetObject(L, 2, typeof(UnityEngine.Color32[])); int _miplevel = LuaAPI.xlua_tointeger(L, 3); gen_to_be_invoked.SetPixels32(_colors, _miplevel); return(0); } if (gen_param_count == 6 && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 2) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 3) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 4) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 5) && translator.Assignable <UnityEngine.Color32[]>(L, 6)) { int _x = LuaAPI.xlua_tointeger(L, 2); int _y = LuaAPI.xlua_tointeger(L, 3); int _blockWidth = LuaAPI.xlua_tointeger(L, 4); int _blockHeight = LuaAPI.xlua_tointeger(L, 5); UnityEngine.Color32[] _colors = (UnityEngine.Color32[])translator.GetObject(L, 6, typeof(UnityEngine.Color32[])); gen_to_be_invoked.SetPixels32(_x, _y, _blockWidth, _blockHeight, _colors); return(0); } if (gen_param_count == 7 && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 2) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 3) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 4) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 5) && translator.Assignable <UnityEngine.Color32[]>(L, 6) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 7)) { int _x = LuaAPI.xlua_tointeger(L, 2); int _y = LuaAPI.xlua_tointeger(L, 3); int _blockWidth = LuaAPI.xlua_tointeger(L, 4); int _blockHeight = LuaAPI.xlua_tointeger(L, 5); UnityEngine.Color32[] _colors = (UnityEngine.Color32[])translator.GetObject(L, 6, typeof(UnityEngine.Color32[])); int _miplevel = LuaAPI.xlua_tointeger(L, 7); gen_to_be_invoked.SetPixels32(_x, _y, _blockWidth, _blockHeight, _colors, _miplevel); return(0); } } catch (System.Exception gen_e) { return(LuaAPI.luaL_error(L, "c# exception:" + gen_e)); } return(LuaAPI.luaL_error(L, "invalid arguments to UnityEngine.Texture2D.SetPixels32!")); }