示例#1
0
    void LoadTexDistanceField(string name)
    {
#if UNITY_STANDALONE
        string filename = System.IO.Path.Combine(Application.streamingAssetsPath, name + ".png");
        string filename_df = System.IO.Path.Combine(Application.streamingAssetsPath, name + "_df.png");
        //if (System.IO.File.Exists(filename_df))
        //{
        //    Texture2D tex = new Texture2D(1, 1);
        //    tex.LoadImage(System.IO.File.ReadAllBytes(filename_df));
        //    texs[name] = tex;
        //}
        //else
        {
            Texture2D tex = new Texture2D(1, 1);
            tex.LoadImage(System.IO.File.ReadAllBytes(filename));//加载原始图片
            Color32[] _bsdata = tex.GetPixels32(0);
            KDTree2D tree = new KDTree2D();
            List<KDTree2D.Train> treedata = new List<KDTree2D.Train>();
            FindBorder(tex.width,tex.height,_bsdata, treedata);//四次采样寻找边界,并把在边界上的点填入点集

            var node = tree.CreatKDTree(treedata);//用KDTree来查找最近点
            int w = tex.width;
            int h = tex.height;
            DateTime t1 = DateTime.Now;
            
            float maxlen = (float)Mathf.Sqrt(w * w + h * h) / 4;
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    var near = tree.KDTreeFindNearest(node, new KDTree2D.Train() { positionX = x, positionY = y });
                    float d = (float)Mathf.Sqrt((near.point.positionX - x) * (near.point.positionX - x) 
                        + (near.point.positionY - y) * (near.point.positionY - y));
                    if (_bsdata[y * w + x].a < 128)
                    {
                        d *= -1;

                        _bsdata[y * w + x]= _bsdata[(int)near.point.positionY * w + (int)near.point.positionX];

                    }
                    float dist = d / maxlen;
                    if (dist < -1) dist = -1;
                    if (dist > 1) dist = 1;
                    var b = (byte)(128 + 127.0f * dist);

                    _bsdata[y * w + x].a = b;//替换原alpha值为距离值,形状内>128,形状外<128

                }
            }
            DateTime t2 = DateTime.Now;
            Debug.Log("t=" + (t2 - t1).TotalSeconds);
            tex.SetPixels32(_bsdata);

            tex.Apply();
            
            System.IO.File.WriteAllBytes(filename_df,  tex.EncodeToPNG());//保存为新文件
            texs[name] = tex;
        }
#endif
    }
示例#2
0
    void LoadTexDistanceField(string name)
    {
#if UNITY_STANDALONE
        string filename    = System.IO.Path.Combine(Application.streamingAssetsPath, name + ".png");
        string filename_df = System.IO.Path.Combine(Application.streamingAssetsPath, name + "_df.png");
        //if (System.IO.File.Exists(filename_df))
        //{
        //    Texture2D tex = new Texture2D(1, 1);
        //    tex.LoadImage(System.IO.File.ReadAllBytes(filename_df));
        //    texs[name] = tex;
        //}
        //else
        {
            Texture2D tex = new Texture2D(1, 1);
            tex.LoadImage(System.IO.File.ReadAllBytes(filename));//加载原始图片
            Color32[]             _bsdata  = tex.GetPixels32(0);
            KDTree2D              tree     = new KDTree2D();
            List <KDTree2D.Train> treedata = new List <KDTree2D.Train>();
            FindBorder(tex.width, tex.height, _bsdata, treedata); //四次采样寻找边界,并把在边界上的点填入点集

            var      node = tree.CreatKDTree(treedata);           //用KDTree来查找最近点
            int      w    = tex.width;
            int      h    = tex.height;
            DateTime t1   = DateTime.Now;

            float maxlen = (float)Mathf.Sqrt(w * w + h * h) / 4;
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    var near = tree.KDTreeFindNearest(node, new KDTree2D.Train()
                    {
                        positionX = x, positionY = y
                    });
                    float d = (float)Mathf.Sqrt((near.point.positionX - x) * (near.point.positionX - x)
                                                + (near.point.positionY - y) * (near.point.positionY - y));
                    if (_bsdata[y * w + x].a < 128)
                    {
                        d *= -1;

                        _bsdata[y * w + x] = _bsdata[(int)near.point.positionY * w + (int)near.point.positionX];
                    }
                    float dist = d / maxlen;
                    if (dist < -1)
                    {
                        dist = -1;
                    }
                    if (dist > 1)
                    {
                        dist = 1;
                    }
                    var b = (byte)(128 + 127.0f * dist);

                    _bsdata[y * w + x].a = b;//替换原alpha值为距离值,形状内>128,形状外<128
                }
            }
            DateTime t2 = DateTime.Now;
            Debug.Log("t=" + (t2 - t1).TotalSeconds);
            tex.SetPixels32(_bsdata);

            tex.Apply();

            System.IO.File.WriteAllBytes(filename_df, tex.EncodeToPNG()); //保存为新文件
            texs[name] = tex;
        }
#endif
    }
示例#3
0
    void GenHeight()
    {
        try
        {
            Color32[]             _bsdata  = edit.GetPixels32(0);
            KDTree2D              tree     = new KDTree2D();
            List <KDTree2D.Train> treedata = new List <KDTree2D.Train>();
            FindBorder(edit.width, edit.height, _bsdata, treedata); //四次采样寻找边界,并把在边界上的点填入点集

            var      node = tree.CreatKDTree(treedata);             //用KDTree来查找最近点
            int      w    = edit.width;
            int      h    = edit.height;
            DateTime t1   = DateTime.Now;

            float maxlen = 0;
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    if (_bsdata[y * w + x].a > 0)//形状内
                    {
                        //var near = tree.KDTreeFindNearest(node, new KDTree2D.Train() { positionX = x, positionY = y });
                        var near = tree.BBFFindNearest(node, new KDTree2D.Train()
                        {
                            positionX = x, positionY = y
                        });
                        float d = (float)Mathf.Sqrt((near.point.positionX - x) * (near.point.positionX - x)
                                                    + (near.point.positionY - y) * (near.point.positionY - y));
                        float wl = d / 16.0f;
                        if (wl > 1.0f)
                        {
                            wl = 1.0f;
                        }
                        wl *= Mathf.PI * 0.5f;
                        wl  = Mathf.Sin(wl);
                        int l = (int)(wl * 255);
                        if (l > 255)
                        {
                            l = 255;
                        }
                        _bsdata[y * w + x].a = (byte)(l);
                    }
                    else
                    {
                        _bsdata[y * w + x].a = 0;//形状外
                    }
                }
            }

            editHeight = new Texture2D(w, h, TextureFormat.Alpha8, false);
            editHeight.SetPixels32(_bsdata, 0);
            editHeight.Apply();

            _bsdata               = GenNormal(w, h, _bsdata);
            editNormal            = new Texture2D(w, h, TextureFormat.ARGB32, false);
            editNormal.filterMode = FilterMode.Point;
            editNormal.SetPixels32(_bsdata, 0);
            editNormal.Apply();

            DateTime t2 = DateTime.Now;
            Debug.Log("GetTime=" + (t2 - t1).TotalSeconds);
        }
        catch
        {
        }
    }
示例#4
0
    void GenHeight()
    {
        try
        {
            Color32[] _bsdata = edit.GetPixels32(0);
            KDTree2D tree = new KDTree2D();
            List<KDTree2D.Train> treedata = new List<KDTree2D.Train>();
            FindBorder(edit.width, edit.height, _bsdata, treedata);//四次采样寻找边界,并把在边界上的点填入点集

            var node = tree.CreatKDTree(treedata);//用KDTree来查找最近点
            int w = edit.width;
            int h = edit.height;
            DateTime t1 = DateTime.Now;

            float maxlen = 0;
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    if (_bsdata[y * w + x].a > 0)//形状内
                    {
                        //var near = tree.KDTreeFindNearest(node, new KDTree2D.Train() { positionX = x, positionY = y });
                        var near = tree.BBFFindNearest(node, new KDTree2D.Train() { positionX = x, positionY = y });
                        float d = (float)Mathf.Sqrt((near.point.positionX - x) * (near.point.positionX - x)
                            + (near.point.positionY - y) * (near.point.positionY - y));
                        float wl = d / 16.0f;
                        if (wl > 1.0f) wl = 1.0f;
                        wl *= Mathf.PI * 0.5f;
                        wl=Mathf.Sin(wl);
                        int l = (int)(wl * 255);
                        if (l > 255) l = 255;
                        _bsdata[y * w + x].a = (byte)(l);
                    }
                    else
                    {
                        _bsdata[y * w + x].a = 0;//形状外
                    }
                }
            }

            editHeight = new Texture2D(w, h, TextureFormat.Alpha8, false);
            editHeight.SetPixels32(_bsdata, 0);
            editHeight.Apply();

            _bsdata = GenNormal(w, h, _bsdata);
            editNormal = new Texture2D(w, h, TextureFormat.ARGB32, false);
            editNormal.filterMode = FilterMode.Point;
            editNormal.SetPixels32(_bsdata, 0);
            editNormal.Apply();

            DateTime t2 = DateTime.Now;
            Debug.Log("GetTime=" + (t2 - t1).TotalSeconds);
        }
        catch
        {

        }
    }