• 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
Question by ALLCAPS · Oct 06, 2013 at 11:25 AM · c#mesh

Generate mesh from faces, invisible

I am writing a set of classes to read a Quake3 .bsp map into usable C# objects. My first goal is to get the geometry visible. I have already parsed much of the .bsp, all of the data that pertains to faces is available to me. Faces in a Quake 3 map are defined as a face with vertexes, each vertex has a normal, and each face has texture coords, along with other things. These faces are arranged into models, with all of the faces that make up the world geometry in one model. I'm not sure how to composite these faces into a single model, I'm trying to render them on their own just to see something on the screen.

Currently I'm trying to create gameobjects that will render the map for me, and I'm not sure how would be best to do this.

I tried to make one gameobject per face (inefficient, I'm sure) just to get something on the screen and confirm that my data was at least good, but no matter what I do the mesh is not visible. I have set the normals array, uv array, vertex, and triangles up, and I did them in the order recommended in the Mesh script reference. I have logs of the data from the .bsp, and it seems to be good. The vertexes/face data all looks sensible. The classes that pull this data from the .bsp are pretty large, but the problem is likely how I'm constructing the object.

I have a very strong feeling that making one object/mesh perfect is causing my problem, but I'd like to ask the community for a little guidance before I chase a wild goose guessing at a better way. And before somebody asks why I'm parsing Quake3 .bsp, the answer is just for fun. I'm not asking for free code, just some suggestions or pointers as to how to jump this last hurdle.

 GameObject GenerateModel(Face face)
     {
         GameObject faceObject = new GameObject("BSPface");
         Mesh worldFace = new Mesh();
         worldFace.name = "BSPfacemesh";
 
         // Rip verts, uvs, and normals
         List<Vector3> verts = new List<Vector3>();
         List<Vector2> uvs = new List<Vector2>();
         List<Vector3> normals = new List<Vector3>();
         int vstep = face.vertex;
         for (int i = 0; i < face.n_vertexes; i++)
         {
             verts.Add(map.vertexLump.verts[vstep].position);
             uvs.Add(map.vertexLump.verts[vstep].texcoord);
             normals.Add(map.vertexLump.verts[vstep].normal);
             vstep++;
         }
 
         // add the verts, uvs, and normals we ripped to the gameobjects mesh filter
         worldFace.vertices = verts.ToArray();
         worldFace.normals = normals.ToArray();
 
         // I honestly don't know what these three uv arrays are for, so just put the same data in all of them
         worldFace.uv = uvs.ToArray();
         worldFace.uv1 = uvs.ToArray();
         worldFace.uv2 = uvs.ToArray();
 
         // Rip meshverts / triangles
         List<int> mverts = new List<int>();
         int mstep = face.meshvert;
         for (int i = 0; i < face.n_meshverts; i++)
         {
             mverts.Add(map.meshvertLump.meshVerts[mstep]);
         }
 
         // add the meshverts to the object being built
         worldFace.triangles = mverts.ToArray();
 
         // bring it all together
         faceObject.AddComponent<MeshFilter>();
         faceObject.GetComponent<MeshFilter>().mesh = worldFace;
         faceObject.GetComponent<MeshFilter>().mesh.RecalculateBounds();
         faceObject.AddComponent<MeshRenderer>();
         faceObject.renderer.material.shader = Shader.Find("Diffuse");
         faceObject.renderer.material.color = Color.blue;
 
         return faceObject;
     }
Comment

People who like this

0 Show 6
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 Hoeloe · Oct 06, 2013 at 11:30 AM 0
Share

Have you tried checking the backfaces to see if the triangle winding order is reversed?

avatar image ALLCAPS · Oct 06, 2013 at 06:17 PM 0
Share

I tried reversing the triangle indexes, but it's still not showing anything. I'd expect it to show some garbage at least, but it's totally invisible.

Even with a shader applied that renders backfaces I get nothing.

avatar image Hoeloe · Oct 06, 2013 at 07:50 PM 0
Share

Are you sure any of the data is actually reaching the lists? It would make sense if no mesh were being generated - can you select the mesh in the editor while the game is running?

avatar image ALLCAPS · Oct 06, 2013 at 08:48 PM 0
Share

Yes, it generates a bunch of objects, and each of them has a mesh filter with a mesh on it, and a mesh renderer with a material. I can select the mesh and it shows something like (4 verts, 2 tris, uv,uv2), and it shows the correct number of verts and tris for each surface.

Something else that may be relevant is that the objects themselves are positioned at 0,0,0 and the mesh has verts defined where they should be in the level.

Will this pose a problem? I know that it's not ideal, but shouldn't the mesh still render where it "should" be? If I select the topmost gameobject and hold down to scroll through the list quickly the handle in the editor view zips around tracing out the rough layout of the level, so I know some kind of data is there and correct.

Would it work if I read all of the faces into sub-meshes on the same gameobject and joined them using the built-in method for that?

avatar image robertbu · Oct 06, 2013 at 09:10 PM 0
Share

It is hard to sort out the problem in the absence of sample data. It may be as simple as normals being backwards. I don't think rewinding your triangles will help since you don't recalc your normals. You might recalc your normals with the winding both ways, or reverse all your normals as a test.

When I have these kinds of problems with meshes, I build something as simple as a I can to debug. My suggestion is to output the data for the first triangle. Output indices of the first triangle, output the three vertices based on these indices, output the normal for the triangle, and output the uvs for the three vertices. If the data looks okay, then encode this one triangle into a mesh and see what happens.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by whydoidoit · Oct 07, 2013 at 12:03 AM

This bit's got to be wrong I'd guess:

     int mstep = face.meshvert;
     for (int i = 0; i < face.n_meshverts; i++)
     {
         mverts.Add(map.meshvertLump.meshVerts[mstep]);
     }

It looks like it's just adding the same vertex index face.n_meshverts times. Presumably it should be stepping through? Perhaps by adding i to mstep?

Comment
aldonaletto
robertbu

People who like this

2 Show 1 · 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 ALLCAPS · Oct 07, 2013 at 12:12 AM 0
Share

Oh my god. How embarrassing! mstep should ++ each iteration to grab the next vertex for the triangles! Just adding mstep++; fixed everything.

That's what I get for trying to ride the "almost have something on the screen" high at 4am that comes from coding something for eight hours straight. I now have geometry on the screen, and it all looks great! It's oriented on its side because I have to account for some differences in the XYZ directions of Unity/Quake3, but the data is there and looks good.

Thanks a ton! What a dumb mistake!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Set UVs depending on input 1 Answer

calculate normals 2 Answers

Setting Mesh objects in C# only renders Triangles 1 Answer

How to Change an Object's Mesh to another Object's Mesh via Code C# 1 Answer

C# Preserving GameObjects' Previous Meshes 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