Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
1
Question by Jagrafess · Mar 19, 2020 at 10:56 AM · procedural meshmesh manipulationvertex color

Vertex color emptied after instanciating mesh at runtime (build only)

I have a project where I'm using vertex colours as data to procedurally stretch my meshes, ie red goes left, green: forward, blue: up, I don't use them to render at all. At run-time I load the mesh and modify the vertices using the colour information. This works perfectly in debug, but in a built version the color data in the instanciated meshes is empty.

If I manually place a the meshes in the scene with a material that uses vertex colours this seems to force unity to know it has colours and it works fine. However this is an ugly hack, is there something I'm missing?

Comment
Add comment · 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 Namey5 · Mar 19, 2020 at 11:37 AM 0
Share

It definitely could be a bug. Are you using GPU instancing? Also, when you modify the vertex colours, are you doing it post-instantiation (with different vertex colours per instance), or are the vertex colours shared between all copies of the mesh?

avatar image Jagrafess · Mar 19, 2020 at 11:50 AM 0
Share

Example parts

Here's an example, just a single script and a basic model with vertex colours, the whole project was a bit big to upload, I've just put this together quickly to demonstrate the scenario. You'll notice it works fine in debug, but when you build it, it throws an out of bounds exception because the mesh.colors is empty.

Note: you need to turn on Read/Write Enabled on the mesh

Here's the code again to make it more visible:

  private void Stretch$$anonymous$$esh()
     {
         var instance = Instantiate($$anonymous$$eshObject);
         var meshes = instance.GetComponentsInChildren<$$anonymous$$eshFilter>().Select(v => v.mesh);
 
         var moveDist = 10f;
         foreach (var mesh in meshes)
         {
             var verts = mesh.vertices;
             var colors = mesh.colors;
 
             for (var i = 0; i < verts.Length; i++)
             {
                 // On build this throws an index out of range exception because the colours are empty
                 verts[i].x += moveDist * colors[i].r;
             }
 
             var new$$anonymous$$esh = new $$anonymous$$esh
             {
                 vertices = verts,
                 triangles = mesh.triangles,
                 colors = mesh.colors,
                 normals = mesh.normals,
                 tangents = mesh.tangents,
                 uv = mesh.uv,
             };
             new$$anonymous$$esh.RecalculateBounds();
 
             var go = new GameObject("Test");
             var filter = go.AddComponent<$$anonymous$$eshFilter>();
             var renderer = go.AddComponent<$$anonymous$$eshRenderer>();
             filter.mesh = new$$anonymous$$esh;
             renderer.material = Test$$anonymous$$aterial;
         }
     }
example.zip (8.2 kB)
avatar image Namey5 Jagrafess · Mar 19, 2020 at 12:55 PM 0
Share

Ah, my misunderstanding. You're absolutely right, it would appear as though Unity only registers mesh colours if they are going to be immediately used in rendering; if the material doesn't use mesh colours, it seems to just skip loading them entirely. The only real workaround I can suggest without having to manually place the objects is to just go into the mesh import settings and override the default material to something that uses vertex colours. It has the same effect in forcing Unity to recognise the mesh fully, but you don't need to change anything in your scene. I can only guess this has something to do with Unity wanting to pass as little data into the shader as possible, and maybe getting a bit carried away in their assumptions on how meshes are used. $$anonymous$$ight be worth reporting as a bug. Aside from that, I will say - could you not do this all in a shader anyway? It would be a lot more efficient than directly modifying the mesh.

avatar image Jagrafess Namey5 · Mar 20, 2020 at 02:07 PM 0
Share

$$anonymous$$akes sense, however I don't think a shader will work too well for this, I'm building lots of these with different values passed in and they are combined into meshes with the same materials in the end so they're pretty cheap once the procedural generation has finished. After some more playing I think I'll just move toward using bones ins$$anonymous$$d, they're a little easier to setup and more flexible in the end plus unity has a method to bake them down to a mesh.

avatar image Bunny83 Jagrafess · Aug 21, 2020 at 06:13 AM 0
Share

You do realise that this line:

 var instance = Instantiate($$anonymous$$eshObject);

does NOT instantiate your meshes at all. All your mesh filters will still use their original meshes. However this part:

  .Select(v => v.mesh);

will implicitly duplicate the mesh for this meshfilter instance specifically. Have you tried to actually instantiate the shared$$anonymous$$esh manually for each $$anonymous$$eshFilter? Something like that:

 var meshFilters = instance.GetComponentsInChildren<$$anonymous$$eshFilter>();
 var moveDist = 10f;
 foreach (var meshFilter in meshFilters)
 {
     var mesh = Instantiate(meshFilter.shared$$anonymous$$esh);
     meshFilter.shared$$anonymous$$esh = mesh;
     var verts = mesh.vertices;
     var colors = mesh.colors;



avatar image axelucho · Aug 21, 2020 at 04:51 AM 0
Share

Were you able to find a fix for this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jagrafess · Aug 21, 2020 at 05:03 AM

No @axelucho, I ended up using fixed width meshes for the meantime while I concentrated on other parts of the project. Unless the latest version of Unity fixes it, it doesn't look like a viable solution.

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

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

Related Questions

Why do I get this weird mesh error when I have a mesh over 250 by 250? 1 Answer

Mesh.colors doesn't work even though colors are passed correctly to the array 1 Answer

Can I Assign Different Material To Each Quad In A Mesh? 1 Answer

Change vertices colors without copying whole array 1 Answer

AddAdditionalVertexStreams and static meshes 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