美文网首页
Unity制作点阵视频

Unity制作点阵视频

作者: Walk_In_Jar | 来源:发表于2022-12-26 10:12 被阅读0次

准备资料:AE、视频素材
视频素材地址 https://cloud.189.cn/t/naAviyrY3QVr (访问码:eyz9)
新建合成,导入素材,添加效果颜色键,扣除白色。


为了效果明显,加点位移动画


image.png

ctrl +m 导出
格式选择png序列,降低采样,帧率为5,点击渲染


image.png
image.png
image.png image.png

导入Unity


image.png

读取单张图片

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using System.IO;

public class GameEnter : MonoBehaviour
{
    const int width = 16*4;
    const int height = 9*4;

    public Transform parent;
    public Image itemPrefab;

    public Texture2D[] texture;

    const float padding=3;

    readonly Vector2 size = new Vector2(23, 23);
    public int samplingStep = 1;
    private Image[,] itemList = new Image[width, height];

    // Start is called before the first frame update
    void Start()
    {
        itemPrefab.rectTransform.sizeDelta = size;  
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                var img = GameObject.Instantiate(itemPrefab, new Vector3((i ) * (size.x + padding), (j) * (size.y + padding)),Quaternion.identity,parent);
                img.gameObject.SetActive(true);
                itemList[i, j] = img;
            }
        }
        Caculate(texture[30]);
    }

    void Caculate(Texture2D texture,int idx)
    {
        for (int i = 0; i < colored.Count; i++)
        {
            colored[i].color = Color.white;
        }
        colored.Clear();

        var widthStep = texture.width / samplingStep;
        var heightStep = texture.height / samplingStep;
        for (int i = 0; i <= heightStep; i += samplingStep)
        {
            for (int j = 0; j <= widthStep; j += samplingStep)
            {
                int colorPixelCnt = 0;
                for (int ii = 0; ii <= samplingStep; ++ii)
                {
                    for (int jj = 0; jj <= samplingStep; ++jj)
                    {
                        var color = texture.GetPixel(j * samplingStep + jj, i * samplingStep + ii);
                        if (color.r + color.g + color.b >= 1f)
                        {
                            ++colorPixelCnt;
                        }
                    }
                }
                if (colorPixelCnt > samplingStep)
                {
                    var x = j * (width-1) / widthStep;
                    var y = i * (height-1) / heightStep;

                    itemList[x, y].color = Color.red;
                }
            }
        }
    }
}

效果


image.png

读取连续图片

    void Start()
    {
        itemPrefab.rectTransform.sizeDelta = size;  
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                var img = GameObject.Instantiate(itemPrefab, new Vector3((i ) * (size.x + padding), (j) * (size.y + padding)),Quaternion.identity,parent);

                img.gameObject.SetActive(true);

                itemList[i, j] = img;
            }
        }
        StartCoroutine(LoadCorount());
    }
    IEnumerator LoadCorount()
    {
       for (int i = 0; i < texture.Length; i++)
       {
           Caculate(texture[i],i);
         yield return new WaitForSeconds(0.3f);
       }
    }

    private List<Image> colored = new List<Image>();
    void Caculate(Texture2D texture,int idx)
    {
        for (int i = 0; i < colored.Count; i++)
        {
            colored[i].color = Color.white;
        }
        colored.Clear();

        var widthStep = texture.width / samplingStep;
        var heightStep = texture.height / samplingStep;
        for (int i = 0; i <= heightStep; i += samplingStep)
        {
            for (int j = 0; j <= widthStep; j += samplingStep)
            {
                int colorPixelCnt = 0;
                for (int ii = 0; ii <= samplingStep; ++ii)
                {
                    for (int jj = 0; jj <= samplingStep; ++jj)
                    {
                        var color = texture.GetPixel(j * samplingStep + jj, i * samplingStep + ii);
                        if (color.r + color.g + color.b >= 1f)
                        {
                            ++colorPixelCnt;
                        }
                    }
                }
                if (colorPixelCnt > samplingStep)
                {
                    var x = j * (width-1) / widthStep;
                    var y = i * (height-1) / heightStep;

                    itemList[x, y].color = Color.red;
                    colored.Add(itemList[x, y]);
                }
            }
        }
    }

由于读写texture比较消耗性能,将读取到的信息存储为json


    void Start()
    {
        itemPrefab.rectTransform.sizeDelta = size;  
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                var img = GameObject.Instantiate(itemPrefab, new Vector3((i ) * (size.x + padding), (j) * (size.y + padding)),Quaternion.identity,parent);

                img.gameObject.SetActive(true);

                itemList[i, j] = img;
            }
        }
        //执行一次之后就不需要了
         for (int i = 0; i < texture.Length; i++)
         {
           Caculate(texture[i],i);
         }
         File.WriteAllText(Application.dataPath+"/../info.json",JsonUtility.ToJson(newit));

        
        newit = JsonUtility.FromJson<Items>(File.ReadAllText( Application.dataPath + "/../info.json"));
        StartCoroutine(LoadCorount());
       
    }

    Items newit = new Items();
    IEnumerator LoadCorount()
    {
      for (int i = 0; i < newit.items.Count; i++)
        {
            for (int l = 0; l < colored.Count; l++)
            {
                colored[l].color = Color.white;
            }
            colored.Clear();

            for (int j = 0; j < newit.items[i].pixels.Count; j++)
            {
                var pix = newit.items[i].pixels[j];
                itemList[pix.x, pix.y].color = Color.red;

                 colored.Add(itemList[pix.x, pix.y]);
            }
            yield return new WaitForSeconds(0.3f);
        }
    }

    private List<Image> colored = new List<Image>();
    void Caculate(Texture2D texture,int idx)
    {
        Item item = new Item
        {
            idx = idx
        };
        var widthStep = texture.width / samplingStep;
        var heightStep = texture.height / samplingStep;
        for (int i = 0; i <= heightStep; i += samplingStep)
        {
            for (int j = 0; j <= widthStep; j += samplingStep)
            {
                int colorPixelCnt = 0;
                for (int ii = 0; ii <= samplingStep; ++ii)
                {
                    for (int jj = 0; jj <= samplingStep; ++jj)
                    {
                        var color = texture.GetPixel(j * samplingStep + jj, i * samplingStep + ii);
                        if (color.r + color.g + color.b >= 1f)
                        {
                            ++colorPixelCnt;
                        }
                    }
                }
                if (colorPixelCnt > samplingStep)
                {
                    var x = j * (width-1) / widthStep;
                    var y = i * (height-1) / heightStep;

                    item.pixels.Add(new Pixel(x, y));
                }
            }
        }

        newit.items.Add(item);
    }
    [Serializable]
    class Items
    {
        public List<Item> items = new List<Item>();
    }
    [Serializable]
    class Item
    {
        public int idx;

        public List<Pixel> pixels=new List<Pixel> ();
    }
    [Serializable]
    class Pixel
    {
        public int x;
        public int y;
        public Pixel(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }

效果


Honeycam 2022-12-27 10-08-19.gif

相关文章

网友评论

      本文标题:Unity制作点阵视频

      本文链接:https://www.haomeiwen.com/subject/hefgqdtx.html