fix the logic but dont change things you dont know abt

	public static Renderer[] GeneratePrim(this ScenePrimData spd, DetailLevel detailLevel)
	{
		if (spd.prim.PrimData.PCode == PCode.Avatar) return new Renderer[0];

		MeshmerizerR mesher = new();
		FacetedMesh fmesh = mesher.GenerateFacetedMesh(spd.prim, detailLevel);

		GameObject gomesh;
		Renderer rendr;
		MeshFilter meshFilter;
		Renderer _renderers = new Renderer();
		// Create a single GameObject if it doesn't exist
		if (_renderers == null)
		{
			gomesh = SimManager.Instantiate(ResourceCache.cubePrefab);
#if UNITY_EDITOR
			gomesh.name = "Combined Mesh";
#endif
			//gomesh.transform.SetPositionAndRotation(spd.obj.transform.localPosition, spd.obj.transform.localRotation);
			gomesh.transform.parent = spd.meshHolder.transform;
			// Instead of setting the global position of the combined mesh directly
			// Reset the local position and rotation of 'gomesh' relative to its parent which already set at correct position
			gomesh.transform.SetLocalPositionAndRotation(Vector3.zero, Quaternion.identity);
			gomesh.transform.localScale = Vector3.one;
			rendr = gomesh.GetComponent<Renderer>();
			_renderers = rendr;
		}
		else
		{
			rendr = _renderers;
			gomesh = rendr.gameObject;
			rendr.enabled = true;
			rendr.GetComponent<MeshFilter>().mesh = null;
		}
		PrimInfo pi = gomesh.GetComponent<PrimInfo>();
		if (spd.prim.LocalID == 0) Debug.Log("local ID cannot be 0");
		pi.localID = spd.prim.LocalID;
		pi.uuid = spd.uuid;

		List<Vector3> allVertices = new List<Vector3>();
		List<Vector3> allNormals = new List<Vector3>();
		List<Vector2> allUvs = new List<Vector2>();
		List<Material> materials = new List<Material>();
		List<int> subMeshIndices = new List<int>();

		int vertexOffset = 0;

		for (int j = 0; j < fmesh.Faces.Count; j++)
		{
			var faceVertices = fmesh.Faces[j].Vertices;
			int vertexCount = faceVertices.Count;

			Vector3[] vertices = new Vector3[vertexCount];
			Vector3[] normals = new Vector3[vertexCount];
			Vector2[] uvs = new Vector2[vertexCount];

			Primitive.TextureEntryFace textureEntryFace = spd.prim.Textures.GetFace((uint)j);

			_transformTexCoords.TransformTexCoords(faceVertices, fmesh.Faces[j].Center, textureEntryFace, spd.prim.Scale);

			for (int i = 0; i < vertexCount; i++)
			{
				vertices[i] = faceVertices[i].Position.ToUnity();
				normals[i] = faceVertices[i].Normal.ToUnity() * -1f;
				uvs[i] = faceVertices[i].TexCoord.ToUnity();
			}

			allVertices.AddRange(vertices);
			allNormals.AddRange(normals);
			allUvs.AddRange(uvs);

			int[] indices = fmesh.Faces[j].Indices.Select(index => index + vertexOffset).ToArray();
			subMeshIndices.AddRange(indices);

			vertexOffset += vertexCount;

			Color color = textureEntryFace.RGBA.ToUnity();
			Material clonemat;

			if (color.a < 0.999f)
			{
				clonemat = new Material(textureEntryFace.Fullbright ? ResourceCache.alphaFullbrightMaterial : ResourceCache.alphaMaterial);
			}
			else
			{
				clonemat = new Material(textureEntryFace.Fullbright ? ResourceCache.opaqueFullbrightMaterial : ResourceCache.opaqueMaterial);
			}

			clonemat.SetColor("_BaseColor", color);

			materials.Add(clonemat);
		}

		rendr.materials = materials.ToArray();

		meshFilter = gomesh.GetComponent<MeshFilter>();
		Mesh mesh = new Mesh();
		mesh.SetVertices(allVertices);
		mesh.SetNormals(allNormals);
		mesh.SetUVs(0, allUvs);

		mesh.subMeshCount = fmesh.Faces.Count;

		int offset = 0;
		for (int j = 0; j < fmesh.Faces.Count; j++)
		{

			pi.face = j;
			int count = fmesh.Faces[j].Indices.Count;
			mesh.SetTriangles(subMeshIndices.GetRange(offset, count), j);
			offset += count;
			ClientManager.simManager.TextureFace(spd.prim, j, rendr);
		}

		meshFilter.sharedMesh = mesh.ReverseWind().FlipNormals();


		return new Renderer[] { rendr };
	}