コード例 #1
0
        public static Texture2D GetTexture(DitherMode mode)
        {
            if (_textures.ContainsKey(mode))
            {
                return(_textures[mode]);
            }

            var tex = new Texture2D(8, 8, TextureFormat.RGBA32, false);

            tex.filterMode = FilterMode.Point;

            var pixels = new Color32[tex.width * tex.height];

            for (int i = 0; i < pixels.Length; i++)
            {
                int x = i % tex.width;
                int y = i / tex.height;

                float ditherVal = GetLimit(mode, x, y);
                byte  b         = (byte)(255 * ditherVal);
                pixels[i] = new Color32(b, b, b, 255);
            }

            tex.SetPixels32(pixels);
            tex.Apply();
            _textures[mode] = tex;

            return(tex);
        }
コード例 #2
0
        public void ConvertImage(MagickImage image)
        {
            double scale = ImageScaleSlider.Value;

            PixelInterpolateMethod method = (PixelInterpolateMethod)InterpolationSelection.SelectedItem;
            DitherMode             dither = (DitherMode)DitherModeSelection.SelectedItem;
            BitMode mode = (BitMode)BitModeSelection.SelectedItem;

            currentWindow?.ConvertImage(image, scale, method, dither, mode);
        }
コード例 #3
0
        private static float GetLimit(DitherMode mode, int x, int y)
        {
            switch (mode)
            {
            case DitherMode.Bayer2x2: return(Bayer2x2[((y & 1) << 1) + (x & 1)]);

            case DitherMode.Bayer4x4: return(Bayer4x4[((y & 3) << 2) + (x & 3)]);

            case DitherMode.Bayer8x8: return(Bayer8x8[((y & 7) << 3) + (x & 7)]);

            case DitherMode.Cluster4x4: return(Bayer4x4[((y & 3) << 2) + (x & 3)]);

            case DitherMode.Cluster8x8: return(Bayer8x8[((y & 7) << 3) + (x & 7)]);

            default: return(1);
            }
        }
コード例 #4
0
        public static int GetDitherSize(DitherMode mode)
        {
            switch (mode)
            {
            case DitherMode.Bayer2x2: return(2);

            case DitherMode.Bayer4x4: return(4);

            case DitherMode.Bayer8x8: return(8);

            case DitherMode.Cluster4x4: return(4);

            case DitherMode.Cluster8x8: return(8);

            default: return(1);
            }
        }
コード例 #5
0
 public void Initialization()
 {
     mCachedDensity = -1;
     mCachedRadias  = -1;
     Dither         = DitherMode.Off;
     InstanceMaterial.enableInstancing = true;
     if (Indirect)
     {
         InstanceMaterial.shader = UnityUtils.FindShader("SAO_TJia_V3/Grass/TjiaInstancedGrassShader");
         mArgsBuffer             = new ComputeBuffer(1, args.Length * sizeof(uint), ComputeBufferType.IndirectArguments);
         UpdateBuffers();
     }
     else
     {
         InstanceMaterial.shader = UnityUtils.FindShader("SAO_TJia_V3/Grass/TjiaDirectInstancedGrassShader");
         mTransMatrixList        = new List <Matrix4x4[]>();
         UpdateBuffersDirect();
     }
 }
コード例 #6
0
ファイル: ColorTable.cs プロジェクト: sku11knight/725anhour
        public Color32[] ApplyToTexture(Texture2D target, DitherMode mode)
        {
            var src_pixels = target.GetPixels32();
            var out_pixels = new Color32[src_pixels.Length];

            for (int y = 0; y < target.height; y++)
            {
                for (int x = 0; x < target.width; x++)
                {
                    int     ofs = x + y * target.width;
                    Color32 c   = src_pixels[ofs];
                    if (c.a <= 0)
                    {
                        out_pixels[ofs] = Color.clear;
                        continue;
                    }


                    byte alpha = c.a;
                    int  First, Second;
                    GetNearestDitherIndices(c, out First, out Second);

                    Color32 A = colors[First];
                    Color32 B = colors[Second];

                    float Val       = ColorFindInterpolation(c, A, B);
                    var   ditherVal = DitherUtils.ColorDither(mode, x, y, Val);

                    c = ditherVal  ? B : A;

                    Val       = (float)alpha / 255.0f;
                    ditherVal = DitherUtils.ColorDither(mode, x, y, Val);
                    alpha     = (byte)(ditherVal ? 255 : 0);

                    //Current := ColorGrey(255 * DitherVal);
                    c = new Color32(c.b, c.g, c.b, alpha);
                    out_pixels[ofs] = c;
                }
            }
            return(out_pixels);
        }
コード例 #7
0
ファイル: ColorTable.cs プロジェクト: sku11knight/725anhour
        public int[] ApplyToTextureAsIndices(Texture2D target, DitherMode mode)
        {
            var src_pixels  = target.GetPixels32();
            var out_indices = new int[src_pixels.Length];

            for (int y = 0; y < target.height; y++)
            {
                for (int x = 0; x < target.width; x++)
                {
                    int     ofs = x + y * target.width;
                    Color32 c   = src_pixels[ofs];
                    if (c.a <= 0)
                    {
                        out_indices[ofs] = -1;
                        continue;
                    }


                    byte alpha = c.a;
                    int  First, Second;
                    GetNearestDitherIndices(c, out First, out Second);

                    Color32 A = colors[First];
                    Color32 B = colors[Second];

                    if (A.Equals(B))
                    {
                        out_indices[ofs] = First;
                    }
                    else
                    {
                        float Val       = ColorFindInterpolation(c, A, B);
                        var   ditherVal = DitherUtils.ColorDither(mode, x, y, Val);

                        out_indices[ofs] = (ditherVal ? Second : First);
                    }
                }
            }
            return(out_indices);
        }
コード例 #8
0
        public virtual void ConvertImage(MagickImage image, double scale, PixelInterpolateMethod pixelInterpolate, DitherMode dither, BitMode bitMode)
        {
            image.InterpolativeResize(Math.Max(1, (int)(image.Width * scale)), Math.Max(1, (int)(image.Height * scale)), pixelInterpolate);

            if (dither == DitherMode.Riemersma || dither == DitherMode.FloydSteinberg || dither == DitherMode.None)
            {
                image.Quantize(new QuantizeSettings()
                {
                    DitherMethod = (DitherMethod)dither,
                    Colors       = 256,
                    ColorSpace   = ColorSpace.sRGB,
                    TreeDepth    = 100
                });
            }
            else
            {
                image.OrderedDither(dither.ToString());
            }
        }
コード例 #9
0
        public static int ColorDitherMultiplier(DitherMode mode, int x, int y, float value)
        {
            float limit = GetLimit(mode, x, y);

            return(value >= limit ? 1 : 0);
        }
コード例 #10
0
        public static bool ColorDither(DitherMode mode, int x, int y, float value)
        {
            float limit = GetLimit(mode, x, y);

            return(value >= limit);
        }
コード例 #11
0
    void OnGUI()
    {
        GUILayout.Label("Convert a texture to 8bits", EditorStyles.boldLabel);
        sourceTexture = (Texture2D)EditorGUILayout.ObjectField("Source Texture", sourceTexture, typeof(Texture2D), false);
        palTexture    = (Texture2D)EditorGUILayout.ObjectField("Palette Texture", palTexture, typeof(Texture2D), false);
        path          = EditorGUILayout.TextField("Destination Path", path);
        ditherMode    = (DitherMode)EditorGUILayout.EnumPopup("Dither Mode", ditherMode);


        if (GUILayout.Button("Convert"))
        {
            var palette = new ColorTable();
            palette.LoadFromTexture(palTexture);
            var indices = palette.ApplyToTextureAsIndices(sourceTexture, ditherMode);
            var tex     = new Texture2D(sourceTexture.width, sourceTexture.height, TextureFormat.ARGB32, false);
            tex.filterMode = FilterMode.Point;

            var colors = new Color32[indices.Length];
            for (int i = 0; i < colors.Length; i++)
            {
                var index = indices[i];
                if (index < 0)
                {
                    colors[i] = Color.clear;
                }
                else
                {
                    float n = index / (float)(palTexture.width - 1);
                    byte  k = (byte)(n * 255);
                    colors[i] = new Color32(k, k, k, 255);
                }
            }
            tex.SetPixels32(colors);
            tex.Apply();

            string targetPath = string.IsNullOrEmpty(path) ? Application.dataPath : Path.Combine(Application.dataPath, path);
            Directory.CreateDirectory(targetPath);

            //var targetPath = "Assets";

            string fileName = sourceTexture.name;

            string tag = "_" + palTexture.name;
            if (!fileName.Contains(tag))
            {
                fileName += tag;
            }
            fileName += ".png";

            targetPath = Path.Combine(targetPath, fileName);

            Debug.Log("Trying export 8bit texture to " + targetPath);

            /*AssetDatabase.CreateAsset(tex, targetPath);
             * var sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
             * AssetDatabase.CreateAsset(sprite, targetPath.Replace("_8bit", "_spr"));*/


            File.WriteAllBytes(targetPath, tex.EncodeToPNG());

            AssetDatabase.Refresh();

            /*Texture2D temp = (Texture2D)AssetDatabase.LoadAssetAtPath(targetPath, typeof(Texture2D));
             * temp.filterMode = FilterMode.Point;
             *
             * AssetDatabase.SaveAssets();*/
        }
    }
コード例 #12
0
        public override void ConvertImage(MagickImage image, double scale, PixelInterpolateMethod pixelInterpolate, DitherMode dither, BitMode bitMode)
        {
            Reset();

            Mode = bitMode;

            int[] size = Utils.LCDSizeToNum((LCDSize)Converter.Instance.LCDSizeSelection.SelectedItem);

            image.InterpolativeResize(size[1] * Y, size[0] * X, pixelInterpolate);

            image.Depth = bitMode == BitMode.Bit3 ? 3 : 5;
            image.Settings.Compression = CompressionMethod.NoCompression;

            if (dither == DitherMode.Riemersma || dither == DitherMode.FloydSteinberg || dither == DitherMode.None)
            {
                image.Quantize(new QuantizeSettings()
                {
                    Colors = bitMode == BitMode.Bit3 ? 8 : 32, DitherMethod = (DitherMethod)dither, ColorSpace = ColorSpace.sRGB, TreeDepth = 100
                });
            }
            else
            {
                image.Quantize(new QuantizeSettings()
                {
                    Colors = bitMode == BitMode.Bit3 ? 8 : 32, DitherMethod = DitherMethod.No, ColorSpace = ColorSpace.sRGB, TreeDepth = 100
                });
                image.OrderedDither(dither.ToString());
            }
        }