- Home /
Create multiple meshs in one mesh
Hi, I tried to create a "Screen" using Mesh and MeshFillter but when i create a new Mesh the last one is remove, i am very bad for explain, here is the code...
using UnityEngine; using System.Collections;
public class ScreenBoard : MonoBehaviour {
public Transform ScreenObject;
public Texture2D PixelsRed;
public Texture2D PixelsBlue;
public Texture2D PixelsGreen;
public Texture2D PixelsBlack;
public Texture2D PixelsWhite;
public Texture2D PixelsYellow;
public Texture2D PixelsOrange;
public Texture2D PixelsMangenta;
MeshFilter mf;
Mesh mesh;
MeshRenderer mr;
int[,] ColorIdInt;
//15w by 10h pixelscreen
void ResetScreen() {
ColorIdInt = new int[15,10];
mf = ScreenObject.GetComponent<MeshFilter>();
mesh = new Mesh();
mr = ScreenObject.GetComponent<MeshRenderer>();
mf.mesh = mesh;
}
void GetUpdate(float X, float Y, string ColorId) {
int SaveX = Mathf.RoundToInt(X);
int SaveY = Mathf.RoundToInt(Y);
X = Mathf.RoundToInt(X);
Y = Mathf.RoundToInt(Y);
X = X * 0.1f;
Y = Y * 0.1f;
Vector3[] verticles = new Vector3[4] {
new Vector3(X + 0, Y + 0, -0.01f), new Vector3( X + 0.1f, Y + 0, -0.01f), new Vector3(X + 0, Y + 0.1f, -0.01f), new Vector3(X + 0.1f, Y + 0.1f, -0.01f)
};
int[] tri = new int[6];
tri[0] = 0;
tri[1] = 2;
tri[2] = 1;
tri[3] = 2;
tri[4] = 3;
tri[5] = 1;
mesh.vertices = verticles;
mesh.triangles = tri;
if(ColorId == "Red") {
mr.material.mainTexture = PixelsRed;
ColorIdInt[SaveX,SaveY] = 1;
}
if(ColorId == "Green") {
mr.material.mainTexture = PixelsGreen;
ColorIdInt[SaveX,SaveY] = 2;
}
if(ColorId == "Blue") {
mr.material.mainTexture = PixelsBlue;
ColorIdInt[SaveX,SaveY] = 3;
}
if(ColorId == "Black") {
mr.material.mainTexture = PixelsBlack;
ColorIdInt[SaveX,SaveY] = 4;
}
if(ColorId == "White") {
mr.material.mainTexture = PixelsWhite;
ColorIdInt[SaveX,SaveY] = 5;
}
if(ColorId == "Yellow") {
mr.material.mainTexture = PixelsYellow;
ColorIdInt[SaveX,SaveY] = 6;
}
if(ColorId == "Orange") {
mr.material.mainTexture = PixelsOrange;
ColorIdInt[SaveX,SaveY] = 7;
}
if(ColorId == "Magenta") {
mr.material.mainTexture = PixelsMangenta;
ColorIdInt[SaveX,SaveY] = 8;
}
}
// Use this for initialization
void Start () {
ResetScreen();
GetUpdate(1, 0, "Red");
GetUpdate(2, 0, "Green");
}
// Update is called once per frame
void Update () {
}
}
First, i reset mesh of the screen, after, i execute the GetUpdate function. There, i get position of vertices and triangle, and, i assing them on my mesh (my problem: the new mesh replace the old one). After, i add the material. I hope you understand... sorry for bad english
Of course the mesh gets replaced, that what you tell thegame to do, after all, with these lines:
mesh.vertices = verticles;
mesh.triangles = tri;
What you can do is to convert toe mesh.vertices and mesh.triangles into a temporary list, then add the new vertices and triangles to it, and then assign those list back to the mesh via .ToArray().
Thanks! It's work perfectly using list! But i have one problem; how to set a specific material for each "Pixel"? i know i need to use UVs and all that stuff? Do you have any pathway or hint?
(I'm a bite late, i'm busy)
Each element in the UV array is a Vector2 which has the x,y coordinates of the corresponding vertex that has the same index in the vertices array as the Vector2 in the UV array. The x and y values go from 0 to 1 (values above that just repeat). They define a point on the texture image the material uses, typcially as the Diffuse/Albedo texture. For more information about UV mapping, you should read the entry in the User $$anonymous$$anual and Scripting API.
To use multiple materials you would typically use a mesh with submeshes and a renderer with multiple materials. RIght now you are using mesh.triangles and renderer.material. This is equivilant to using the first submesh, mesh.GetTriangles(0) and the first material in the materials, renderer.materials[0], So to use a second submesh you would set the number of submeshes, use mesh.SetTriangles(int) to set the submesh, and set rendererer,materials to have more elements. i.e.
var filter = GetComponent<$$anonymous$$eshFilter>();
var mesh = new $$anonymous$$esh();
mesh.sub$$anonymous$$eshCount = 2;
mesh.vertices = vertices;
mesh.SetTriangles(triangles1, 0);
mesh.SetTriangles(triangles2, 1);
filter.mesh = mesh;
var renderer = GetComponent<$$anonymous$$eshRenderer>();
renderer.materials = new $$anonymous$$aterial[2]{material1, material2};
http://docs.unity3d.com/ScriptReference/$$anonymous$$esh-sub$$anonymous$$eshCount.html
http://docs.unity3d.com/ScriptReference/$$anonymous$$esh.SetTriangles.html
http://docs.unity3d.com/ScriptReference/Renderer-materials.html
http://docs.unity3d.com/ScriptReference/$$anonymous$$esh.html
Uh... I understand uvs but there my (last) problem, after assing UVs and "divise" triangle of the mesh, i don't know how to assing the material to the triangles. Here's my script and i hope you help me to fix the last problem :) I put just the render/mesh thing of the script
public $$anonymous$$aterial[] mats;
List<Vector2> uvs;
$$anonymous$$eshFilter mf;
$$anonymous$$esh mesh;
$$anonymous$$eshRenderer mr;
...
Vector2[] uv = new Vector2[4];
uv[0] = new Vector2(0,0);
uv[1] = new Vector2(1,0);
uv[2] = new Vector2(0,1);
uv[3] = new Vector2(1,1);
foreach(Vector2 SingleUV in uv) {
uvs.Add(SingleUV);
}
mesh.uv = uvs.ToArray();
mesh.RecalculateNormals();
mesh.sub$$anonymous$$eshCount = (triangles.Count / 6);
mf.mesh.SetTriangles(new int[] {
vertices.Count + 0,
vertices.Count + 2,
vertices.Count + 1,
vertices.Count + 2,
vertices.Count + 3,
vertices.Count + 1,
}, triangles.Count / 6));
mr.material = mats;
The script I attached should be a good start.
Submeshes are different sets of triangles for one mesh, you were actually using the first submesh when you did mesh.triangles=triangles. The exact same thing should happen if you did mesh.SetTriangles(triangles, 0).
If you change sub$$anonymous$$eshCount to 2 then you can define the second submesh (i.e. mesh.SetTriangles(triangles2, 1) which means you'd have two submeshes. If your renderer has 2 materials then the first submesh will automatically use the first material and the second submesh will use the second material.
ANOTHER PROBLE$$anonymous$$ WHICH IS NOT ON WEB: Failed setting triangles. Some indices are referencing out of bounds vertices.
Great! But, can and how i edit sub-mesh? Did i need to use (Again :P) a list? I currently working on.
Your answer
![](https://koobas.hobune.stream/wayback/20220612051520im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to solve a Unity fatal error? 1 Answer
Mesh memory leak error 0 Answers
Mesh wont render/ appears invisible 4 Answers
Replace MeshFilter mesh by a other mesh in Editor 1 Answer
Apply new complex fbx to saved prefab 0 Answers