【问题标题】:Trying to create a 3D voxel terrain, and I am trying to create random seeds rather than the same seed multiple times试图创建一个 3D 体素地形,我试图创建随机种子而不是多次创建相同的种子
【发布时间】:2023-03-13 06:55:01
【问题描述】:

我试图弄清楚如何在 C# 中随机化我的 Perlin 噪声,但无法使用我目前拥有的代码找到这样做的方法。这是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PerlinCubeGenScript01 : MonoBehaviour {

public float perlinNoise = 0f;
public float refinement = 0f;
public int multiplier = 0;
public int cubes = 0;
public float darkness;


void Start () {


    for (int i = 0; i < cubes; i++) {

        for (int j = 0; j < cubes; j++) {

            perlinNoise = Mathf.PerlinNoise(i * refinement, j * refinement);
            GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
            go.transform.position = new Vector3(i, Mathf.Round(perlinNoise * multiplier), j);

            int cubeY = (int) Mathf.Round(perlinNoise * multiplier);
            Debug.Log(cubeY);

            go.GetComponent<Renderer>().material.color = new Color(1, 1, 1, 0f);

        }
    }
}

void Update () {

}
}

【问题讨论】:

  • 你不是每次都选择0,0点吗?你没有改变 refinement 变量...

标签: c# random 3d perlin-noise voxel


【解决方案1】:

只要 Perlin Noise 函数允许您选择 2D 空间中的任何点,为什么不简单地使用随机数作为种子并从那里开始选择点?

void Start () {

    Random rnd = new Random();
    int seed = rnd.Next(Int32.MinValue, Int32.MaxValue);

    for (int i = 0; i < cubes; i++) {

        for (int j = 0; j < cubes; j++) {

            perlinNoise = Mathf.PerlinNoise(seed + (i * refinement), seed + (j * refinement));  
            // Note that if refinement never changes you are always picking the same point
            GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
            go.transform.position = new Vector3(i, Mathf.Round(perlinNoise * multiplier), j);

            int cubeY = (int) Mathf.Round(perlinNoise * multiplier);
            Debug.Log(cubeY);

            go.GetComponent<Renderer>().material.color = new Color(1, 1, 1, 0f);

        }
    }
}

【讨论】:

    猜你喜欢
    • 2018-09-08
    • 2014-02-10
    • 2012-12-09
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多