public IBitmapImplementation Crop(IntRectangle cropArea) { var pixels = Bitmap.GetPixels(cropArea.A.X, cropArea.A.Y, cropArea.Width, cropArea.Height); var res = new UnityEngine.Texture2D(cropArea.Width, cropArea.Height); res.SetPixels(pixels); return(new BitmapImplementation(res)); }
public static UnityEngine.Texture2D MakeTexture(int width, int height, UnityEngine.Color col) { UnityEngine.Color[] pix = new UnityEngine.Color[width * height]; for (int i = 0; i < pix.Length; ++i) { pix[i] = col; } UnityEngine.Texture2D result = new UnityEngine.Texture2D(width, height); result.SetPixels(pix); result.Apply(); return(result); }
public UnityEngine.Texture2D GetTexture() { if (texture == null) { //var pixels = new UnityEngine.Color[lightmapColors.Length]; //for (int i = 0; i < pixels.Length; i++) // pixels[i] = lightmapColors[i].GetColor(); texture = new UnityEngine.Texture2D(width, height, UnityEngine.TextureFormat.RGBA32, false); texture.SetPixels(lightmapColors); texture.Apply(); } return(texture); }
/** テクスチャーからテクスチャー作成。 */ 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); }
public override void SetPixels(Color[] pixels) { var unityPixels = new UnityEngine.Color[pixels.Length]; Color pixel; for (int i = 0; i < pixels.Length; ++i) { pixel = pixels[i]; unityPixels[i].r = pixel.r; unityPixels[i].g = pixel.g; unityPixels[i].b = pixel.b; unityPixels[i].a = pixel.a; } unityTexture.SetPixels(unityPixels); }
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); }
protected override void OnUpdate() { Entities.WithAll <ChunkMap>().ForEach((Entity e, ref ChunkMap chunkMap) => { if (chunkMap.dirty == 2) { // create texture UnityEngine.Texture2D mapTexture = new UnityEngine.Texture2D( chunkMap.width, chunkMap.height, UnityEngine.Experimental.Rendering.DefaultFormat.LDR, //UnityEngine.Experimental.Rendering.DefaultFormat.HDR, UnityEngine.Experimental.Rendering.TextureCreationFlags.None); if (mapTexture == null) { World.EntityManager.RemoveComponent <ChunkMap>(e); //UnityEngine.Debug.LogError("Map Texture [" + chunkMap.chunkPosition + "] created for chunk was null."); return; } mapTexture.filterMode = UnityEngine.FilterMode.Point; UnityEngine.Color[] pixels = new UnityEngine.Color[chunkMap.width * chunkMap.height]; mapTexture.name = "ChunkMap_" + chunkMap.chunkPosition.x + "_" + chunkMap.chunkPosition.y + "_" + chunkMap.chunkPosition.z; int xzIndex = 0; for (int i = 0; i < chunkMap.width; i++) { for (int j = 0; j < chunkMap.height; j++) { //int xzIndex = i + j * chunkMap.height; byte voxel = chunkMap.topVoxels[xzIndex]; if (voxel == 0) { pixels[xzIndex] = UnityEngine.Color.black; } else if (voxel == 1) { pixels[xzIndex] = UnityEngine.Color.green; } else if (voxel == 2) { pixels[xzIndex] = UnityEngine.Color.red; } else if (voxel == 3) { pixels[xzIndex] = UnityEngine.Color.blue; } else if (voxel == 4) { pixels[xzIndex] = UnityEngine.Color.yellow; } else { pixels[xzIndex] = UnityEngine.Color.magenta; } int height = (int)chunkMap.heights[xzIndex]; //float colorBrightness = 3 * ((float)height / (float)chunkMap.highestHeight); pixels[xzIndex] *= ((float)height / 64); //math.min(1, colorBrightness); xzIndex++; } } mapTexture.SetPixels(pixels); mapTexture.Apply(); //Bootstrap.instance.debugMaps.Add(mapTexture); float2 mapPosition = new float2(chunkMap.chunkPosition.x, chunkMap.chunkPosition.z); if (maps.ContainsKey(mapPosition)) { if (maps[mapPosition] != null) { UnityEngine.GameObject.Destroy(maps[mapPosition]); } maps.Remove(mapPosition); } maps.Add(mapPosition, mapTexture); World.EntityManager.RemoveComponent <ChunkMap>(e); } }); }
static int _m_SetPixels(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.Color[]>(L, 2)) { UnityEngine.Color[] _colors = (UnityEngine.Color[])translator.GetObject(L, 2, typeof(UnityEngine.Color[])); gen_to_be_invoked.SetPixels(_colors); return(0); } if (gen_param_count == 3 && translator.Assignable <UnityEngine.Color[]>(L, 2) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 3)) { UnityEngine.Color[] _colors = (UnityEngine.Color[])translator.GetObject(L, 2, typeof(UnityEngine.Color[])); int _miplevel = LuaAPI.xlua_tointeger(L, 3); gen_to_be_invoked.SetPixels(_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.Color[]>(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.Color[] _colors = (UnityEngine.Color[])translator.GetObject(L, 6, typeof(UnityEngine.Color[])); gen_to_be_invoked.SetPixels(_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.Color[]>(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.Color[] _colors = (UnityEngine.Color[])translator.GetObject(L, 6, typeof(UnityEngine.Color[])); int _miplevel = LuaAPI.xlua_tointeger(L, 7); gen_to_be_invoked.SetPixels(_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.SetPixels!")); }
protected internal override Texture2D Read(ContentReader reader, Texture2D existingInstance) { Texture2D texture = null; SurfaceFormat surfaceFormat; if (reader.version < 5) { SurfaceFormatLegacy legacyFormat = (SurfaceFormatLegacy)reader.ReadInt32(); switch (legacyFormat) { case SurfaceFormatLegacy.Dxt1: surfaceFormat = SurfaceFormat.Dxt1; break; case SurfaceFormatLegacy.Dxt3: surfaceFormat = SurfaceFormat.Dxt3; break; case SurfaceFormatLegacy.Dxt5: surfaceFormat = SurfaceFormat.Dxt5; break; case SurfaceFormatLegacy.Color: surfaceFormat = SurfaceFormat.Color; break; default: throw new NotSupportedException("Unsupported legacy surface format."); } } else { surfaceFormat = (SurfaceFormat)reader.ReadInt32(); } int width = (reader.ReadInt32()); int height = (reader.ReadInt32()); int levelCount = (reader.ReadInt32()); int levelCountOutput = levelCount; SurfaceFormat convertedFormat = surfaceFormat; switch (surfaceFormat) { case SurfaceFormat.Dxt1: case SurfaceFormat.Dxt1a: convertedFormat = SurfaceFormat.Color; break; case SurfaceFormat.Dxt3: case SurfaceFormat.Dxt5: convertedFormat = SurfaceFormat.Color; break; case SurfaceFormat.NormalizedByte4: convertedFormat = SurfaceFormat.Color; break; } UnityEngine.Texture2D unityTexture = new UnityEngine.Texture2D(width, height, XnaToUnity.TextureFormat(convertedFormat), levelCountOutput > 1); for (int level = 0; level < levelCount; level++) { int levelDataSizeInBytes = (reader.ReadInt32()); byte[] levelData = reader.ReadBytes(levelDataSizeInBytes); int levelWidth = width >> level; int levelHeight = height >> level; if (level >= levelCountOutput) { continue; } //Convert the image data if required switch (surfaceFormat) { case SurfaceFormat.Dxt1: case SurfaceFormat.Dxt1a: levelData = DxtUtil.DecompressDxt1(levelData, levelWidth, levelHeight); break; case SurfaceFormat.Dxt3: levelData = DxtUtil.DecompressDxt3(levelData, levelWidth, levelHeight); break; case SurfaceFormat.Dxt5: levelData = DxtUtil.DecompressDxt5(levelData, levelWidth, levelHeight); break; } // un-premultiply alpha (instead do it in the shader) for (int i = 0; i < levelData.Length; i += 4) { float r = levelData[i + 0] / 255.0f; float g = levelData[i + 1] / 255.0f; float b = levelData[i + 2] / 255.0f; float a = levelData[i + 3] / 255.0f; levelData[i + 0] = (byte)(r / a * 255.0f); levelData[i + 1] = (byte)(g / a * 255.0f); levelData[i + 2] = (byte)(b / a * 255.0f); //levelData[i + 0] = 0; //levelData[i + 1] = 255; //levelData[i + 2] = 0; //levelData[i + 3] = 255; } // swap rows because unity textures are laid out bottom-top instead of top-bottom int rowSize = width * 4; byte[] temp = new byte[rowSize]; for (int i = 0; i < levelData.Length / 2; i += rowSize) { for (int j = 0; j < rowSize; j++) { temp[j] = levelData[i + j]; } int p = levelData.Length - (i + rowSize); for (int j = 0; j < rowSize; j++) { levelData[i + j] = levelData[p + j]; } for (int j = 0; j < rowSize; j++) { levelData[p + j] = temp[j]; } } UnityEngine.Color[] unityColors = new UnityEngine.Color[levelData.Length * 4]; unityTexture.SetPixels(XnaToUnity.Color(levelData, ref unityColors), level); unityTexture.Apply(); texture = new Texture2D(unityTexture); } return(texture); }
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; }