【问题标题】:Unity/C#: procedural texture for procedural meshUnity/C#:程序网格的程序纹理
【发布时间】:2016-06-30 09:59:58
【问题描述】:

我想在 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;
    }
}

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

非常感谢您的帮助;-)

【问题讨论】:

    标签: unity3d procedural-generation


    【解决方案1】:

    我认为您缺少设置 UV。检查这个:Unity3D answers link

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

    并像这样设置它:mesh.uv = uvs;

    【讨论】:

    • 谢谢!我真的忘记了紫外线,抱歉这个相当愚蠢的问题。如果有人遇到这个问题,请添加以下行:mesh.uv = new Vector2[4] { Vector2.zero, new Vector2 (1, 0), new Vector2 (1, 1), new Vector2 (0, 1) };使这个基本示例工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多