/** テクスチャーからテクスチャー作成。 */ public static UnityEngine.Texture2D CreateTextureFromTexture(UnityEngine.Texture2D a_texture, int a_offset_x, int a_offset_y, int a_size_w, int a_size_h) { UnityEngine.Texture2D t_new_texture = null; try{ UnityEngine.Color[] t_color_list = a_texture.GetPixels(a_offset_x, a_texture.height - a_offset_y - a_size_h, a_size_w, a_size_h); t_new_texture = new UnityEngine.Texture2D(a_size_w, a_size_h, UnityEngine.TextureFormat.RGBA32, false); t_new_texture.SetPixels(t_color_list); t_new_texture.Apply(); }catch (System.Exception t_exception) { UnityEngine.Debug.LogError(t_exception.Message); } return(t_new_texture); }
internal static UnityEngine.Texture2D BlitTexture(UnityEngine.Texture2D texture, UnityEngine.TextureFormat format, bool alphaOnly = false) { // Create a temporary RenderTexture of the same size as the texture var tmp = UnityEngine.RenderTexture.GetTemporary( texture.width, texture.height, 0, UnityEngine.RenderTextureFormat.Default, UnityEngine.RenderTextureReadWrite.sRGB); // Blit the pixels on texture to the RenderTexture UnityEngine.Graphics.Blit(texture, tmp); // Backup the currently set RenderTexture var previous = UnityEngine.RenderTexture.active; // Set the current RenderTexture to the temporary one we created UnityEngine.RenderTexture.active = tmp; // Create a new readable Texture2D to copy the pixels to it var result = new UnityEngine.Texture2D(texture.width, texture.height, format, false); // Copy the pixels from the RenderTexture to the new Texture result.ReadPixels(new UnityEngine.Rect(0, 0, tmp.width, tmp.height), 0, 0); result.Apply(); // Broadcast alpha to color if (alphaOnly || !HasColor(texture)) { var pixels = result.GetPixels(); for (var i = 0; i < pixels.Length; i++) { pixels[i].r = pixels[i].a; pixels[i].g = pixels[i].a; pixels[i].b = pixels[i].a; } result.SetPixels(pixels); result.Apply(); } // Reset the active RenderTexture UnityEngine.RenderTexture.active = previous; // Release the temporary RenderTexture UnityEngine.RenderTexture.ReleaseTemporary(tmp); return(result); }
//get rows or columns with no buildings public static List <CityGenerator.MapRow> GetEmptyRows(UnityEngine.Texture2D heightmap) { List <UnityEngine.Color> heightmapColor = new List <UnityEngine.Color>(heightmap.GetPixels()); System.Func <UnityEngine.Color, float> colorToFloat = c => c.r; List <float> heightmapGray = CityGenerator.Lodash.Map(heightmapColor, colorToFloat); /* - begin ported code - */ int i; List <CityGenerator.MapRow> empty = new List <CityGenerator.MapRow>(); //loop through rows for (i = 0; i < heightmap.height; i++) { // list of float grayscale vals for the current row var row = GetHeightmapRow(heightmapGray, heightmap.width, heightmap.height, i); //all values in row are over tree threshold row = CityGenerator.Lodash.Reject(row, BlockHasTrees); if (row.Count == 0) { CityGenerator.MapRow emptyRow; emptyRow.axis = 0; emptyRow.index = i; empty.Add(emptyRow); } } //loop through columns for (i = 0; i < heightmap.width; i++) { var col = GetHeightmapColumn(heightmapGray, heightmap.width, heightmap.height, i); col = CityGenerator.Lodash.Reject(col, BlockHasTrees); if (col.Count == 0) { CityGenerator.MapRow emptyCol; emptyCol.axis = 1; emptyCol.index = i; empty.Add(emptyCol); } } return(empty); }
static int _m_GetPixels(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.Color[] gen_ret = gen_to_be_invoked.GetPixels( ); 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.Color[] gen_ret = gen_to_be_invoked.GetPixels(_miplevel); translator.Push(L, gen_ret); return(1); } if (gen_param_count == 5 && 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)) { 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.Color[] gen_ret = gen_to_be_invoked.GetPixels(_x, _y, _blockWidth, _blockHeight); translator.Push(L, gen_ret); return(1); } 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) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(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); int _miplevel = LuaAPI.xlua_tointeger(L, 6); UnityEngine.Color[] gen_ret = gen_to_be_invoked.GetPixels(_x, _y, _blockWidth, _blockHeight, _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.GetPixels!")); }
private static void ThreadedScale(UnityEngine.Texture2D tex, int newWidth, int newHeight, bool useBilinear) { texColors = tex.GetPixels(); newColors = new UnityEngine.Color[newWidth * newHeight]; if (useBilinear) { ratioX = 1.0f / ((float)newWidth / (tex.width - 1)); ratioY = 1.0f / ((float)newHeight / (tex.height - 1)); } else { ratioX = ((float)tex.width) / newWidth; ratioY = ((float)tex.height) / newHeight; } w = tex.width; w2 = newWidth; var cores = UnityEngine.Mathf.Min(UnityEngine.SystemInfo.processorCount, newHeight); var slice = newHeight / cores; finishCount = 0; if (mutex == null) { mutex = new System.Threading.Mutex(false); } if (cores > 1) { int i = 0; ThreadData threadData; for (i = 0; i < cores - 1; i++) { threadData = new ThreadData(slice * i, slice * (i + 1)); System.Threading.ParameterizedThreadStart ts = useBilinear ? new System.Threading.ParameterizedThreadStart(BilinearScale) : new System.Threading.ParameterizedThreadStart(PointScale); System.Threading.Thread thread = new System.Threading.Thread(ts); thread.Start(threadData); } threadData = new ThreadData(slice * i, newHeight); if (useBilinear) { BilinearScale(threadData); } else { PointScale(threadData); } while (finishCount < cores) { System.Threading.Thread.Sleep(1); } } else { ThreadData threadData = new ThreadData(0, newHeight); if (useBilinear) { BilinearScale(threadData); } else { PointScale(threadData); } } tex.Resize(newWidth, newHeight); tex.SetPixels(newColors); tex.Apply(); texColors = null; newColors = null; }