【问题标题】:For loop not starting from zeroFor循环不是从零开始
【发布时间】:2021-10-18 15:45:58
【问题描述】:

我正在尝试将生物群系系统实施到程序地形生成系统中。我这样做是为了让每个地形块的高度乘数根据它所在的生物群落而变化,但这会导致玩家能够进入两个生物群落相遇的地图下方。我试图使每个块的边缘高度为 0,以便它们正确连接。问题是,for 循环似乎不是从 0 开始的。我曾经让包含 x

for (int y = 0; y < borderedSize; y += meshSimplificationIncrement) {
    for (int x = 0; x < borderedSize; x += meshSimplificationIncrement) {
        float height;
        if (x <= 8 || x >= 233) {
            height = 5.0f;
            //Debug.Log(height);
            //Debug.Log("x" + x);
        } else if (y <= 8 || y >= 233) {
            height = 5.0f;
            //Debug.Log(height);
            //Debug.Log("y" + y);
        } else if (y <= 32 || y >= 225) {
            height = heightCurve.Evaluate(heightMap[x, y]) * heightMultiplier;
            height = height / 1.5f;
        } else if (x <= 32 || x >= 225) {
            height = heightCurve.Evaluate(heightMap[x, y]) * heightMultiplier;
            height = height / 1.5f;
        } else {
            height = heightCurve.Evaluate(heightMap[x, y]) * heightMultiplier;
        }
        int vertexIndex = vertexIndicesMap[x, y];
        Vector2 percent = new Vector2((x - meshSimplificationIncrement) / (float)meshSize, (y - meshSimplificationIncrement) / (float)meshSize);
        Vector3 vertexPosition = new Vector3(topLeftX + percent.x * meshSizeUnsimplified, height, topLeftZ - percent.y * meshSizeUnsimplified);

        meshData.AddVertex(vertexPosition, percent, vertexIndex);

        if (x < borderedSize - 1 && y < borderedSize - 1) {
            int a = vertexIndicesMap[x, y];
            int b = vertexIndicesMap[x + meshSimplificationIncrement, y];
            int c = vertexIndicesMap[x, y + meshSimplificationIncrement];
            int d = vertexIndicesMap[x + meshSimplificationIncrement, y + meshSimplificationIncrement];
            meshData.AddTriangle(a, d, c);
            meshData.AddTriangle(d, a, b);
        }

        vertexIndex++;
    }
}

【问题讨论】:

  • xy 从 0 开始。如果您愿意,可以将它们打印出来或使用调试器查看。
  • 假设变量的值是调试失败的可靠方法。始终验证变量的值,甚至作为最重要的事情触发。输出值的能力是您通过“Hello World”示例学到的一件事。
  • 你有一个复杂的 if/else 链,你有时检查 x,有时检查 y。使用调试器单步执行代码以验证(如果 x==0,您将永远不会检查 y)
  • Vector2 percent 对于 x=0 或 y=0 可以有负值是否正常?

标签: c# unity3d mesh procedural-generation procedural


【解决方案1】:

相信我,循环确实从 0 开始。

我会在边缘创建一个下降到 0 的斜坡。

const int rampSize = 32;

for (int y = 0; y < borderedSize; y += meshSimplificationIncrement)
{
    for (int x = 0; x < borderedSize; x += meshSimplificationIncrement)
    {
        float height = heightCurve.Evaluate(heightMap[x, y]) * heightMultiplier;
        int min = Math.Min(x, y);
        int max = Math.Max(x, y);
        int borderDist = Math.Min(min, borderedSize - max);
        if (borderDist < rampSize) {
            height = height * (float)borderDist / rampSize;
        }
        ...
    }
}

这会创建一个看起来像截头金字塔的斜坡。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 2021-12-18
    • 2013-06-22
    • 1970-01-01
    相关资源
    最近更新 更多