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 LuminosityXVII · Mar 08, 2014 at 11:38 PM · errorbeginnerperlin

Having trouble getting the sample code for Mathf.PerlinNoise to work.

I'm referring to this page, with the specific sample code as follows:


 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour {
     public int pixWidth;
     public int pixHeight;
     public float xOrg;
     public float yOrg;
     public float scale = 1.0F;
     private Texture2D noiseTex;
     private Color[] pix;
     void Start() {
         noiseTex = new Texture2D(pixWidth, pixHeight);
         pix = new Color[noiseTex.width * noiseTex.height];
         renderer.material.mainTexture = noiseTex;
     }
     void CalcNoise() {
         float y = 0.0F;
         while (y < noiseTex.height) {
             float x = 0.0F;
             while (x < noiseTex.width) {
                 float xCoord = xOrg + x / noiseTex.width * scale;
                 float yCoord = yOrg + y / noiseTex.height * scale;
                 float sample = Mathf.PerlinNoise(xCoord, yCoord);
                 pix[y * noiseTex.width + x] = new Color(sample, sample, sample);
                 x++;
             }
             y++;
         }
         noiseTex.SetPixels(pix);
         noiseTex.Apply();
     }
     void Update() {
         CalcNoise();
     }
 }


As a pretty new beginner, I don't really know how to apply this, or what to make it a component of. That's part of the problem.

Before I even try anything, however, when I paste this sample code into a new script and save it, the console error immediately spits an error at me:

 Assets/Scripts/Perlin.cs(25,37): error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)

So, inside the CalcNoise() function, I looked at line 25, where it appears it doesn't like the fact that "pix[y * noiseTex + x]" contains x and y as floats instead of integers.

I'm not sure how to fix it properly, but I tried starting by declaring x and y as integers. This at least made the error disappear from the console. I then tried adding a new plane to the scene and making this script a component of it, but then when I ran it I got a new error:

 Array size must be at least width*height
 UnityEngine.Texture2D:SetPixels(Color[])
 Perlin:CalcNoise() (at Assets/Scripts/Perlin.cs:30)
 Perlin:Update() (at Assets/Scripts/Perlin.cs:34)

...And from here, I'm shamefully lost.

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 robertbu · Mar 08, 2014 at 11:49 PM

Below is a fixed version of this script. I also established some default values at the top of the file that gives you a result similar to the one in the reference. Create a Quad, attach an material using Unlit/Texture for a the shader, attach this script and hit run:

 using UnityEngine;
 using System.Collections;
 
 public class Bug40 : MonoBehaviour {
     public int pixWidth = 1024;
     public int pixHeight = 1024;
     public float xOrg = 1.2F;
     public float yOrg = 0.34F;
     public float scale = 11.0F;
     private Texture2D noiseTex;
     private Color[] pix;
     void Start() {
         noiseTex = new Texture2D(pixWidth, pixHeight);
         pix = new Color[noiseTex.width * noiseTex.height];
         renderer.material.mainTexture = noiseTex;
     }
     void CalcNoise() {
         float y = 0.0F;
         while (y < noiseTex.height) {
             float x = 0.0F;
             while (x < noiseTex.width) {
                 float xCoord = xOrg + x / noiseTex.width * scale;
                 float yCoord = yOrg + y / noiseTex.height * scale;
                 float sample = Mathf.PerlinNoise(xCoord, yCoord);
                 pix[(int)(y * noiseTex.width + x)] = new Color(sample, sample, sample);
                 x++;
             }
             y++;
         }
         noiseTex.SetPixels(pix);
         noiseTex.Apply();
     }
     void Update() {
         CalcNoise();
     }
 }

it is a bit strange for the example code to call CalcNoise() every frame. I guess that is to allow you to play with xOrg, yOrg, and scale in the inspector and see the result.

Comment
Add comment · Show 1 · 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 LuminosityXVII · Mar 09, 2014 at 12:35 AM 0
Share

Thank you kindly, that worked! Though I'm kind of annoyed I didn't think of such an easy fix as simply using (int)().

Anyhow, good point about calling CalcNoise() every frame. Not that it makes all that much difference, but I changed it to the following so I can play with the values and have it only update when I want it to:

 void Update()
 {
     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space))
     {
         CalcNoise();
     }
 }

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

20 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

Related Questions

Unity Script Editor Not Working 1 Answer

Errors while assigning material to other object's by scripting. 1 Answer

Error line 22! Help. i dont know why? 1 Answer

(C#) Problem with animations not playing 1 Answer

xpecting (, found 'OnGUI' 2 Answers


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