问题描述
我正在统一制作一个基于元胞自动机的沙子模拟,以及我现在做的方式(遍历纹理中的每个像素,检查它的颜色,并根据该颜色检查下面的像素并移到它的一侧,然后相应地移动它)非常低效且缓慢。我听说我可以用计算着色器做类似但更快的事情,我想知道如何根据计算着色器中的颜色将计算应用于纹理中的每个像素。 我之前的非计算着色器代码 (C#):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SimulateCells : MonoBehavIoUr
{
public Texture2D texture;
// Update is called once per frame
void Update()
{
UpdateCells();
texture.Apply();
}
void UpdateCells()
{
for (int y = 0; y < texture.height; y++)
{
for (int x = 0; x < texture.width; x++)
{
if (CellTypeDatabase.instance.colors.Contains(texture.GetPixel(x,y)) && x != texture.width - 1 && x != 0 && y != texture.height - 1 && y != 0)
{
CellObject cell = CellTypeDatabase.instance.cellObjects[CellTypeDatabase.instance.colors.IndexOf(texture.GetPixel(x,y))];
Color pixel = texture.GetPixel(x,y);
if (cell.useRigidGravity)
{
if (texture.GetPixel(x,y - 1) == Color.white || texture.GetPixel(x,y - 1) == CellTypeDatabase.instance.cellObjects[2].color)
{
texture.SetPixel(x,y,texture.GetPixel(x,y - 1));
texture.SetPixel(x,y - 1,pixel);
}
}
else if (cell.usePowderGravity)
{
if (texture.GetPixel(x,pixel);
}
else if (texture.GetPixel(x - 1,y - 1) == Color.white || texture.GetPixel(x - 1,texture.GetPixel(x - 1,y - 1));
texture.SetPixel(x - 1,pixel);
}
else if (texture.GetPixel(x + 1,y - 1) == Color.white || texture.GetPixel(x + 1,texture.GetPixel(x + 1,y - 1));
texture.SetPixel(x + 1,pixel);
}
}
else if (cell.useLiquidGravity)
{
if (texture.GetPixel(x,y - 1) == Color.white)
{
texture.SetPixel(x,Color.white);
texture.SetPixel(x,Color.white);
texture.SetPixel(x + 1,Color.white);
texture.SetPixel(x - 1,y) == Color.white)
{
texture.SetPixel(x,pixel);
}
}
}
else
{
continue;
}
}
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)