Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by StuwuStudio · Dec 05, 2015 at 10:52 PM · errormeshmaterialmeshfilterreplace

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

Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · Dec 06, 2015 at 01:06 PM 0
Share

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().

avatar image StuwuStudio · Dec 06, 2015 at 08:03 PM 0
Share

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)

avatar image Cherno StuwuStudio · Dec 07, 2015 at 08:17 PM 0
Share

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.

avatar image maccabbe StuwuStudio · Dec 07, 2015 at 10:22 PM 0
Share

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

avatar image StuwuStudio · Dec 07, 2015 at 10:47 PM 0
Share

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;
avatar image StuwuStudio · Dec 08, 2015 at 01:00 AM 0
Share

I check on web and i found sub mesh. How to use it?

avatar image maccabbe StuwuStudio · Dec 08, 2015 at 03:09 AM 0
Share

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.

avatar image StuwuStudio maccabbe · Dec 08, 2015 at 10:39 PM 0
Share

ANOTHER PROBLE$$anonymous$$ WHICH IS NOT ON WEB: Failed setting triangles. Some indices are referencing out of bounds vertices.

avatar image StuwuStudio · Dec 08, 2015 at 12:43 PM 0
Share

Great! But, can and how i edit sub-mesh? Did i need to use (Again :P) a list? I currently working on.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

37 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges