2020-11-28 16:54:41 +02:00

55 lines
1.6 KiB
Plaintext

#pragma kernel ConvertTrianglesToDotsKernel
struct TriangleInput {
float3 p1;
float3 p2;
float3 p3;
};
StructuredBuffer<TriangleInput> triangles;
RWStructuredBuffer<float3> positionOutputs;
float maxDotAmount = 0.225;
float seed = 1.32523252;
int pointCountPerTriangle = 10;
float pointsPerUnit = 1;
float rand(float3 co){
return (frac(sin(dot(co ,float3(12.9898,78.233,45.5432))) * 43758.5453));
}
float triangleArea (float3 a, float3 b, float3 c) {
//for simplicitys sake ignoring the y dimension and finding area of 2D triangle
return (a.x*(b.z-c.z) + b.x*(c.z-a.z) + c.x*(a.z-b.z))/2.0;
}
float3 randomPointOnTriangle (float3 a, float3 b, float3 c, float index) {
float r1 = rand(index*b*seed);
float r0 = r1 + rand(index*c*seed) * (1-r1);//bigger
return float3(r1 * a.x + (r0-r1)*b.x + (1-r0)*c.x,
r1 * a.y + (r0-r1)*b.y + (1-r0)*c.y,
r1 * a.z + (r0-r1)*b.z + (1-r0)*c.z
);
}
[numthreads(16,1,1)]
void ConvertTrianglesToDotsKernel (uint3 id : SV_DispatchThreadID)
{
TriangleInput tri = triangles[id.x];
int startIndex = id.x*pointCountPerTriangle;
//based on triangle size get point amount
float points = min(triangleArea(tri.p1,tri.p2,tri.p3)*pointsPerUnit,pointCountPerTriangle);
for (int i = 0; i < pointCountPerTriangle; ++i) {
if(i < points) {
positionOutputs[startIndex + i] = randomPointOnTriangle(tri.p1,tri.p2,tri.p3, saturate((float)i/(float)points) );//tri.p1 * saturate(1 - (float)i/(float)points) + tri.p2 * saturate((float)i/(float)points);
}
else {
positionOutputs[startIndex + i] = float3(0,0,0);
}
}
}