void SaveGradient(string path) { Color[] colors = new Color[resolution.ProductAbsoluted]; Texture2D texture = new Texture2D(resolution.x, resolution.y); foreach (Int2 pixel in resolution.Loop()) { int index = pixel.y * resolution.x + pixel.x; colors[index] = gradient.Evaluate(pixel.x / (resolution.x - 1f)); } texture.SetPixels(colors); texture.Apply(); File.WriteAllBytes(path, texture.EncodeToPNG()); DestroyImmediate(texture); }
protected static Sprite CreateSprite(Color[,] colors) { Int2 size = new Int2(colors.GetLength(0), colors.GetLength(1)); Texture2D texture = new Texture2D(size.x, size.y, TextureFormat.RGBA32, true) { filterMode = FilterMode.Point, wrapMode = TextureWrapMode.Clamp }; Color[] pixels = new Color[colors.Length]; foreach (Int2 position in size.Loop()) { pixels[position.y * size.x + position.x] = colors[position.x, position.y]; } texture.SetPixels(pixels); texture.Apply(); return(Sprite.Create(texture, new Rect(Float2.zero, size), Float2.half, size.MinComponent)); }
void SaveCubemap(string path) { Texture2D first = FirstTexture; Int2 singleSize = new Int2(first.width, first.height); Int2 resolution = GetCubemapSize(singleSize); TextureFormat format; switch (extension) { case TextureType.pngHalf: { format = TextureFormat.RGBA32; break; } case TextureType.pngFull: { format = TextureFormat.RGBA64; break; } case TextureType.exrHalf: { format = TextureFormat.RGBAHalf; break; } case TextureType.exrFull: format = TextureFormat.RGBAFloat; { break; } default: throw ExceptionHelper.Invalid(nameof(extension), extension, InvalidType.unexpected); } Color[] colors = new Color[resolution.ProductAbsoluted]; Color[][] sourceColors = new Color[textures.Length][]; for (int i = 0; i < textures.Length; i++) { if (textures[i] == null) { continue; } sourceColors[i] = textures[i].GetPixels(); } Parallel.ForEach ( resolution.Loop(), pixel => { Direction direction = GetPixelTextureDirection(pixel, singleSize); Color color; if (!direction.IsZero) { Color[] source = sourceColors[direction.Index]; if (source == null) { color = Color.black; //Black for missing texture } else { Int2 region = GetPixelTextureOffset(pixel, singleSize); color = source[region.y * singleSize.x + region.x]; } } else { color = Color.clear; } colors[pixel.y * resolution.x + pixel.x] = color; } ); Release(ref sourceColors); Texture2D result = new Texture2D(resolution.x, resolution.y, format, false); result.SetPixels(colors); result.Apply(); Release(ref colors); byte[] bytes; switch (extension) { case TextureType.pngHalf: case TextureType.pngFull: { bytes = result.EncodeToPNG(); break; } case TextureType.exrHalf: { bytes = result.EncodeToEXR(Texture2D.EXRFlags.CompressZIP); break; } case TextureType.exrFull: { bytes = result.EncodeToEXR(Texture2D.EXRFlags.OutputAsFloat); break; } default: throw ExceptionHelper.Invalid(nameof(extension), extension, InvalidType.unexpected); } DestroyImmediate(result); Release(ref result); File.WriteAllBytes(path, bytes); }