Ejemplo n.º 1
0
        private void Combine(HLODBuildInfo info, dynamic options)
        {
            var renderers   = info.renderers;
            var atlas       = m_Packer.GetAtlas(info);
            var atlasLookup = new Dictionary <Texture2D, Rect>();

            for (int i = 0; i < atlas.MultipleTextures.Length; ++i)
            {
                atlasLookup[atlas.MultipleTextures[i].textureList[0]] = atlas.UVs[i];
            }

            List <TextureInfo> textureInfoList = options.TextureInfoList;
            var combineInstances = new List <CombineInstance>();
            var combinedMesh     = new Mesh();

            for (int i = 0; i < info.renderers.Count; ++i)
            {
                var mf = info.renderers[i].GetComponent <MeshFilter>();
                if (mf == null)
                {
                    continue;
                }

                var mesh = ConvertMesh(mf, info.simplifiedMeshes[i], atlasLookup, textureInfoList[0]);

                for (int j = 0; j < mesh.subMeshCount; ++j)
                {
                    var ci = new CombineInstance();
                    ci.mesh         = mesh;
                    ci.subMeshIndex = j;

                    Matrix4x4 mat      = mf.transform.localToWorldMatrix;
                    Vector3   position = m_hlod.transform.position;
                    mat.m03     -= position.x;
                    mat.m13     -= position.y;
                    mat.m23     -= position.z;
                    ci.transform = mat;
                    combineInstances.Add(ci);
                }
            }

            combinedMesh.indexFormat = IndexFormat.UInt32;
            combinedMesh.CombineMeshes(combineInstances.ToArray());
            combinedMesh.RecalculateBounds();

            var go           = new GameObject(info.name);
            var meshRenderer = go.AddComponent <MeshRenderer>();
            var meshFilter   = go.AddComponent <MeshFilter>();

            go.transform.SetParent(m_hlod.transform);
            meshFilter.sharedMesh = combinedMesh;
            meshRenderer.material = GetMaterial(options, atlas.PackedTexture);

            info.combinedGameObjects.Add(go);
        }
Ejemplo n.º 2
0
        private void Combine(Vector3 rootPosition, TexturePacker packer, HLODBuildInfo info, dynamic options)
        {
            var atlas = packer.GetAtlas(info);

            if (atlas == null)
            {
                return;
            }

            List <TextureInfo> textureInfoList           = options.TextureInfoList;
            List <MeshCombiner.CombineInfo> combineInfos = new List <MeshCombiner.CombineInfo>();

            for (int i = 0; i < info.WorkingObjects.Count; ++i)
            {
                var obj = info.WorkingObjects[i];
                ConvertMesh(obj.Mesh, obj.Materials, atlas, textureInfoList[0].InputName);

                for (int si = 0; si < obj.Mesh.subMeshCount; ++si)
                {
                    var ci = new MeshCombiner.CombineInfo();
                    ci.Mesh      = obj.Mesh;
                    ci.MeshIndex = si;

                    ci.Transform      = obj.LocalToWorld;
                    ci.Transform.m03 -= rootPosition.x;
                    ci.Transform.m13 -= rootPosition.y;
                    ci.Transform.m23 -= rootPosition.z;

                    combineInfos.Add(ci);
                }
            }

            MeshCombiner combiner     = new MeshCombiner();
            WorkingMesh  combinedMesh = combiner.CombineMesh(Allocator.Persistent, combineInfos);

            WorkingObject   newObj = new WorkingObject(Allocator.Persistent);
            WorkingMaterial newMat = m_createdMaterials[atlas].Clone();

            combinedMesh.name = info.Name + "_Mesh";
            newObj.Name       = info.Name;
            newObj.SetMesh(combinedMesh);
            newObj.Materials.Add(newMat);

            info.WorkingObjects.Dispose();
            info.WorkingObjects = new DisposableList <WorkingObject>();
            info.WorkingObjects.Add(newObj);
        }