Having trouble with a texture not showing on a generated mesh.
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[i] = 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[i] = 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<MeshRenderer>();
terrainMesh.GetComponent<MeshRenderer>().sharedMaterial = material;
terrainMesh.AddComponent<MeshFilter>();
MeshFilter meshFilter = terrainMesh.GetComponent<MeshFilter>();
MeshGenerator meshGenerator = FindObjectOfType<MeshGenerator>();
Mesh mesh = meshGenerator.GenerateMesh(mapWidth, mapHeight);
meshFilter.mesh = mesh;
NoiseTextureGenerator noiseTextureGenerator = FindObjectOfType<NoiseTextureGenerator>();
texture2D = noiseTextureGenerator.GenerateNoise(textureResX, textureResY, textureOrigins.x, textureOrigins.y, textureScale);
terrainMesh.GetComponent<MeshRenderer>().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.
I've tested applying the texture to a cube, after it is generated, and the cube renders the texture fine. There must be something that I am doing wrong when I generate the mesh, but I can't think of what it could be. It's not the normals, because I am recalculating them after generating the mesh, and the mesh looks fine when lit. Maybe I'm creating overlapping triangles, but I don't think so, as that would cause visible graphical glitches on the mesh in general. If someone could look over my mesh generation for flaws, that would be a welcome gesture, because as far as I know, the mesh generation should work fine.
I also just tested changing the offset of the texture at runtime, and the value of the color changes, almost as if it is rendering only a single pixel of the texture. I tried changing the texture scale in code, but it made no change at all.
Answer by Phoenix2233 · Jun 12, 2021 at 06:29 PM
Turns out I just needed to generate the uvs for my mesh.
Your answer
Follow this Question
Related Questions
Imported meshes colors changed to blue. How do I fix them? 2 Answers
2 Models 1 Cracked How to make both look the same? (Material Problems) 1 Answer
Why does my model not display proper material? 2 Answers
Automatically assign hundred of textures to .obj file 0 Answers
How do you draw a sprite from an atlas on to a mesh? 1 Answer