3

Im creating a pretty simple game in Unity3d where I need to create multiple Meshes. The code that I create is pretty simple and yet after having more than 8 Meshes at the same time, the peerformance reduces considerably to just a couple of fps (~8 fps). The Mesh that I create is just a simple square so I really don´t know where the problem is, here´s my code:

using UnityEngine;
using System.Collections;

public class TetraGenerator : MonoBehaviour {
    public int slices;
    public GameObject forceSource;
    void OnMouseDown(){
        var arcLength = Mathf.PI / slices;
        var distance = 10;
        var height = 1;
        var origin = Random.Range(-slices,slices);

        Vector3[] vertices = new Vector3[4];
        vertices [0] = new Vector3 (Mathf.Cos(origin*arcLength),Mathf.Sin(origin*arcLength));
        vertices [1] = new Vector3 (Mathf.Cos(origin*arcLength),Mathf.Sin(origin*arcLength));
        vertices [2] = new Vector3 (Mathf.Cos((origin+1)*arcLength),Mathf.Sin((origin+1)*arcLength));
        vertices [3] = new Vector3 (Mathf.Cos((origin+1)*arcLength),Mathf.Sin((origin+1)*arcLength));

        vertices [0] *= distance;
        vertices [1] *= (distance+height);
        vertices [2] *= (distance+height);
        vertices [3] *= distance;

        Vector3 frameRef = new Vector3(Mathf.Cos(origin*arcLength+(arcLength/2)),Mathf.Sin(origin*arcLength+(arcLength/2)));
        frameRef *= distance;

        vertices [0] -= frameRef;
        vertices [1] -= frameRef;
        vertices [2] -= frameRef;
        vertices [3] -= frameRef;

        int[] triangles = new int[]{0,1,2,2,3,0};

        Mesh mesh = new Mesh ();
        mesh.vertices = vertices;
        mesh.triangles = triangles;

        GameObject tile = new GameObject("tile",typeof(MeshFilter),typeof(MeshRenderer));
        tile.transform.position = frameRef;

        MeshFilter meshFilter = tile.GetComponent<MeshFilter> ();
        meshFilter.mesh = mesh;

    }
}

1 Answer 1

1

Your problem is that you are not setting a material or that you dont provide everything the material needs like uv coordinates or vertex colors. I am not sure if its the error messages in the Debug.Log or if the shader itself is causing the low framerate, but to test it you can use:

// enter this at the top and set the material in the inspector
public Material mat;

[...]

// enter this at the bottom
MeshRenderer meshRenderer = tile.GetComponent<MeshRenderer>();
meshRenderer.material = mat;

and as material you create a new one and use a shader with this code:

Shader "SimpleShader"
{
    SubShader
    {
        Pass
        {
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            struct vertexInput
            {
                float4 pos : POSITION;
            };

            struct vertexOutput
            {
                float4 pos : SV_POSITION;
                float4 col : COLOR0;
            };

            vertexOutput vert(vertexInput input)
            {
                vertexOutput output;

                output.pos = mul(UNITY_MATRIX_MVP, input.pos);
                output.col = float4(1, 0, 0, 1);

                return output;
            }

            float4 frag(vertexOutput input) : COLOR
            {
                return input.col;
            }

            ENDCG
        }
    }
}    
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.