Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 maifee · Jun 22, 2019 at 11:03 AM · meshprocedural meshmeshfiltersharedmesh

Procedural Mesh problem from Editor Window

Here is the method to generate cube collected over internet:

     static void CreateCube()
     {
         Vector3[] vertices = {
             new Vector3 (0, 0, 0),
             new Vector3 (1, 0, 0),
             new Vector3 (1, 1, 0),
             new Vector3 (0, 1, 0),
             new Vector3 (0, 1, 1),
             new Vector3 (1, 1, 1),
             new Vector3 (1, 0, 1),
             new Vector3 (0, 0, 1),
         };
 
         int[] triangles = {
             0, 2, 1, //face front
             0, 3, 2,
             2, 3, 4, //face top
             2, 4, 5,
             1, 2, 5, //face right
             1, 5, 6,
             0, 7, 4, //face left
             0, 4, 3,
             5, 4, 7, //face back
             5, 7, 6,
             0, 6, 7, //face bottom
             0, 1, 6
         };
 
         GameObject tem = new GameObject("MUA-OSM Custom Mesh");
         tem.AddComponent<MeshFilter>();
 
 
 
         Mesh mesh = tem.GetComponent<MeshFilter>().mesh;
         mesh.Clear();
         mesh.vertices = vertices;
         mesh.triangles = triangles;
         mesh.Optimize();
         mesh.RecalculateNormals();
     }
 

When I call this method, it gives me error :

Instantiating mesh due to calling MeshFilter.mesh during edit mode. This will leak meshes. Please use MeshFilter.sharedMesh instead. UnityEngine.MeshFilter:get_mesh()

And when finally I change Mesh mesh = tem.GetComponent().mesh; to Mesh mesh = tem.GetComponent().sharedMesh;

It gives me another error, saying :

NullReferenceException: Object reference not set to an instance of an object

I clicked on the Console and it referred me to the same line.

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 Bunny83 · Jun 22, 2019 at 12:14 PM

Because you did not create any mesh yet. You should do:

 Mesh mesh = new Mesh();
 mesh.vertices = vertices;
 mesh.triangles = triangles;
 mesh.Optimize();
 mesh.RecalculateNormals();
 mesh.RecalculateBounds();
 
 GameObject tem = new GameObject("MUA-OSM Custom Mesh");
 MeshFilter mf = tem.AddComponent<MeshFilter>();
 mf.sharedMesh = mesh;

You forget the important RecalculateBounds as well. Though you should realise that the mesh you're going to create here is not a cube mesh. A single vertex can only have one vertex normal. So your "cube" will look more like a sphere when it comes to shading the faces. In order to have a cube mesh to actually look like a cube you need 24 vertices (4 vertices per face, so 6*4 == 24). This is exactly what the default Unity cube mesh looks like. Apart from the vertex normals if you want to have a texture mapped onto the faces you need to specify UV coordinates as well. Again those have to be specified per vertex. So another reason why a single face need to have it's own vertices.

Comment
Add comment · 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 Bunny83 · Jun 22, 2019 at 12:36 PM 0
Share

I've posted a code example how to create a mesh that resembles the camera view frustum. With a few modifications it would create a normal cube mesh. The only difference would be the initial 8 corners. The code actually splits up the 8 corners into 24 vertices using the m_VertOrder map. All that needs to be changed is like 30 and 31 you would set them to 0 and 1 ins$$anonymous$$d of using the near and far planes and als remove the for loop in line 33 and 34. This should give you a cube mesh. However still without UV coordinates, so no texture mapping.

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

121 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mesh.vertices are all 0? 1 Answer

Cannot using two script to modify the same mesh at the same time 1 Answer

Query if MeshFilter is instantiated or shared? 1 Answer

Sharing a generated mesh between multiple game objects 2 Answers

sharedMesh - what is the mesh shared with? 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