Anti Triangle Strips for Dynamic Batching

Hi all

I am testing a script that baypass the Triangle Strip creation on Unity Import pipeline.

It generate another mesh without calling ‘Optimitzation’ function, and then, no Triangle Strips are generated.

using UnityEngine;

class AntiStrip : MonoBehaviour
{

	void Start()
	{
		MeshFilter mf = (MeshFilter)this.gameObject.GetComponent(typeof(MeshFilter));
		if (mf == null)
		{
			Debug.LogError("Comp:MeshFilter");
		}

		Mesh me = mf.mesh;
		Mesh MeNew = new Mesh();

		Debug.Log("Triangles: " + (me.triangles.Length / 3));
		Debug.Log("Vertices: " + me.vertexCount);


		if (me.vertices != null)
		{
			MeNew.vertices = (Vector3[])me.vertices.Clone();
		}

		if (me.triangles != null)
		{
			MeNew.SetTriangles(me.triangles, 0);
		}

		if (me.uv != null)
		{
			MeNew.uv = (Vector2[])me.uv.Clone();
		}

		if (me.uv1 != null)
		{
			MeNew.uv1 = (Vector2[])me.uv2.Clone();
		}

		if (me.uv2 != null)
		{
			MeNew.uv2 = (Vector2[])me.uv2.Clone();
		}

		if (me.normals != null)
		{
			MeNew.normals = (Vector3[])me.normals.Clone();
		}

		if (me.tangents != null)
		{
			MeNew.tangents = (Vector4[])me.tangents.Clone();
		}

		if (me.colors != null)
		{
			MeNew.colors = (Color[])me.colors.Clone();
		}

		if (me.bindposes != null)
		{
			MeNew.bindposes = (Matrix4x4[])me.bindposes.Clone();
		}

		if (me.boneWeights != null)
		{
			MeNew.boneWeights = (BoneWeight[])me.boneWeights.Clone();
		}
	
		MeNew.name = me.name;
		MeNew.hideFlags = me.hideFlags;
		MeNew.subMeshCount = me.subMeshCount;
		MeNew.RecalculateBounds();

		mf.mesh = MeNew;
	}
}

Let me know if it is useful for you and if you found any error :slight_smile:

Thanks in advance

LLORENS

for those that are not able to understand “baypass the Triangle Strip creation on Unity Import pipeline”, that means avoiding Unity adding extra info to your meshes so you don’t get meshes with extra triangles or vertex WHEN IMPORTING.

Its interesting anyway that it does that, cause indexed triangle lists are the fasted way to represent the data and they would be more flexible ( as by the official hardware creators dev recommentations at http://www.imgtec.com/powervr/insider/sdk/KhronosOpenGLES2xSGX.asp ). That being said, perhaps Unity 3 does so since Beta6, cause from what I read from other users, it seems like meshes now out of the nowhere are larger in RAM, which would be a direct consequence of going with lists instead of the strips (thats also important to keep in mind here if you target pre3GS where RAM is the most precious resource at hand)

Thanks for sharing this, its interesting.
Do you think it would be possible to get a similar thing on a much higher and more automatic way: the importer script? (unsure if it would be of use there of ir the idea of stripifying it is done afterwards, didn’t invest much time on this end yet)

Hi dreamora

Thanks for your answer, i don’t know (yet) how to inject this script in Import Pipeline, meanwhile i can say that its working for me, i attached it on my enemy prefabs and they are spawning and batching dynamically.

I don’t know how to automatize this script more than now sorry :frowning:

LLORENS

Does this script also work on Unity not iPhone? and can it also be used with static models? So is it always a good practice to add it to your models? Any problems?

Thanks.

Hi

I think that in PC is not aplicable because grpahic hardware (on PC) have a deep optimitzations about triangle strips.

You can use it, but i dont know if it will benefit you with a high performance.

And yes, it can be applied on static models and any other meshes.

Keep in mind that is posible in Unity 3 that this script is no more applicable, because the posibility of keep meshes in a triangle list representation instead of triangle strip, but i dont know if this is true or not :slight_smile:

Hope it help you.

LLORENS