[英]Procedural mesh slows down unity scene
我在Unity3d中創建了一個非常簡單的游戲,我需要在其中創建多個網格物體。 我創建的代碼非常簡單,但在同時擁有8個以上的網格后,同步性能大大降低到幾個fps(~8 fps)。 我創建的Mesh只是一個簡單的方塊,所以我真的不知道問題出在哪里,這是我的代碼:
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;
}
}
您的問題是您沒有設置材料或者您沒有提供材料需要的所有材料,如紫外線坐標或頂點顏色。 我不確定它是否是Debug.Log中的錯誤消息,或者着色器本身是否導致低幀率,但為了測試它你可以使用:
// 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;
作為材料,您創建一個新的並使用着色器與此代碼:
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
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.