- Home /
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.
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.
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
Follow this Question
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