Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can't read data from static mesh?

Discussion in 'Editor & General Support' started by barbe63, May 9, 2015.

  1. barbe63

    barbe63

    Joined:
    Oct 6, 2014
    Posts:
    35
    Hello

    I'm using static flag now as it seems better since unity 5 than combine them with a script. Problem is i use a couple of functions that need to acces vertices, triangles etc from those meshes (for drawing a mesh for a decal) and Unity tells me I can't..

    I can't set them to non static and back to static on the fly since it would need the editor.

    I understand that the engine need to protect data from being written but the reading? I don't get why it would be also protected... Maybe i'm doig something wrong. What's are your thoughts on this?

    Btw my meshes have the write/read check so it's not that, it works only if i remove the static flag. I can't afford that or i have to return in the old way... Combining myself.

    Also posted here: http://answers.unity3d.com/questions/962919/cant-read-data-from-static-mesh.html
     
    Last edited: May 10, 2015
  2. barbe63

    barbe63

    Joined:
    Oct 6, 2014
    Posts:
    35
  3. barbe63

    barbe63

    Joined:
    Oct 6, 2014
    Posts:
    35
  4. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Rather than typing 4 letters into a post, why not make the assumption that your question isn't easy to understand, and spend a bit more time explaining it. I, for example, have used Unity for 6 years and don't understand what you are talking about, and can't see any code to help explain things.
     
    blizzy likes this.
  5. barbe63

    barbe63

    Joined:
    Oct 6, 2014
    Posts:
    35
    Sorry about that, i thought it was clear enough but i often forget that my english is pretty bad,.

    I need to instantiate a decal on the fly and to doing so i need access to the vertices, triangles etc data of the mesh its colliding with so the mesh could actually have the same shape of the colliding object. I had that working great before but i wasn't using the static flag. instead i was combining myself with CombineChildrenAdvanced. Before UNITY 5 it was better to do so than letting unity doing the job (or at least that's what i thought) because draw calls were very important for a mobile app. Now it seems that draw calls aren't the bottleneck anymore so i tried to set static flag instead and i gained a solid 10 fps. But the function i need to make my decal can't run anymore since the static flag protect the read access (that i can't understand why) and UNITY send me a bug that i can't access to those data, and if I remove the static flag it works again.
    I can post you part of the code (which is not mine)but i'm not sure it would be revelant here.

    Code (CSharp):
    1. public static void BuildDecalForObject(Decal decal, GameObject affectedObject) {
    2.         Mesh affectedMesh = affectedObject.GetComponent<MeshFilter>().sharedMesh;
    3.         if(affectedMesh == null) return;
    4.  
    5.         float maxAngle = decal.maxAngle;
    6.  
    7.         Plane right = new Plane( Vector3.right, Vector3.right/2f );
    8.         Plane left = new Plane( -Vector3.right, -Vector3.right/2f );
    9.  
    10.         Plane top = new Plane( Vector3.up, Vector3.up/2f );
    11.         Plane bottom = new Plane( -Vector3.up, -Vector3.up/2f );
    12.  
    13.         Plane front = new Plane( Vector3.forward, Vector3.forward/2f );
    14.         Plane back = new Plane( -Vector3.forward, -Vector3.forward/2f );
    15.  
    16.         Debug.Log(affectedObject);
    17.         Vector3[] vertices = affectedMesh.vertices;
    18. /.. the code goes on but the error comes at this line
    The exact message error is : Not allowed to access vertices on Mesh 'Combined mesh (root: scene)'
    I did some searching around and this was leading me to the static hint, which is indeed the guilty one here.

    What i want to know is:
    -is there another way to proceed to fit my need? How do you guys do?
    -and why is the static flag preventing me to read in the first place?

    Oh and for more precision about what i'm trying to do.. I have a turret that shot a rocket, if it hit a walls or something i want to have a decal who fit the collided shape to be instantiated.

    Please also tell me if i'm clear enough now or if I have to try harder explaining it.
     
    Last edited: May 12, 2015
  6. barbe63

    barbe63

    Joined:
    Oct 6, 2014
    Posts:
    35
    I still need an answer on this. Do you understand my problem now?
     
  7. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    I don't know enough about your decisions. GameObjects marked as static in the editor get consumed into batches. Telling the editor that a game object is not going to move at all allows the runtime to combine a lot of these game objects together, so makes rendering faster. I don't know enough about your game objects. Potentially if they were small, I'd duplicate them, not mark them as static, and use some runtime code to grab the verts from them when the level starts. I'd then make sure that the runtime knows that they don't need rendering. (Alternatively, you could write some editor script that mines out the verts, and stores them Resources as text files.)
    I've never done this.

    Because the GO is being converted into a batch along with other static GOs. Marking them as static means "render these babies as fast as possible." To make them fast, they don't look like Meshes that a GO has.

    So, i think having a non-rendered dupe of the wall makes sense, which is what I was saying above.
     
    blizzy likes this.
  8. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Seems like I understood your problem, but no-one else in the community does. Or they see you as someone who's happy to use bump, and decide to leave you to it.
     
  9. barbe63

    barbe63

    Joined:
    Oct 6, 2014
    Posts:
    35
    Hahaha! Yeah maybe. Thanks a lot for your concern and your nice input. It looks like the txt solution would be better for the overall performances but i don't have any clue of how I could manage this with my few knowledge, so I decided to give the dupe walls a try. Again thank you!
     
  10. barbe63

    barbe63

    Joined:
    Oct 6, 2014
    Posts:
    35
    That's just perfect! This is working again and the profiler don't even show any sign of performances losses. Thanks a ton!