Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FederalRazer89 · Jan 17, 2021 at 10:14 PM · editoreditorwindowperlin noise

Mathf.PerlinNoise only outputs a single number in editor window

I have created a button that opens a editor window that displays a texture as a way to preview my heightmap/noisemap, but when i try to use Mathf.PerlinNoise i only get the nummer 0.4652731 regardless of what values i input. Any ideas why it only outputs 0.4652731?


On the third code sample you can see the specific problem, but i included the whole call sequence.


 if(GUILayout.Button("Preview Noise"))
         {
             texture = TerrainNoiseGeneration.NoiseMap(10,10);
             TerrainGeneratorEditorWindow.OpenNoisePreviewWindow(texture);
         }


 public static void OpenNoisePreviewWindow(Texture2D previewTexture)
 {

     EditorWindow.GetWindow<TerrainGeneratorEditorWindow>("Preview Noise");
     EditorWindow.GetWindow<TerrainGeneratorEditorWindow>("Preview Noise").texture = previewTexture;

 }
 
 void OnGUI()
 {
     GUILayout.Label("Test", EditorStyles.boldLabel);
     //texture = new Texture2D(200, 200);
     if (texture == null)
     {
         texture = Texture2D.blackTexture;
     }
     
     EditorGUI.DrawPreviewTexture(new Rect(20, 20, 300, 300), texture);
 }


     public static Texture2D NoiseMap(int mapWidth, int mapHeight)
     {
 
 
 
         float[,] noiseMap = new float[mapWidth, mapHeight];
         float maxNoiseHeight = float.MinValue;
         float minNoiseHeight = float.MaxValue;
 
         float noiseHeight = 0;
         //Mathf.PerlinNoise
         //        float centerWidth = mapWidth / 2f;
         //        float centerHeight = mapHeight / 2f;
 
         for (int y = 0; y < mapHeight; y++)
         {
             for (int x = 0; x < mapWidth; x++)
             {
                 //values
                 float sampleX = x;
                 float sampleY = y;
 
                 float perlinNoise = Mathf.PerlinNoise(sampleX, sampleY); //0.4652731 ???
                 noiseHeight = perlinNoise;
                 if (noiseHeight > maxNoiseHeight)
                 {
                     maxNoiseHeight = noiseHeight;
                 }
                 else if (noiseHeight < minNoiseHeight)
                 {
                     minNoiseHeight = noiseHeight;
                 }
 
                 noiseMap[x, y] = perlinNoise;

             }
 
         }
 
         for (int y = 0; y < mapHeight; y++)
         {
             for (int x = 0; x < mapWidth; x++)
             {
                 noiseMap[x, y] = Mathf.InverseLerp(minNoiseHeight, maxNoiseHeight, noiseMap[x, y]);
                 
             }
         }
 
         int width = noiseMap.GetLength(0);
         int height = noiseMap.GetLength(1);
 
         Texture2D texture = new Texture2D(width, height);
         texture.GetPixels();
 
         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();
 
 
 
         return texture;
     }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eno-Khaon · Jan 17, 2021 at 11:29 PM

Unity's Perlin Noise sampling repeats every 1.0 value. Because you're sampling using your integer iterators directly, you're sampling exactly 1 position in the repeating pattern over and over again.

For a simple, scale-independent solution to the problem, you can divide your inputs by your map width and height.

 float sampleX = (float)x / mapWidth;
 float sampleY = (float)y / mapHeight;
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FederalRazer89 · Jan 18, 2021 at 11:16 PM 0
Share

Thanks, that kind of helped, so i tried to just added additional float value to the X and Y value and it solved it


  float sampleX = (x + 0.01f) / mapWidth;
  float sampleY = (y + 0.01f) / mapHeight;


avatar image Eno-Khaon FederalRazer89 · Jan 20, 2021 at 04:23 AM 1
Share

Ah, whoops. $$anonymous$$y mistake on that. I forgot to factor in integer math being performed in this case. $$anonymous$$odified my answer to reflect my intent, although your solution works well.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

154 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Editor: Drawing outside the own window 0 Answers

How to get EditorWindow client rect? 1 Answer

Reflection on class variable values null on editor startup 0 Answers

Listen to Keyboard Events on EditorWindow 1 Answer

Set the Rect of PopupWindowContent 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges