- Home /
Getting triangles of a single gameobject (one prefab instance, not all)
Hi! Finally I've found a question that does not seem to be covered yet here! :)
Here it is: I want to do a custom lighting by changing colors of faces that point to my light source. This is going to be some sort of "prebaked" lighting but without the overhead of additional textures and without some limitations of lightmapping.
In order to do this I determine the gameobjects close to the lightsource. Out of those objects I want to get the faces that point toward the lightsource.
My problem is that I can get the mesh, vertices, normals, etc. but only for all instances of the prefab (as it is shared). There does not seem to be a way to get for example only the triangles of a given (single) game object. Whenever I call gameObject.GetComponent().mesh.vertices I get the entire list of vertices (probably of all existing instances of my static prefab).
What I am looking for would be some sort of: gameobject.GetMeYourTriangleIndicesButNotThoseOfYourSiblingInstances().
After hours of research I start running out of ideas. Any help on this would be highly welcome!!!
So if you have two cubes (eight vertices each), your .vertices call is getting 16 vertices back?
right! when static flag is set. (see my comment to scroodge's answer)
Answer by ScroodgeM · Aug 12, 2012 at 10:22 PM
as i understood you correct, you need
http://docs.unity3d.com/Documentation/ScriptReference/Mesh-subMeshCount.html
http://docs.unity3d.com/Documentation/ScriptReference/Mesh.GetTriangles.html
note, that submeshes are separated only in triangles, vertices are shared for all submeshes. so vertex used in one submesh can be used in another.
These submeshes are also shared unfortunately. I don't get them for a single instance.
The problem seems to be the 'static' flag. I wrote a simple script and attached it to one of my cubes:
$$anonymous$$esh mesh = gameObject.GetComponent().mesh; Vector3[] vertices = mesh.vertices; Debug.Log("v="+vertices.Length+", smc="+mesh.sub$$anonymous$$eshCount);
When I run it with static flag set on my cube, then I get this : v=6169 ('=vertice count'), smc=576 ('=submesh count')
Without static flag: v=24 ('vertice count'), smc=5 ('=submesh count')
=> without static flag I can access the triangles of a single object => with static flag this information is no longer available. I can only get triangles for... I don't know... the result of unitys internal merging process (problably all static objects)
As I am developing for mobile plattforms I really want to use the static flag in order to avoid thousands of draw calls on static objects.
i missed that you are talking about statics...
if you mark object as 'static' it means that you want not to move, rotate, change, scale, change material etc. this checkbox lets unity to combine all static objects to decrease drawcalls.
this is internal mechanism and AFAI$$anonymous$$ unity doesn't store information for reverting this action. so, in your static mode you are not able to get object back.
as a solution, you can make a custom mesh-combinator which will store information for recombine, or just stores (but not draws) original information and just recombines it on original changes.
it's a lot of examples with mesh-combiners in unity free access.
i believe unity should behave differently on this one. an internal optimization should not alter the result of unity's interface methods. this can lead a developer on the wrong path.
furtunately, in my case I can live without statics. The performance seems to be good enough!