private static void RecordGrass(Dictionary <string, List <GameObject> > grassDic, SceneElementAsset holder) { foreach (var pair in grassDic) { List <GameObject> objects = pair.Value; MeshRenderer mr = objects[0].GetComponentInChildren <MeshRenderer>(); MeshFilter mf = objects[0].GetComponentInChildren <MeshFilter>(); if (mr == null || mf == null) { continue; } GrassGroup group = new GrassGroup(); group.matrixList = new Matrix4x4[objects.Count]; for (int i = 0; i < objects.Count; i++) { Transform trans = objects[i].transform; group.matrixList[i] = Matrix4x4.TRS(trans.position, trans.rotation, trans.localScale); } group.mesh = mf.sharedMesh; group.mat = mr.sharedMaterial; holder.Add(group); } }
private static void Record(GameObject o, SceneElementAsset holder, string prefabPath, int id) { if (!o.transform.parent) { return; } if (o.transform.parent && o.transform.parent.name == "preload") { PreloadElement element = new PreloadElement(); element.ResId = SearchPrefabIndex(prefabPath); element.position = o.transform.position; element.rotation = o.transform.eulerAngles; element.scale = o.transform.localScale; holder.Add(element); } else { MeshRenderer[] mrs = o.GetComponentsInChildren <MeshRenderer>(); SceneElement element = new SceneElement(); element.ID = id; element.ResId = SearchPrefabIndex(prefabPath); if (element.ResId == -1) { return; } element.order = orderList.IndexOf(o.transform.parent.name); element.position = o.transform.position; element.rotation = o.transform.eulerAngles; element.scale = o.transform.localScale; element.lightmapIndex = new List <int>(); element.lightmapOffset = new List <Vector4>(); if (mrs != null && mrs.Length > 0) { Vector3 min = mrs[0].bounds.min; Vector3 max = mrs[0].bounds.max; foreach (MeshRenderer mr in mrs) { //添加光照贴图 element.lightmapIndex.Add(mr.lightmapIndex); element.lightmapOffset.Add(mr.lightmapScaleOffset); //对比包围盒大小 min = Vector3.Min(mr.bounds.min, min); max = Vector3.Max(mr.bounds.max, max); } Vector3 size = max - min; Bounds bounds = new Bounds(min + size / 2, size); if (size.x <= 0) { size.x = 0.2f; } if (size.y <= 0) { size.y = 0.2f; } if (size.z <= 0) { size.z = 0.2f; } bounds.size = size; element.Bounds = bounds; holder.Bounds.Encapsulate(bounds); } else { Bounds bounds = new Bounds(element.position, 0.1f * Vector3.one); element.Bounds = bounds; } holder.Add(element); } }