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)); }
private void SaveRenderTextureToPNG( UnityEngine.RenderTexture renderTexture, UnityEngine.TextureFormat textureFormat, RenderCheck renderCheck) { string fileName = string.Empty; if (renderCheck.Match != null) { fileName = UnityEditor.AssetDatabase.GetAssetPath(renderCheck.Match); } else { fileName = UnityEditor.AssetDatabase.GenerateUniqueAssetPath(UnityEditor.AssetDatabase.GetAssetPath(renderCheck)); } fileName = System.IO.Path.GetDirectoryName(fileName) + "/" + System.IO.Path.GetFileNameWithoutExtension(fileName) + ".png"; if (renderCheck.Match != null && (renderCheck.Match.width != renderTexture.width || renderCheck.Match.height != renderTexture.height)) { UnityEngine.GameObject.Destroy(renderCheck.Match); } if (renderCheck.Match == null) { renderCheck.Match = new UnityEngine.Texture2D(renderTexture.width, renderTexture.height, textureFormat, false, true); } UnityEngine.RenderTexture previousRT = UnityEngine.RenderTexture.active; UnityEngine.RenderTexture.active = renderTexture; UnityEngine.Texture2D linearTexture = new UnityEngine.Texture2D(renderTexture.width, renderTexture.height, UnityEngine.TextureFormat.RGBAFloat, false); linearTexture.ReadPixels(new UnityEngine.Rect(0.0f, 0.0f, (float)renderTexture.width, (float)renderTexture.height), 0, 0, false); //linearTexture.Apply(); renderCheck.Match.SetPixels32(linearTexture.GetPixels32()); UnityEngine.GameObject.DestroyImmediate(linearTexture); linearTexture = null; UnityEngine.RenderTexture.active = previousRT; renderCheck.Match.Apply(false, false); byte[] pngFile = UnityEngine.ImageConversion.EncodeToPNG(renderCheck.Match); try { using (System.IO.Stream fileStream = new System.IO.FileStream(fileName, System.IO.FileMode.Create)) { System.IO.BinaryWriter streamWriter = new System.IO.BinaryWriter(fileStream); streamWriter.Write(pngFile); this.needRefresh = true; this.needReload.Add(new System.Collections.Generic.KeyValuePair <RenderCheck, string>(renderCheck, fileName)); } } catch (System.Exception exception) { UnityEngine.Debug.LogError(string.Format("Saving texture at path {0} failed with exception {1}", fileName, exception.Message)); } }
public void Serialize(OverloadLevelConvertSerializer serializer) { serializer.StartCmd(CommandId); int width = 0; int height = 0; string formatStr = UnityEngine.TextureFormat.RGB24.ToString(); string filterModeStr = UnityEngine.FilterMode.Bilinear.ToString(); string name = string.Empty; bool mipmap = false; if (serializer.IsWriting) { width = texture.width; height = texture.height; formatStr = texture.format.ToString(); filterModeStr = texture.filterMode.ToString(); name = texture.name; #if OVERLOAD_LEVEL_EDITOR mipmap = texture.mipmap; #endif } serializer.SerializeX(this, x => x.texture2DGuid); serializer.Serialize(ref width); serializer.Serialize(ref height); serializer.Serialize(ref formatStr); serializer.Serialize(ref mipmap); serializer.Serialize(ref filterModeStr); serializer.Serialize(ref name); UnityEngine.Color32[] pixelData = null; if (serializer.IsWriting) { serializer.SerializeOut_array(typeof(UnityEngine.Color32), texture.GetPixels32()); } else { pixelData = (UnityEngine.Color32[])serializer.SerializeIn_array(typeof(UnityEngine.Color32)); } if (!serializer.IsWriting) { UnityEngine.TextureFormat format = (UnityEngine.TextureFormat)Enum.Parse(typeof(UnityEngine.TextureFormat), formatStr); this.texture = new UnityEngine.Texture2D(width, height, format, mipmap); UnityEngine.FilterMode filterMode = (UnityEngine.FilterMode)Enum.Parse(typeof(UnityEngine.FilterMode), filterModeStr); this.texture.filterMode = filterMode; this.texture.name = name; this.texture.SetPixels32(pixelData); this.texture.Apply(); } serializer.FinishCmd(CommandId); }
static int _m_GetPixels32(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 == 1) { UnityEngine.Color32[] gen_ret = gen_to_be_invoked.GetPixels32( ); translator.Push(L, gen_ret); return(1); } if (gen_param_count == 2 && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 2)) { int _miplevel = LuaAPI.xlua_tointeger(L, 2); UnityEngine.Color32[] gen_ret = gen_to_be_invoked.GetPixels32(_miplevel); translator.Push(L, gen_ret); return(1); } } catch (System.Exception gen_e) { return(LuaAPI.luaL_error(L, "c# exception:" + gen_e)); } return(LuaAPI.luaL_error(L, "invalid arguments to UnityEngine.Texture2D.GetPixels32!")); }
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); }
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; }
public HeightMapData(string height_map_path) { #if !OVERLOAD_LEVEL_EDITOR UnityEngine.Texture2D unity_heightmap_tex = UnityEditor.AssetDatabase.LoadAssetAtPath(height_map_path, typeof(UnityEngine.Texture2D)) as UnityEngine.Texture2D; if (unity_heightmap_tex != null) { try { UnityEngine.Color32[] raw_heightmap_data = unity_heightmap_tex.GetPixels32(); m_width = Convert.ToUInt32(unity_heightmap_tex.width); m_height = Convert.ToUInt32(unity_heightmap_tex.height); m_height_data = new float[m_width, m_height]; // Unity textures are set up so that the rows are laid out bottom to top in memory. We want to rearrange this // so that m_height_data[0,0] is the TOP-LEFT corner of the map, and the coordinates are in the order [x, y] ([column, row]) for (uint row_index = 0; row_index < m_height; ++row_index) { uint unity_row_index = (m_height - 1) - row_index; for (uint col_index = 0; col_index < m_width; ++col_index) { uint pixel_index = (unity_row_index * m_width) + col_index; var pixel_color32 = raw_heightmap_data[pixel_index]; // Assuming that the heightmap textures are grayscale, so it doesn't matter which color channel we use. // Bias the height value so that a pixel value of 128 is 0 deformation, 255 is full forward deformation, // and both 0 and 1 equal full backward deformation. (The clamping at 0/1 is because the halfway point // of 0-255 is 127.5, which falls between two integers. Moving the center point over to exactly 128.0 // makes the positive and negative ranges unequal in size. int biased_height_value = System.Math.Max(Convert.ToInt32(pixel_color32.r) - 128, -127); m_height_data[col_index, row_index] = Convert.ToSingle(biased_height_value) / 127.0f; } } } catch (System.Exception e) { UnityEngine.Debug.LogError(e.Message); // Just create an empty heightmap. m_width = m_height = 1; m_height_data = new float[1, 1]; m_height_data[0, 0] = 0.0f; } // There doesn't seem to be a way to unload the asset from the AssetDatabase once we're done with it. Googling shows a bunch // of other people who are confused by this, but no good answer except to use AssetBundles or the Resources interface, which // would require us to put all heightmaps in the Assets/Resources folder. These would then get bundled into standalone builds, // which is entirely unnecessary. So...I guess we just keep all heightmaps loaded until you close out the Unity Editor session. // I doubt this will be a memory or performance issue, but it's still annoying. } else { // Just create an empty heightmap. m_width = m_height = 1; m_height_data = new float[1, 1]; m_height_data[0, 0] = 0.0f; } #elif UNITY_STANDALONE // In standalone builds we can only load certain resources which have been packaged into the build. // However, we shouldn't ever need to use this code in a standalone build (it's editor-only level // conversion functionality), so just make sure it compiles - it doesn't need to work. m_width = m_height = 1; m_height_data = new float[1, 1]; m_height_data[0, 0] = 0.0f; #else try { System.Drawing.Bitmap heightmap_tex = new System.Drawing.Bitmap(height_map_path); m_width = System.Convert.ToUInt32(heightmap_tex.Width); m_height = System.Convert.ToUInt32(heightmap_tex.Height); m_height_data = new float[m_width, m_height]; for (int row_index = 0; row_index < heightmap_tex.Height; ++row_index) { for (int col_index = 0; col_index < heightmap_tex.Width; ++col_index) { var pixel_color32 = heightmap_tex.GetPixel(col_index, row_index); // Assuming that the heightmap textures are grayscale, so it doesn't matter which color channel we use. // Bias the height value so that a pixel value of 128 is 0 deformation, 255 is full forward deformation, // and both 0 and 1 equal full backward deformation. (The clamping at 0/1 is because the halfway point // of 0-255 is 127.5, which falls between two integers. Moving the center point over to exactly 128.0 // makes the positive and negative ranges unequal in size. int biased_height_value = System.Math.Max(Convert.ToInt32(pixel_color32.R) - 128, -127); m_height_data[col_index, row_index] = Convert.ToSingle(biased_height_value) / 127.0f; } } } catch (System.IO.FileNotFoundException exc) { System.Windows.Forms.MessageBox.Show("Failed to find height map: " + exc.FileName); m_width = m_height = 1; m_height_data = new float[1, 1]; m_height_data[0, 0] = 0.0f; } #endif }