gpt4 book ai didi

c# - 程序网格减慢统一场景

转载 作者:行者123 更新时间:2023-11-30 17:48:32 25 4
gpt4 key购买 nike

我在 Unity3d 中创建了一个非常简单的游戏,我需要在其中创建多个网格。我创建的代码非常简单,但在同时拥有 8 个以上的网格后,对等性能大大降低到只有几个 fps(~8 fps)。我创建的网格只是一个简单的正方形,所以我真的不知道问题出在哪里,这是我的代码:

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;

}
}

最佳答案

您的问题是您没有设置 Material ,或者您没有提供 Material 所需的一切,例如 uv 坐标或顶点颜色。我不确定是 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;

作为 Material ,您创建一个新 Material 并使用带有此代码的着色器:

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
}
}
}

关于c# - 程序网格减慢统一场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22926739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com