void Init(GameObject obj, BakeDepthParam param) { m_param = param; m_horizon_height = obj.transform.position.y; Mesh mesh = obj.GetComponent <MeshFilter>().sharedMesh; const float unit_size = 10; m_step_arr = new int[mesh.vertexCount]; m_width = (int)(unit_size * obj.transform.lossyScale.x / m_param.UnitSize) + 1; m_height = (int)(unit_size * obj.transform.lossyScale.z / m_param.UnitSize) + 1; //m_quad_tree_width = Mathf.NextPowerOfTwo(m_width); m_vertiecs = mesh.vertices; m_indices = new List <int>(); m_quad_gen_list = new List <KeyValuePair <int, int> >(); m_uvs = mesh.uv; m_colors = mesh.colors; Debug.Log("[OptimizeMesh-Enter] vertex count : " + m_vertiecs.Length.ToString() + " uv count : " + m_uvs.Length.ToString() + " color count : " + m_colors.Length.ToString()); //辅助结构 m_max_step = Mathf.ClosestPowerOfTwo(m_param.MaxQuadWidth); m_list_quad_tree_max_step = new int[mesh.vertexCount]; m_list_vertex_state = new VertexState[mesh.vertexCount]; m_quad_size_loop = new int[m_max_step]; for (int i = 0; i < m_max_step; ++i) { m_quad_size_loop[i] = CalQuadMaxStep(i); } }
public List <PatchInfo> DividePatch(GameObject plane, BakeDepthParam param, Mesh mesh) { //根据plane的大小划分成多个不同的网格,这里要通过参数设置保证plane的大小是设置的Patch的整数倍 // 1单位的Scale 为 10, Vector2 total_size = new Vector2(0f, 0f); total_size.x = plane.transform.lossyScale.x * CommonData.UnitSize; total_size.y = plane.transform.lossyScale.z * CommonData.UnitSize; Vector2 unit_size = new Vector2(param.Size.x * param.UnitSize, param.Size.y * param.UnitSize); Vector2Int total_num = new Vector2Int(Mathf.CeilToInt(total_size.x / unit_size.x), Mathf.CeilToInt(total_size.y / unit_size.y)); List <PatchInfo> meshes = new List <PatchInfo>(); //根据原有位置和目标的圆心位置,得到一个中心平移的向量 Vector3 mesh_first_vertex_pos = new Vector3(unit_size.x / 2, 0, unit_size.y / 2) - new Vector3(total_size.x / 2, 0, total_size.y / 2); Vector2Int patch_vertex_size = new Vector2Int(param.Size.x, param.Size.y); Vector2Int total_vertex_size = new Vector2Int(Mathf.CeilToInt(total_size.x / param.UnitSize) + 1, Mathf.CeilToInt(total_size.y / param.UnitSize) + 1); for (int i = 0; i < total_num.x; ++i) { for (int j = 0; j < total_num.y; ++j) { //需要中心对称 Vector2Int offset_index = new Vector2Int(i, j); meshes.Add(GenPatchInfo(offset_index, unit_size, plane, mesh_first_vertex_pos, patch_vertex_size, total_vertex_size, mesh)); } } Debug.Log("[Divide Sub Mesh] lossyscale : " + plane.transform.lossyScale.ToString() + ", total size : " + total_size.ToString() + ", unit size : " + unit_size.ToString() + ", total num : " + total_num.ToString()); Debug.Log("[GenMeshUtil] Divide Mesh Done"); return(meshes); }
public void OptimizeMesh(GameObject obj, BakeDepthParam param) { //初始化 Init(obj, param); //合并格子 CombineQuad(); //压缩顶点 CompressMesh(); //把计算后的值更新给obj UpdateMesh(obj); Debug.Log("OptimizeMeshUtil Done"); }