MWS/Terrain/Assets/Scripts/GrassCalculation.compute
2021-09-16 19:47:54 +02:00

37 lines
1.0 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> oldVegetation;
RWStructuredBuffer<float> vegetation;
int sizeX;
int sizeY;
int index(int x, int y){
return x + sizeX *y;
}
float get(RWStructuredBuffer<float> array, int x, int y){
int _index = index(x, y);
if(_index<0 || _index >= sizeX * sizeY){
return 0;
}
return array[_index];
}
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)
{
int x = id.x;
int y = id.y;
// TODO: insert actual code here!
vegetation[index(x, y)] = clamp(oldVegetation[x, y] + laplace(oldVegetation,x,y ),0,1);
}