I have been following [Unity’s Perlin reference][1] and [this reference][2] attempting to just easily view my noise function by making a texture of it.
Here is the code:
internal void init()
{
Texture2D noiseTex = new Texture2D(20,20);
Color[] pix = new Color[noiseTex.width * noiseTex.height];
SimplexNoiseGenerator3 noiseGen3 = new SimplexNoiseGenerator3();
int ytex = 0;
//loops through texture coords applying a noise color at each coord.
while(ytex < noiseTex.height){
int xtex = 0;
while(xtex < noiseTex.width){
float sample = noiseGen3.GetNoise(xtex,ytex,0);
float value = (255 * sample);
pix[ytex * noiseTex.width + xtex] = new Color(value, value, value);
Color thisColor = new Color(value,value,value);
//noiseTex.SetPixel(xtex,ytex, thisColor);
//noiseTex.SetPixels (pix);
//noiseTex.Apply();
Debug.Log ("at coords " + xtex + " " + ytex + "is color " + pix[ytex*noiseTex.width + xtex]);
Debug.Log ("get pixel color " + xtex + " " + ytex + " " + noiseTex.GetPixel(xtex, ytex));
xtex++;
}
ytex++;
}
noiseTex.SetPixels (pix);
noiseTex.Apply();
Mesh meshTex = new Mesh();
GameObject texGO = new GameObject();
texGO.name = "texGO";
texGO.AddComponent<MeshFilter>().mesh = meshTex;
texGO.AddComponent<MeshRenderer>();
Vector3[] meshTexVerts = new Vector3[4];
meshTexVerts[0] = new Vector3(-20,0,0);
meshTexVerts[1] = new Vector3(-20,20,0);
meshTexVerts[2] = new Vector3(0,20,0);
meshTexVerts[3] = new Vector3(0,0,0);
int[] meshTexTris = new int[6];
meshTexTris[0] = 0;
meshTexTris[1] = 1;
meshTexTris[2] = 2;
meshTexTris[3] = 0;
meshTexTris[4] = 2;
meshTexTris[5] = 3;
texGO.renderer.material.mainTexture = noiseTex;
Vector2[] meshUV = new Vector2[meshTexVerts.Length];
/*meshUV[0] = new Vector2(0,0);
meshUV[1] = new Vector2(0,20);
meshUV[2] = new Vector2(20,20);
meshUV[3] = new Vector2(20,0);
*/
int i = 0;
while(i< meshUV.Length){
meshUV <em>= new Vector2(meshTexVerts_.x, meshTexVerts*.z);*_</em>
* i++;*
* }*
* meshTex.vertices = meshTexVerts;*
* meshTex.triangles = meshTexTris;*
* meshTex.uv = meshUV;*
* meshTex.Optimize();*
My debugging shows that the noise value is correct between 0-255 for each (xtex,ytex). The color array pix[] also inserts the value appropriately for each coord.
Unfortunately, noiseTex.GetPixel returns the same RBG value for each coord, and I’m unable to figure out why. As such my whole mesh displays as a single shade of grey.
Any ideas to the issue or other options of implementation are appreciated.
*[1]: http://docs.unity3d.com/Documentation/ScriptReference/Mathf.PerlinNoise.html*_
*[2]: http://cabbynode.net/*_