如何用C#中的浮子计算

发布于 2025-02-13 04:26:38 字数 495 浏览 0 评论 0原文

我想以Unity进行编程地形生成器,并为Perlin Noise Terrain Generator提供了一些工作代码。我目前正在使用Unity(使用C#),我无法弄清楚如何将地形深度(Z)的产生分开(我想使用SQRT功能来计算的山脉/山谷的高度),这取决于X和地形深度取决于y。我的问题是,当我尝试将浮子转换为ints时,我将不得不将它们围起来,否则它们无法正常工作,但是我想平稳地计算地形,我可以将这2个高度组合成1个高度而不粗糙而结合起来地形?

float CalculateHeights(int x, int y)
{
    float xCoord = x;

        private int xz = float xCoord; 

    float yCoord = (float)y / height; 

        private int yz =float yCoord;

    int z = (xz + yz) / 2;

    return Mathf.Sqrt(z);
}

I want to programm a terrain generator in Unity and already have some working code for a Perlin Noise Terrain Generator. Im currently using Unity (using c#) and i cant figure out how to seperate the generation of the terrain depth(z)(the height of the mountains/depth of the valleys which i want to calculate using a Sqrt function) depending on x and the terrain depth depending on y. My problem is, when i try to convert the floats into ints, i would have to round them up or else they wont work, but i want to calculate the Terraindepth smoothly, is there a way i can combine those 2 heights into 1 without roughening the terrain?

float CalculateHeights(int x, int y)
{
    float xCoord = x;

        private int xz = float xCoord; 

    float yCoord = (float)y / height; 

        private int yz =float yCoord;

    int z = (xz + yz) / 2;

    return Mathf.Sqrt(z);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2025-02-20 04:26:38

如果您试图在Unity中使用Perlin噪声,则可以随时使用内置的发电机。

例如:

float height = Mathf.PerlinNoise(X,Y);

警告:如果您不使用此功能使用X和Y的浮点值,那么您将始终获得相同的值。

希望这会有所帮助。

docs- https://docs.unity3d.com/scriptreference/scriptreference/mmathf.perlinnoise.htmll < /a>

If you're trying to use Perlin Noise in Unity, you can always use the inbuilt generator.

Ex:

float height = Mathf.PerlinNoise(X,Y);

Warning though: If you don't use floating point values for X and Y with this function, you'll have issues with always getting the same value.

Hope this helps.

Docs - https://docs.unity3d.com/ScriptReference/Mathf.PerlinNoise.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文