I am having a lot of trouble with perlin noise right now.
This is the code I am using to generate the noise:
using System.Collections;
using System.Collections.Generic;
using UnityEditor.PackageManager.UI;
using UnityEngine;
public class NoiseTextureGenerator : MonoBehaviour
{
Texture2D noiseTexture;
public Texture2D GenerateNoise(int pixelWidth, int pixelHeight, float xOrigin, float yOrigin, float scale)
{
Color[] colors = new Color[pixelWidth * pixelHeight];
noiseTexture = new Texture2D(pixelWidth, pixelHeight);
for (float x = 0.0f; x < noiseTexture.width; x++ )
{
for(float y = 0.0f; y < noiseTexture.height; y++)
{
float xCoord = xOrigin + x / noiseTexture.width * scale;
float yCoord = yOrigin + y / noiseTexture.height * scale;
float sample = Mathf.PerlinNoise(xCoord, yCoord);
colors[Mathf.RoundToInt(y) * noiseTexture.width + Mathf.RoundToInt(x)] = new Color(sample, sample, sample);
}
}
noiseTexture.SetPixels(colors);
noiseTexture.Apply();
return noiseTexture;
}
}
This is the script I am using the generate the mesh:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeshGenerator : MonoBehaviour
{
Mesh mesh;
Vector3[] vertices;
int[] triangles;
public Mesh GenerateMesh(int mapWidth, int mapHeight)
{
mesh = new Mesh();
vertices = new Vector3[(mapHeight + 1) * (mapWidth + 1)];
triangles = new int[6 * (mapHeight * mapWidth)];
int index = 0;
for (int x = 0; x <= mapHeight; x++)
{
for (int y = 0; y <= mapWidth; y++)
{
vertices[index] = new Vector3(x, 0, y);
index += 1;
}
}
int z = 0;
int a = 0;
for (int i = 0; i < triangles.Length; i += 6)
{
if (z == mapWidth)
{
triangles *= a + 1;*
triangles[i + 1] = a + 2;
triangles[i + 2] = a + mapWidth + 3;
triangles[i + 3] = a + 1;
triangles[i + 4] = a + mapWidth + 3;
triangles[i + 5] = a + mapWidth + 2;
z = 1;
a += 2;
}
else
{
triangles = a;
triangles[i + 1] = a + 1;
triangles[i + 2] = a + mapWidth + 2;
triangles[i + 3] = a;
triangles[i + 4] = a + mapWidth + 2;
triangles[i + 5] = a + mapWidth + 1;
z++;
a++;
}
}
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
return mesh;
}
}
Both of these scripts are called by this one:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CreateMesh : MonoBehaviour
{
Texture2D texture2D;
public int mapWidth;
public int mapHeight;
public int textureResX;
public int textureResY;
public Vector2 textureOrigins;
public float textureScale;
public Material material;
void Start()
{
GameObject terrainMesh = new GameObject();
terrainMesh.transform.position = new Vector3(0, 0, 0);
terrainMesh.AddComponent();
terrainMesh.GetComponent().sharedMaterial = material;
terrainMesh.AddComponent();
MeshFilter meshFilter = terrainMesh.GetComponent();
MeshGenerator meshGenerator = FindObjectOfType();
Mesh mesh = meshGenerator.GenerateMesh(mapWidth, mapHeight);
meshFilter.mesh = mesh;
NoiseTextureGenerator noiseTextureGenerator = FindObjectOfType();
texture2D = noiseTextureGenerator.GenerateNoise(textureResX, textureResY, textureOrigins.x, textureOrigins.y, textureScale);
terrainMesh.GetComponent().sharedMaterial.mainTexture = texture2D;
}
}
For my material variable on the previous script, I am using a simple unlit texture. When I generate the mesh, it just appears grey, but if I open the material instance on the mesh renderer, the texture that it shows looks like perlin noise. So I guess my question is: Why isn’t the texture being applied to the mesh when it is applied to the material instance?
If you look at the picture below, you’ll see what I mean about the texture on the mesh not matching the texture on the material.
[182017-unity-screenshot.png|182017]_
_