Советы по процедурной сетке с круглыми краями - PullRequest
0 голосов
/ 19 марта 2020

Я пытаюсь придумать код, который позволяет пользователю контролировать округлость процедурной сетки и размеры сетки. Ширина и высота определяются пользователем через GUI вместе с округлостью. См. Иллюстрацию ниже enter image description here

Я добился определенного прогресса, однако значение округлости влияет на размеры, что является нежелательным побочным эффектом. См. Ниже enter image description here

    // Decide which side is smaller and set size of cell
    smallerSide = Math.Min(width, depth);
    float cellSize = (smallerSide / CELLCOUNT) * roundness;

    // Set size of the central cell, to avoid unnecessary vertices
    var xCenterSize = width - (cellSize * CELLCOUNT);
    var yCenterSize = depth - (cellSize * CELLCOUNT);

    for (int i = 0, y = 0; y <= CELLCOUNT; y++)
    {
        for (int x = 0; x <= CELLCOUNT; x++, i++)
        {
            // To avoid conditional in double loop the Offset return 0 or 1 ...
            // base on position in first or second half
            var xOffset = (int)Math.Floor((x * cellSize) / ((CELLCOUNT +1) * cellSize * 0.5f));
            var yOffset = (int)Math.Floor((y * cellSize) / ((CELLCOUNT + 1) * cellSize * 0.5f));

            // Make vertex for each step and if second half that add the offset
            vertices[i] = new Vector3(x * cellSize + xCenterSize* xOffset, 0, y * cellSize + yCenterSize * yOffset);
            controlers[i] = new Vector3(CELLCOUNT * cellSize*0.5f + xCenterSize * xOffset, 0, CELLCOUNT* cellSize * 0.5f + yCenterSize * yOffset);

            // Get Normals
            normals[i] = (vertices[i] - controlers[i]).normalized;

            // Multiply normals to get right distance from controllers to edge
            // HERE I ASSUME THIS iS WRONG
            var multiplier = new Vector3(cellSize * CELLCOUNT * 0.5f, 0, cellSize * CELLCOUNT * 0.5f);

            // Move vertices to right postion
            normals[i] = Vector3.Scale(normals[i], multiplier);
            vertices[i] = controlers[i] + normals[i] * roundness;

            uv[i] = new Vector3((float)x / CELLCOUNT, 0, (float)y / CELLCOUNT);
            tangents[i] = tangent;
        }
    }

Любой намек, почему на размеры сетки влияет параметр округлости? Я думаю, это нормальные множители, если да, какой-нибудь совет по правильной математике?

1 Ответ

0 голосов
/ 20 марта 2020

Это было просто.

vertices[i] = controlers[i] + normals[i];
...