有一个问题,一个脚本,其中一个简单的景观是从三角形生成的。并且草的纹理是通过脚本叠加的。但是在应用纹理之后,网格变成了绿色,好像从纹理中取出了 1 个像素。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
public class ProceduralTerrain : MonoBehaviour
{
Mesh mesh;
Vector3[] vertices;
int[] triangles;
public Renderer rend;
public Texture[] textures;
public int xSize = 100;
public int zSize = 100;
private void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
rend = GetComponent<Renderer>();
rend.material.mainTexture = textures[0];
CreateShape();
}
private void Update()
{
UpdateMesh();
}
void CreateShape()
{
vertices = new Vector3[(xSize + 1) * (zSize + 1)]; //Getting size of grid
for(int i = 0, z = 0; z <= zSize; z++)
{
for (int x = 0; x <= xSize; x++)
{
//Setting PerlinNoise and calculating grid
float y = Mathf.PerlinNoise(x * .01f, z * .01f) * 50f;
vertices[i] = new Vector3(x, y, z);
i++;
}
}
triangles = new int[xSize * zSize * 6];
//setting triangles and verts for simple quad
int vert = 0;
int tris = 0;
for (int z = 0; z < xSize; z++)
{
for (int x = 0; x < xSize; x++)
{
triangles[tris + 0] = vert + 0;
triangles[tris + 1] = vert + xSize + 1;
triangles[tris + 2] = vert + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 4] = vert + xSize + 1;
triangles[tris + 5] = vert + xSize + 2;
vert++;
tris += 6;
}
vert++;
}
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
}
谢谢你。
您尚未设置纹理坐标以将纹理映射到模型。