我可以在发送到网格过滤器之前将网格添加到一起c#

时间:2014-09-21 22:58:35

标签: filter unity3d mesh

这个问题是团结中的一般性问题c# 我知道我不能有多个网格过滤器 但我在脚本中生成了更多的那个网格,我想让它们在一个对象中一次渲染,但我不想合并顶点数组,所以我仍然可以动态删除面孔

基本思想是将体素作为一个游戏对象,但是由脚本构成不同的面孔 我想保持面部可访问而不进入一个巨大的阵列

所以现在我有6个面孔作为网格,我不知道如何渲染它们

最好的方法是什么,/最佳方式去/ / noob

1 个答案:

答案 0 :(得分:0)

所以我有一些答案,但仍需要有关概念方法的指导 所以我重写了我的“体素”,但现在我正在考虑编写一系列功能来“破坏”面部,如果他们不需要,但问题是,如果我把我的三角形(我认为这是最简单的)全部为0 0 0,它不像释放任何内存,它只是不可见,所以它不是好的方式去

怎么会有人去做某种优化的体素?

我想从我的代码中你知道我是初学者

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
public class Voxel : MonoBehaviour {

public Vector3 position ;

const float V = 0.5f;
Vector3[] vertices ;
int[] triangles;
Vector2[] uv ;
Mesh mesh ;

void Start () {
    vertices = new Vector3[24];
    triangles = new int[36];
    uv = new Vector2[24];
    mesh = new Mesh();

    Front ();
    Right ();
    Back ();
    Left ();
    Top ();
    Bottom ();

    renderMesh ();
}
public void renderMesh(){
    mesh.Clear();
    mesh.vertices = vertices;
    mesh.triangles = triangles;
    mesh.uv = uv;
    mesh.Optimize();
    mesh.RecalculateNormals();

    MeshFilter mesh_filter = GetComponent<MeshFilter>();
    MeshCollider mesh_collider = GetComponent<MeshCollider>();
    mesh_filter.mesh = mesh;
    mesh_collider.sharedMesh = mesh;
}

public void Front(){
    vertices[0] = new Vector3(-V,V,-V);//fronttopleft
    vertices[1] = new Vector3(V,V,-V);//fronttopright
    vertices[2] = new Vector3(-V,-V,-V);//frontbottomleft
    vertices[3] = new Vector3(V,-V,-V);//frontbottomright
    triangles[0] = 0;//front polys
    triangles[1] = 1;
    triangles[2] = 3;
    triangles[3] = 0;
    triangles[4] = 3;
    triangles[5] = 2;
    uv[0] = new Vector2(0,1); 
    uv[1] = new Vector2(1,1);
    uv[2] = new Vector2(0,0);
    uv[3] = new Vector2(1,0);
}
public void Right(){
    vertices[4] = new Vector3(V,V,-V);
    vertices[5] = new Vector3(V,V,V);
    vertices[6] = new Vector3(V,-V,-V);
    vertices[7] = new Vector3(V,-V,V);
    triangles[6] = 4;
    triangles[7] = 5;
    triangles[8] = 7;
    triangles[9] = 4;
    triangles[10] = 7;
    triangles[11] = 6;
    uv[4] = new Vector2(0,1); 
    uv[5] = new Vector2(1,1);
    uv[6] = new Vector2(0,0);
    uv[7] = new Vector2(1,0);
}
public void Back(){
    vertices[8] = new Vector3(V,V,V);
    vertices[9] = new Vector3(-V,V,V);
    vertices[10] = new Vector3(V,-V,V);
    vertices[11] = new Vector3(-V,-V,V);
    triangles[12] = 8;
    triangles[13] = 9;
    triangles[14] = 11;
    triangles[15] = 8;
    triangles[16] = 11;
    triangles[17] = 10;
    uv[8] = new Vector2(0,1); 
    uv[9] = new Vector2(1,1);
    uv[10] = new Vector2(0,0);
    uv[11] = new Vector2(1,0);
}
public void Left(){
    vertices[12] = new Vector3(-V,V,V);
    vertices[13] = new Vector3(-V,V,-V);
    vertices[14] = new Vector3(-V,-V,V);
    vertices[15] = new Vector3(-V,-V,-V);
    triangles[18] = 12;
    triangles[19] = 13;
    triangles[20] = 15;
    triangles[21] = 12;
    triangles[22] = 15;
    triangles[23] = 14;
    uv[12] = new Vector2(0,1); 
    uv[13] = new Vector2(1,1);
    uv[14] = new Vector2(0,0);
    uv[15] = new Vector2(1,0);
}
public void Top(){
    vertices[16] = new Vector3(-V,V,V);
    vertices[17] = new Vector3(V,V,V);
    vertices[18] = new Vector3(-V,V,-V);
    vertices[19] = new Vector3(V,V,-V);
    triangles[24] = 16;
    triangles[25] = 17;
    triangles[26] = 19;
    triangles[27] = 16;
    triangles[28] = 19;
    triangles[29] = 18;
    uv[16] = new Vector2(0,1); 
    uv[17] = new Vector2(1,1);
    uv[18] = new Vector2(0,0);
    uv[19] = new Vector2(1,0);
}
public void Bottom(){
    vertices[20] = new Vector3(V,-V,V);
    vertices[21] = new Vector3(-V,-V,V);
    vertices[22] = new Vector3(V,-V,-V);
    vertices[23] = new Vector3(-V,-V,-V);
    triangles[30] = 20;
    triangles[31] = 21;
    triangles[32] = 23;
    triangles[33] = 20;
    triangles[34] = 23;
    triangles[35] = 22;
    uv[20] = new Vector2(0,1); 
    uv[21] = new Vector2(1,1);
    uv[22] = new Vector2(0,0);
    uv[23] = new Vector2(1,0);
}

}