Unity / C#:程序网格的程序纹理

时间:2016-06-30 09:59:59

标签: unity3d procedural-generation

我想在Unity / C#中生成网格和纹理程序。但是,我使用自定义网格的所有尝试仅显示第一个纹素。我试图尽可能简化问题并提出以下示例代码,它绘制一个白色方块而不是“波兰标志”。

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

    [SerializeField] Material unlitMaterial;

    void Start () {
        Mesh mesh = new Mesh();
        mesh.vertices = new Vector3[4] { Vector3.zero, new Vector3 (1, 0, 0), new Vector3 (1, 0, 1), new Vector3 (0, 0, 1) };
        mesh.triangles = new int[6] {1, 0, 3, 2, 1, 3 } ; 
        mesh.RecalculateNormals();

        Texture2D texture = new Texture2D (2, 2);
        texture.SetPixels (new Color[4] {Color.white, Color.white, Color.red, Color.red});
        texture.filterMode = FilterMode.Point;
        texture.Apply();

        GameObject gameObject = new GameObject ();
        MeshRenderer meshRenderer = gameObject.AddComponent<MeshRenderer> ();
        if (unlitMaterial != null)
            meshRenderer.material = unlitMaterial;
        meshRenderer.material.mainTexture = texture;
        gameObject.AddComponent<MeshFilter> ().mesh = mesh;
    }
}

(我尝试了不同的材料,如眩光/纹理,没有效果)

非常感谢您的帮助; - )

1 个答案:

答案 0 :(得分:0)

我认为你缺少设置UV。检查一下:Unity3D answers link

  

因为他只有4个顶点,只需将它们设为(0,0)(0,1)(1,1)和(1,0):uvs [0] = new Vector2(0,0); ....

并将其设置为:mesh.uv = uvs;