ProjektMWS/Projekt MWS/Assets/Scripts/GrassCalculation.compute
2021-07-06 19:43:27 +02:00

61 lines
1.6 KiB
Plaintext

// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain
// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
RWStructuredBuffer<float> vegetation;
RWStructuredBuffer<float> oldVegetation;
RWStructuredBuffer<float> water;
RWStructuredBuffer<float> oldWater;
int sizeX, sizeY;
float DWater;
float DVegetation;
float feedRate;
float killRate;
float dT;
int stepsNumber;
int index(int x, int y) {
return x + y * sizeX;
}
float get(RWStructuredBuffer<float> array, int x, int y) {
if (x<0 || x>sizeX || y<0 || y>sizeY) {
return 0;
}
return array[index(x, y)];
}
float laplace(RWStructuredBuffer<float> array, int x, int y) {
return 0.2 * (get(array, x + 1, y) +
get(array, x - 1, y) +
get(array, x, y + 1) +
get(array, x, y - 1) +
0.05 * get(array, x + 1, y + 1) + get(array, x - 1, y + 1) + get(array, x + 1, y - 1) + get(array, x - 1, y - 1) - get(array, x, y));
}
[numthreads(8, 8, 1)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
// TODO: insert actual code here!
int x = id.x;
int y = id.y;
float w = oldWater[index(x, y)];
float r = oldVegetation[index(x, y)];
float kill = killrate * killModifier[index(x, y];
float feed =feedrate *feddModifier[index(x, y];
water[index(x, y)] = clamp(w + dT * (DWater * laplace(oldWater,x, y)
- w * w * r * r + feedRate * (0.5 + w)),0,1);
vegetation[index(x, y)] = clamp(r + dT * (DVegetation * laplace(oldVegetation, x, y) + r * (w * r - killRate)),0,1);
}