0
\$\begingroup\$

I am learning how to use the Perlin Noise with some Documentation. So far i mostly understood quite a lot.

Here i post the codes in order:

The Noise Code

public static class Noise {

public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, float scale)
    {
        float[,] noiseMap = new float[mapWidth, mapHeight]; //We create new 2D array

        if (scale <= 0)//If input scale is 0 we will have a 0 div!!
        {
            scale = 1f; 

        }

        //We loop through the noise map
        for (int y = 0; y < mapHeight; y++)
        {
            for (int x = 0; x < mapWidth; x++)
            {
                //Now at which point we are sampling our height values
                float SampleX = (float)x/scale; //For non integer values we divide by scale
                float SampleY = (float)y/scale;

                float perlinValue = Mathf.PerlinNoise(SampleX, SampleY);
                noiseMap[x, y] = perlinValue; //We assign the Perlin noise value to the 2D thing
            }
        }
        return noiseMap; //We return the noiseMap we generated
    } 
}

The Map Generator

public class MapGenerator : MonoBehaviour {

    public int mapWidth;
    public int mapHeight;
    public float noiseScale;

    public bool autoUpdate;

    public void GenerateMap()
    {
        //We will fetch the noiseMap from the Noise class
        float[,] noiseMap = Noise.GenerateNoiseMap(mapWidth, mapHeight, noiseScale);


        MapDisplay display = FindObjectOfType<MapDisplay>();
        display.DrawNoiseMap(noiseMap);


    }

}

Map Display

public class MapDisplay : MonoBehaviour {

    public Renderer textureRender;

    public void DrawNoiseMap(float[,] noiseMap)
    {
        int width = noiseMap.GetLength(0);
        int height = noiseMap.GetLength(1);

        Texture2D texture = new Texture2D(width, height);

        Color[] colourMap = new Color[width * height];
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                colourMap[y * width + x] = Color.Lerp(Color.black, Color.white, noiseMap[x, y]);
            }
        }
        texture.SetPixels(colourMap);
        texture.Apply();

        textureRender.sharedMaterial.mainTexture = texture;
        textureRender.transform.localScale = new Vector3();
    }
}

EditorControls Code

//This class controls the editor so instead of MB we use Editor
[CustomEditor (typeof(MapGenerator))]
//We have to say it is custom and the "typeof" of the class
//the custom editor is, in that case. MapGenerator

public class MapGeneratorEditor : Editor {

    public override void OnInspectorGUI()
    {
        MapGenerator mapGen = (MapGenerator)target;//The object being inspected.

        //If we changed anything in the Editor, it will autoupdate.

        if (DrawDefaultInspector())
        {
            if (mapGen.autoUpdate)
            {
                mapGen.GenerateMap();
            }
        }




        if (GUILayout.Button("Generate"))
        {
            mapGen.GenerateMap();
        }

    }

}

This last piece of code makes a button inside the Inspector so when i press it. The whole thing runs and gives me the Perlin Noise Map displayed on a plane.

Plane Configuration (Inspector) enter image description here

QUESTION

The problem i have right now is that when i press the Generate Button (see below):

The Planes Scale goes from (1, 1, 1) to (0, 0, 0) and i do not know why it does that. The only way to get back my Perlin Noise displayed on that Plane is if i manually setup the plane a Scale.

What i am missing here?

Thanks for answer.

enter image description here

\$\endgroup\$
5
  • 3
    \$\begingroup\$ You told it to: textureRender.transform.localScale = new Vector3(); sets scale to (0,0,0). You probably meant to set scale to (1,1,1) \$\endgroup\$ – Wyck Feb 2 '18 at 19:40
  • 3
    \$\begingroup\$ @Wyck that's an answer I'd upvote if you'd care to write it. :) \$\endgroup\$ – DMGregory Feb 2 '18 at 21:16
  • \$\begingroup\$ Wish i could upvote that comment. \$\endgroup\$ – WhiteGlove Feb 3 '18 at 11:46
  • 1
    \$\begingroup\$ @WhiteGlove once a question has an answer, we should not edit it to make it a different question that isn't addressed by that answer. Question posts are free, so just post a new question if you want to ask something new, even if it's related to the same project or feature. \$\endgroup\$ – DMGregory Feb 3 '18 at 14:58
  • \$\begingroup\$ Oh sorry, i just though that the previous question was too stupid (i missed something in the code) so then i switch it to something more complex. The edit took place before that new answer appeared i gave thumbs up (not showing because i do not have the required level in this community) \$\endgroup\$ – WhiteGlove Feb 3 '18 at 15:08
3
\$\begingroup\$

You told it to set the scale to zero when you wrote textureRender.transform.localScale = new Vector3(); That's because a new Vector3 is initialized to (0,0,0). Scale is multiplied, so "no change in scale" is represented by the value of 1 (not zero), so you probably meant to set the scale to (1,1,1), so you'll need to write new Vector3(1,1,1).

Alternatively, the default localScale is (1,1,1) so you could just avoid assigning it a new value altogether and keep its original value of (1,1,1).

\$\endgroup\$
1
  • \$\begingroup\$ Yeah i already solved it but did not updated the code. \$\endgroup\$ – WhiteGlove Feb 3 '18 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.