如何解决如何使用计算着色器将计算应用于纹理中的每个像素?
我正在统一制作一个基于元胞自动机的沙子模拟,以及我现在做的方式(遍历纹理中的每个像素,检查它的颜色,并根据该颜色检查下面的像素并移到它的一侧,然后相应地移动它)非常低效且缓慢。我听说我可以用计算着色器做类似但更快的事情,我想知道如何根据计算着色器中的颜色将计算应用于纹理中的每个像素。 我之前的非计算着色器代码 (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 举报,一经查实,本站将立刻删除。