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 09, 2015 at 01:05 PM · meshmaterialsmeshrenderermeshfiltersubmesh

Submeshs doesn't combine with materials

Simple as that, submeshs doesn't combine with materials and i get an error: Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 18, VertexCount: 4.

The count of submesh is same count as material, i think the error is coming from this: All submeshs aren't assing yet with an material. How can i fix it? Don't give me all the solution, i hate that...

Here's the script!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 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;

 public Material[] mats;

 List<Vector3> vertices;
 List<int> triangles;
 List<Vector2> uvs;

 MeshFilter mf;
 Mesh mesh;
 MeshRenderer mr;

 int[,] ColorIdInt;
 int ColorInt;

 //15w by 10h pixelscreen

 void ResetScreen() {
     vertices = new List<Vector3>();
     triangles = new List<int>();
     uvs = new List<Vector2>();

     ColorIdInt = new int[15,10];
     mf = ScreenObject.GetComponent<MeshFilter>();
     mesh = new Mesh();
     mr = ScreenObject.GetComponent<MeshRenderer>();
     mf.mesh = mesh;
     mesh.subMeshCount = 8;

 }

 void SetPixelUpdate(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] = vertices.Count + 0;
     tri[1] = vertices.Count + 2;
     tri[2] = vertices.Count + 1;

     tri[3] = vertices.Count + 2;
     tri[4] = vertices.Count + 3;
     tri[5] = vertices.Count + 1;

     foreach(Vector3 V3 in verticles) {
         vertices.Add(V3);
     }
     foreach(int TS in tri) {
         triangles.Add(TS);
     }

     mesh.vertices = vertices.ToArray();
     mesh.triangles = triangles.ToArray();

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

     ColorInt = 0;

     switch(ColorId) {
         case "Red":
             ColorInt = 0;
         ColorIdInt[SaveX,SaveY] = 1;
             break;
         case "Green":
             ColorInt = 1;
         ColorIdInt[SaveX,SaveY] = 2;
             break;
         case "Blue":
             ColorInt = 2;
         ColorIdInt[SaveX,SaveY] = 3;
             break;
         case "Black":
             ColorInt = 3;
         ColorIdInt[SaveX,SaveY] = 4;
             break;
         case "White":
             ColorInt = 4;
         ColorIdInt[SaveX,SaveY] = 5;
             break;
         case "Yellow":
             ColorInt = 5;
         ColorIdInt[SaveX,SaveY] = 6;
             break;
         case "Orange":
             ColorInt = 6;
         ColorIdInt[SaveX,SaveY] = 7;
             break;
         case "Magenta":
             ColorInt = 7;
         ColorIdInt[SaveX,SaveY] = 8;
             break;
         default:
             ColorInt = 0;
         ColorIdInt[SaveX,SaveY] = 1;
             break;
     }
     mesh.subMeshCount = 8;
     mf.mesh.SetTriangles(triangles, ColorInt); //ColorInt > submesh
     mr.materials = mats;
 }

 void Fill (int X1, int Y1, int X2, int Y2, string ColorId) {
     for(int x = 0; x < X2; x++) {
         for(int y = 0; y < Y2; y++) {
             SetPixelUpdate(x + X1, y + Y1, ColorId);
         }
     }
 }

 int GetColorOnPixel (int X, int Y) {
     if(ColorIdInt[X,Y] != null) {
         return ColorIdInt[X,Y];
     } else {
         return 0;
     }
 }

 void RemovePixel (int X, int Y) {
     //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)
     vertices.Remove(new Vector3(X + 0, Y + 0, -0.01f));
     vertices.Remove(new Vector3(X + 0.1f, Y + 0, -0.01f));
     vertices.Remove(new Vector3(X + 0, Y + 0.1f, -0.01f));
     vertices.Remove(new Vector3(X + 0.1f, Y + 0.1f, -0.01f));

     mesh.vertices = vertices.ToArray();

     ColorIdInt[X,Y] = 0;
 }

 // Use this for initialization
 void Start () {
     ResetScreen();
     SetPixelUpdate(1,1, "Red");
     SetPixelUpdate(1,2, "Blue");
     SetPixelUpdate(1,3, "Green");

 }
 
 // Update is called once per frame
 void Update () {

 }
 }

I hope you understand; "bad english".

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tomhog · Dec 10, 2015 at 11:54 PM

Hi

I've literally no idea what you're trying to achieve but here's working version

     void SetPixelUpdate(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] = vertices.Count + 0;
         tri[1] = vertices.Count + 2;
         tri[2] = vertices.Count + 1;
         tri[3] = vertices.Count + 2;
         tri[4] = vertices.Count + 3;
         tri[5] = vertices.Count + 1;
         foreach(Vector3 V3 in verticles) {
             vertices.Add(V3);
         }
         foreach(int TS in tri) {
             triangles.Add(TS);
         }
         mesh.vertices = vertices.ToArray();
         //mesh.triangles = triangles.ToArray();
         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();
         ColorInt = 0;
         switch(ColorId) {
         case "Red":
             ColorInt = 0;
             ColorIdInt[SaveX,SaveY] = 1;
             break;
         case "Green":
             ColorInt = 1;
             ColorIdInt[SaveX,SaveY] = 2;
             break;
         case "Blue":
             ColorInt = 2;
             ColorIdInt[SaveX,SaveY] = 3;
             break;
         case "Black":
             ColorInt = 3;
             ColorIdInt[SaveX,SaveY] = 4;
             break;
         case "White":
             ColorInt = 4;
             ColorIdInt[SaveX,SaveY] = 5;
             break;
         case "Yellow":
             ColorInt = 5;
             ColorIdInt[SaveX,SaveY] = 6;
             break;
         case "Orange":
             ColorInt = 6;
             ColorIdInt[SaveX,SaveY] = 7;
             break;
         case "Magenta":
             ColorInt = 7;
             ColorIdInt[SaveX,SaveY] = 8;
             break;
         default:
             ColorInt = 0;
             ColorIdInt[SaveX,SaveY] = 1;
             break;
         }
         mesh.subMeshCount = 8;
         mesh.SetTriangles(triangles, ColorInt); //ColorInt > submesh
         mr.materials = mats;
     }

So you had two problems, 1. you were calling mesh.triangles = triangles.ToArray(); which kept destroying previous SetTriangles calls, 2. At the end you call mf.mesh.SetTriangles(triangles, ColorInt); //ColorInt > submesh, were as in the rest of the code you just use mesh. You need to read up on what happens when you set a mesh to a mesh filter and the difference between mf.mesh and mf.sharedMesh

MeshFilter Doc

Hope that helps Tom

Comment
Add comment · Show 4 · Share
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 StuwuStudio · Dec 11, 2015 at 12:28 AM 0
Share

Still not working but i will try to better explain.

SetPixelUpdate(X, Y, ColorId);

This function is set a new pixel without delete all the other pixel which is save in the 'triangles' and 'vertices' list. X and Y variable is used to set the position of the new pixel. ColorId string is used for say in which submesh the new pixel will be. For exemple; "Red" is assing the pixel to the submesh with the red materials. I don't want to replace any submesh. - Studio
avatar image tomhog StuwuStudio · Dec 11, 2015 at 12:37 AM 0
Share

The code I gave compiles and runs with no errors, wether or not that solves your actual algorithm problem is a different question entirely.

I would simplify what you're doing, have separate meshes until you get it working, then try to combine into submeshes.

avatar image StuwuStudio tomhog · Dec 11, 2015 at 12:59 PM 0
Share

How can fix it? There's nothing on the web... I will post a forum for more help.

Show more comments

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

34 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

Related Questions

Why zero height mesh gets black independent of shader 1 Answer

Bounds of dynamic mesh not updating 1 Answer

Artifacts / Stretch Marks in Mesh Manipulation 0 Answers

Procedurally generated mesh not Rendering all triangles 1 Answer

Unity5 Procedural meshing slower than in Unity4? 1 Answer


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