I am trying to make terraformer in Unity with marching cubes. I am using a compute shader to edit a 3d texture which is used to create the mesh. I want to implement different shapes for changing the mesh. The different shapes work by finding the pixels inside of the it and then changing the blackness value of the pixels. The sphere works but the cube doesn't. I am wondering if anybody knows what I am doing wrong.
#pragma kernel CSMain
RWTexture3D<float> EditTexture;
int size;
int3 brushCentre;
int3 brushSize;
float deltaTime;
float weight;
int shape;
float3 cube0;
float3 cube1;
float3 cube2;
float3 cube4;
float3 cylinder0;
float3 cylinder1;
// return smooth value between 0 and 1 (0 when t = minVal, 1 when t = maxVal)
float smoothstep(float minVal, float maxVal, float t) {
t = saturate((t - minVal) / (maxVal - minVal));
return t * t * (3 - 2 * t);
}
[numthreads(8, 8, 8)]
void CSMain(int3 id : SV_DispatchThreadID)
{
const int b = 4;
if (id.x >= size - b || id.y >= size - b || id.z >= size - b) {
return;
}
if (id.x <= b || id.y <= b || id.z <= b) {
return;
}
//Sphere WORKS
if (shape == 0)
{
int3 offset = id - brushCentre;
int sqrDst = dot(offset, offset);
if (sqrDst <= brushSize.x * brushSize.x) {
float dst = sqrt(sqrDst);
//float brushWeight = 1 - smoothstep(brushSize.x * 0.7, brushSize.x, dst);
EditTexture[id] += weight * deltaTime;
}
}
//Cube DOESNT WORK
if (shape == 1)
{
float3 ab = cube0 - cube1;
float3 ac = cube0 - cube2;
float3 ae = cube0 - cube4;
float3 u = cross(ab, ac);
float3 v = cross(ae, ac);
float3 w = cross(ae, ab);
if (dot(u, id) >= dot(u, cube0) && dot(u, id) <= dot(u, cube4)) // z
{
if (dot(v, id) <= dot(v, cube0) && dot(v, id) >= dot(v, cube1)) // x
{
if (dot(w, id) >= dot(w, cube0) && dot(w, id) <= dot(w, cube2)) // y
{
EditTexture[id] += weight * deltaTime;
}
}
}
}
/*
if (shape == 2)
{
float3 c = cylinder1 - cylinder0;
float3 a = id - cylinder0;
float3 b = id - cylinder1;
if (dot(a, c) >= 0)
{
if (dot(b, c) <= 0)
{
if (length(cross(a, c)) / length(c) <= size.x)
{
EditTexture[id] += weight * deltaTime;
}
}
}
}
*/
/*
if (shape == 1)
{
EditTexture[id] = sin(id.x) * cos(5 * id.y) * 0.2;
}
*/
}