x


Help Perlin Noise for Cubes?

I got this code from the wiki unity3d, i want blocks, no terrain. I don't understand PerlinNoise but is the only way to create random cubes like minecraft. I don't know other mode to make random cubes, help please or a simple code in C# or Js for cubes. Sorry bad english i'm from Argentina.

using UnityEditor;
using UnityEngine;
using System.Collections;

public class TerrainPerlinNoise : ScriptableWizard {

    public float Tiling = 10.0f;

    [MenuItem("Terrain/Generate from Perlin Noise")]
    public static void CreateWizard(MenuCommand command)
    {
        ScriptableWizard.DisplayWizard("Perlin Noise Generation Wizard", typeof(TerrainPerlinNoise));
    }

    void OnWizardUpdate()
    {
        helpString = "This small generation tool allows you to generate perlin noise for your terrain.";
    }

    void OnWizardCreate()
    {
        GameObject obj = Selection.activeGameObject;

        if (obj.GetComponent<Terrain>())
        {
            GenerateHeights(obj.GetComponent<Terrain>(), Tiling);
        }
    }

    public void GenerateHeights(Terrain terrain, float tileSize)
    {
        float[,] heights = new float[terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight];

        for (int i = 0; i < terrain.terrainData.heightmapWidth; i++)
        {
            for (int k = 0; k < terrain.terrainData.heightmapHeight; k++)
            {
                heights[i, k] = Mathf.PerlinNoise(((float)i / (float)terrain.terrainData.heightmapWidth) * tileSize, ((float)k / (float)terrain.terrainData.heightmapHeight) * tileSize)/10.0f;
            }
        }

        terrain.terrainData.SetHeights(0, 0, heights);
    }
}
more ▼

asked Feb 01, 2013 at 09:05 PM

sxriter gravatar image

sxriter
1 3 4 4

Sorry, but this site is for specific questions, and not for "I found this script and I want to make it do something else, who can change it for me?".

Feb 01, 2013 at 09:45 PM Wolfram
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

alt textHello, here is a page that shows you what the Perlin noise is, http://answers.unity3d.com/questions/188721/creating-perlin-noise.html#answer-383517 it generates lots of little bumps like an irregular sinus. What you can do, is instantiate 100 blocks along the X axis, 100 blocks along the Y axis, keep their width and length the same but modify their height with Perlin noise function. to make a really nice bunch of hills, if you multiply 2 Perlin noise functions against each other, then they will make strange irregular hills, because 1x1 equals 1 and 0x0 equals 0, imagine multiplying 2 of the wiggles like in the picture on the link. It becomes a 3d Mountain scape. I would do you some code that I'm a bit tired right now!

private var rot : Quaternion;
rot.eulerAngles = Vector3(0, 0, 0);
var terraincube: GameObject;

function Start (){
for (var px:float = 0; px < 100; px ++) {
    for (var py:float = 0; py< 100; py ++) {

var Perlin1 = Mathf.PerlinNoise(px/30, 76);
var Perlin2 = Mathf.PerlinNoise(py/30, 22);
Instantiate(terraincube, Vector3(py-50, Perlin1*40*Perlin2, px-50), rot);


}

}
}   
cubreterrain.png (115.5 kB)
more ▼

answered Feb 01, 2013 at 09:17 PM

MountDoomTeam gravatar image

MountDoomTeam
1.2k 188 189 206

/30 is closeness of hills, 40 is height of hills.

Feb 01, 2013 at 09:50 PM MountDoomTeam

thank you men! it works

Feb 01, 2013 at 10:30 PM sxriter

Then please mark his answer as "accepted", and don't post comments in the "Your answer" field, use the "add new comment" button instead.

Feb 01, 2013 at 10:35 PM Wolfram
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x59
x52
x47
x2

asked: Feb 01, 2013 at 09:05 PM

Seen: 2688 times

Last Updated: Feb 04, 2013 at 05:41 PM