Unity制作点阵视频
准备资料: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; // 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 = img; } } Caculate(texture); } void Caculate(Texture2D texture,int idx) { for (int i = 0; i < colored.Count; i++) { colored.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.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 = img; } } StartCoroutine(LoadCorount()); } IEnumerator LoadCorount() { for (int i = 0; i < texture.Length; i++) { Caculate(texture,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.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.color = Color.red; colored.Add(itemList); } } } }
由于读写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 = img; } } //执行一次之后就不需要了 for (int i = 0; i < texture.Length; i++) { Caculate(texture,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.color = Color.white; } colored.Clear(); for (int j = 0; j < newit.items.pixels.Count; j++) { var pix = newit.items.pixels; itemList.color = Color.red; colored.Add(itemList); } 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); } class Items { public List<Item> items = new List<Item>(); } class Item { public int idx; public List<Pixel> pixels=new List<Pixel> (); } 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
页:
[1]