Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Rockroot · Jun 14, 2013 at 07:01 PM · c#meshinvisible

Why is my script-crafted mesh invisible in game-mode? (C#)

Hey guys,

I tried to create a flat mesh on my own, just like the terrain, but so i can change its vertices height in each Update()-Call (to create something wave-like).

I'm pretty sure I created the rectangles right (there are no errors anymore) and even the pivot in Scene-View moves when I hit start.

But the mesh turns out to be invisible, even in "Wireframe"-Mode, from all directions (so i guess its not the normals). Also i got a material 'Diffuse' with a Texture on it.

I have the feeling I'm missing something simple, any suggestions?

Here's the code, still in Start(), because I don't need to transform it yet. I just want it visible. Possibly the uv's?

 public class Waves : MonoBehaviour {
     
 
     private int width          = 4;
     private int maxRows        = 1; //Minimum 1
     private float distance     = 2f;
     
     public bool showInformation = true;
     public bool showDetailed    = true;
     
     void Start() {
         
         if(maxRows < 1)
             maxRows = 1;
         
         //Building Vertex-Grid
         List<Vector3> vertices = new List<Vector3>();
         
         for (int row = 0; row <= maxRows; row++){
             if(row%2 == 0){
                 for (int i = 1; i <= width    ; i++    ){
                     vertices.Add(new Vector3(i*distance, 0f, row* distance));
                 }
             } else {
                 for (int i = 1; i <= width    ; i++    ){
                     vertices.Add(new Vector3(i*distance + distance*0.5f, 0f, row* distance));
                 }
             }
         }
         //Convert List<Vector3> to Vector3[]
         Vector3 [] allVertices = new Vector3[vertices.Count];
         
         for (int i = 0; i < vertices.Count; i++) {
             allVertices[i] = vertices[i];
             if(showDetailed)
                 Debug.Log(allVertices[i]);
         }
         
         if(showInformation)
             Debug.Log ("There are " + allVertices.Length + " vertices known");
         
         
         
         
         //Building Triangles
         List<int> triangles = new List<int>();
         
         for (int row = 0; row < maxRows; row++){
             
             //Green Part
             for (int i = 1; i < width    ; i++    ){
                 int a = computeGreenA(row,width,i)-1;
                 int b = computeGreenB(row,width,a)-1;
                 int c = computeGreenC(row,width,a)-1;
                 triangles.Add (a);
                 triangles.Add (b);
                 triangles.Add (c);
             }
             
             //Orange Part
             for (int i = 1; i < width    ; i++    ){
                 int a = computeOrangeA(row,width,i)-1;
                 int b = computeOrangeB(row,width,a)-1;
                 int c = computeOrangeC(row,width,a)-1;
                 triangles.Add (a);
                 triangles.Add (b);
                 triangles.Add (c);
             }
             //The "-1" exists, because the Array goes from, say, 0-7, while the saved Vertices are numbered 1-8!
         if(showInformation)    
             Debug.Log ("We are at row " + row + " and currently got " + triangles.Count / 3 + " Triangles");
         }
         
         
         //Convert List<int> to int[]
         int[] allTriangles = new int[triangles.Count];
         for (int i = 0; i < triangles.Count; i++) {
             allTriangles[i] = triangles[i];
             if(showDetailed)
                 Debug.Log(allTriangles[i]);
         }
         if(showInformation)
             Debug.Log("allTriangles[] holds " + allTriangles.Length + " entries");
         
         
         //Create UV
         Vector2[] uv = new Vector2[allVertices.Length];
         
         for(int i = 0; i < uv.Length; i++) {
             uv[i] = new Vector2 (allVertices[i].x, allVertices[i].z);
             if(showDetailed)
                 Debug.Log(uv[i]);
         }
         
         
         //Create Mesh
         Mesh mesh = new Mesh();
         
         //mesh.MarkDynamic();
         mesh.vertices     = allVertices;
         mesh.triangles     = allTriangles;
         mesh.uv         = uv;
         mesh.RecalculateNormals();
         mesh.RecalculateBounds();
         
         GetComponent<MeshFilter>().mesh = mesh;    
     }
     
     
     //Green Computations
     int computeGreenA(int row,int width,int i){
         return row*width+i;    
     }
     
     int computeGreenB(int row, int width, int a){
         return a+1;
     }
     
     int computeGreenC(int row, int width, int a){
         if(row%2 == 0){
             return a+width;    
         } else {
             return a+width+1;    
         }
     }
     //Orange Computations
     int computeOrangeA(int row,int width,int i){
         return (row+1)*width+i;    
     }
     
     int computeOrangeB(int row, int width, int a){
         if(row%2 == 0){
             return a-width+1;    
         } else {
             return a-width;    
         }
     }
     
     int computeOrangeC(int row, int width, int a){
         return a+1;
     }
 }

I also uploaded the concept for creation of the triangles: Triangles in a field of 7 vertices wide and 4 deep: http://imageshack.us/a/img40/267/wk61.jpg Triangles in a field of 4 vertices wide and 5 deep: http://imageshack.us/a/img62/6407/zvwv.jpg The formulas i got from my 'research': http://imageshack.us/a/img96/1368/82sd.jpg

Comment
Add comment · Show 1
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 Rockroot · Jun 14, 2013 at 08:34 PM 0
Share

You'll need to put

using UnityEngine; using System.Collections; using System.Collections.Generic;

in front of the script to make it work. (sorry)

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Rockroot · Jun 17, 2013 at 01:15 PM

Okay, remember when I said "I'm pretty sure I calculated the triangles right"? I didn't.

The devil lies in this comment: //The "-1" exists, because the Array goes from, say, 0-7, while the saved Vertices are numbered 1-8!

The thing is, that I did -1 on a (line 52 and 62), use a, but then do -1 twice again on b and c (which are already reduced, when I reduced a)

So for people who come here with the same problem: Check your triangles again, and again, and again.

Sorry for taking your time.

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

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

15 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

Related Questions

Why my Rock Meshes are invisible in the scene and visible in the game? 3 Answers

Mesh UVs won't change positions 1 Answer

invisible mesh in game mode 1 Answer

Adding lights and cameras to scenes with cSharp script 2 Answers

Error trying to createMonoBehaviour using the 'new' keyword in cSharp script 4 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