#mesh time

1 messages · Page 1 of 1 (latest)

dull hill
#

made a thread

wind belfry
#

Kk

dull hill
#

here's my hitbox script from a soulslike

#

you can ignore the first half

#

the actual mesh generate might not make a lot of sense here lol

#
        for (int i = 0; i < corners.Count - 8; ++i)
        {
            for (int j = 0; j < 3; ++j)
            {
                int i1 = i;
                int i2 = i ^ (1 << j);
                int i3 = i + 8;
                int i4 = i2 + 8;

                if (i1 < i2 && i1 < i3)
                {
                    quads[index] = i1;
                    quads[index + 1] = i2;
                    quads[index + 2] = i4;
                    quads[index + 3] = i3;
                    index += 4;
                }

            }
        }
#

i was being too clever

#

The script draws a hollow box that sweeps through several box colliders

#

the important part is that it shows...

#
        var vertices = new Vector3[corners.Count];
        var quads = new int[corners.Count * 12];

        Mesh mesh = new();

        mesh.SetVertices(vertices);
        mesh.SetIndices(quads, MeshTopology.Quads, 0);
        mesh.SetColors(colors);
        filt.mesh = mesh;
#

I create an array of Vector3's to store the vertices

wind belfry
#

Ok

dull hill
#

If you are doing a 10x10 visualization, then you will get 100 vertices

wind belfry
#

👍

dull hill
#

quads is a list of integers. Each group of 4 integers is a single quad

#

so if it contained:

#
[0,1,2,3,0,1,4,5]
#

that would be two quads

wind belfry
#

Ok

dull hill
#

the first quad would be made of vertices 0-3

#

the second quad would be indices 0, 1, 4, 5

#

so these quads would be connected to each other, and would thus have smooth shading

wind belfry
#

👌

dull hill
#

actually, do you want smooth shading?

wind belfry
#

Doesn’t really matter, I’m just doing this as a conform tool for a vfx

dull hill
#

ah, okay, so you're in the same boat as I am

dull hill
#

so, you'll need to compute the Z value for each [x,y] pair

wind belfry
#

So a quad of four hits represents what?

#

Ints

dull hill
#

The integers are the indices of the vertices.

wind belfry
#

Oh okay

#

So the general approach is to treat x and y and your independents and z as the dependent?

#

sorry, I feel bad for taking your time, if tot want we can come back to this alter

#

Later

#

I think i understand what you’re saying about the 2d array with z values. Is it that, essentially, the 2d array represents the x y combinations and then you store the z result corresponding to it?

dull hill
#

Mesh mesh = new();

// 10 by 10 grid
int size = 10;

float[] x_inputs = new float[size];
float[] y_inputs = new float[size];

// compute the actual x and y values that go into the function.
// since "x" and "y" below will be the index in the grid, not
// the actual values of x and y

// for this example, the domain for both X and Y is -1 to 1

float min_x = -1;
float max_x = 1;
float min_y = -1;
float max_y = 1;

for (int x = 0; x < size; ++x) {
   float x_t = Mathf.InverseLerp(0, size - 1, x);
   x_inputs[x] = Mathf.Lerp(min_x, max_x, x_t);
}

for (int y = 0; y < size; ++y) {
   float input_y = Mathf.Lerp(min_y, max_y, y_t);
   y_inputs[y] = Mathf.InverseLerp(0, size - 1, y);
}

float[,] values = new float[size, size];
Vector3[] indices = new Vector3[size * size];
int[] quads = new int[(size-1) * (size-1) * 4]; // a 2-by-2 grid of vertices only has 1 quad in it!

for (int x = 0; x < size; ++x) {
  for (int y = 0; y < size; ++y) { 
    float input_x = x_inputs[x];
    float input_y = y_inputs[y];
    values[x,y] = func(input_x, input_y);
    indices[x*size+y] = new Vector3(input_x, input_y, values[x,y]);
  }
}

int offset = 0;

for (int x = 0; x < size - 1; ++x) { 
  for (int y = 0; y < size - 1; ++y) {
    quads[offset++] = x * size + y;
    quads[offset++] = (x+1) * size + y;
    quads[offset++] = x * size + (y+1);
    quads[offset++] = (x+1) * size + (y+1);
  }
}

mesh.SetVertices(vertices);
mesh.SetIndices(quads, MeshTopology.Quads, 0);
#

that oughta do it

#

i didn't actually check if this has any syntax or compiler errors whoops

#

the high level idea:

#
  • compute the inputs to the function. we need to be able to turn array indices into the actual x and y inputs
  • compute the function's values
  • set the vertex positions. note that vertices is 1D, so we do x * size + y to turn our 2D coordinates into a 1D coordinate
  • for each group of four vertices, make a quad out of them
  • set the data
#

one note: i might have gotten the winding backwards

#

i forget if unity does clockwise or counterclockwise winding

#

basically, the order of the indices determines which way the face is oriented