/// <summary>
        /// Convert to data.
        /// </summary>
        public RegionData ToData()
        {
            var data = new RegionData();

            data.GUID = GUID;

            data.LUID = LUID;

            var headAddress       = Head.Address;
            var headAddressLength = headAddress.Count;

            data.HeadAddress = BoctAddressTools.ToString(headAddress);

            if (Head.HasMaterial)
            {
                // If the head has material, the region is a single block.
                data.HeadMaterialId = Head.MaterialId;
            }
            else
            {
                var list = BoctTools.GetSolidBoctArray(Head);

                for (int i = 0; i < list.Length; i++)
                {
                    var b            = list[i];
                    var childAddress = b.Address;
                    // Trim headAddress from child address.
                    var localAddress = childAddress.GetRange(headAddressLength, childAddress.Count - headAddressLength);
                    data.AddressList.Add(BoctAddressTools.ToString(localAddress));
                    data.MaterialIdList.Add(b.MaterialId);
                    data.ExtensionIdList.Add(b.ExtensionId);
                }
            }

            data.HeadExtensionId = Head.ExtensionId;

            return(data);
        }
Exemple #2
0
        public void GenerateMesh(Boct head, GameObject target, string meshName = "")
        {
            var address = head.Address;
            var pos     = BoctAddressTools.GetCoordinate(address);

            #if UNITY_EDITOR
            if (string.IsNullOrEmpty(meshName))
            {
                var cnt = head.SolidCount;
                target.name = BoctAddressTools.ToString(address, true) + "_" + cnt;
            }
            else
            {
                target.name = meshName;
            }
            #endif

            target.transform.localRotation = Quaternion.identity;
            target.transform.localPosition = Vector3.zero;
            target.transform.localScale    = new Vector3(1, 1, 1);

            var mf = target.GetComponent <MeshFilter>();
            if (mf == null)
            {
                mf = target.gameObject.AddComponent <MeshFilter>();
            }

            var mr = target.GetComponent <MeshRenderer>();
            if (mr == null)
            {
                mr = target.gameObject.AddComponent <MeshRenderer>();
            }

            mr.material = BoctMaterial;

            var mesh = new Mesh();
            mesh.name = meshName;

            GenerateMesh(head, address.Count, pos);

            // Convert to arrays.
            mesh.vertices = _vertexList.ToArray();
            mesh.normals  = _normalList.ToArray();
            mesh.colors   = _colorList.ToArray();
            mesh.uv       = _uvList.ToArray();

            int[] indexArray = new int[_vertexList.Count];
            for (int i = 0; i < indexArray.Length; i++)
            {
                indexArray[i] = i;
            }

            mesh.triangles = indexArray;
            mf.mesh        = mesh;

            #if UNITY_EDITOR
            if (SaveMesh)
            {
                var path = MeshPath + "/" + mesh.name + ".asset";
                if (File.Exists(path))
                {
                    AssetDatabase.DeleteAsset(path);
                }
                AssetDatabase.CreateAsset(mesh, MeshPath + "/" + mesh.name + ".asset");
                AssetDatabase.SaveAssets();
            }
            #endif

            if (GenerateCollider)
            {
                var meshCollider = target.GetComponent <MeshCollider>();
                if (meshCollider == null)
                {
                    meshCollider = target.gameObject.AddComponent <MeshCollider>();
                }
                meshCollider.sharedMesh = mesh;
            }

            // Clear lists.
            _vertexList.Clear();
            _normalList.Clear();
            _uvList.Clear();
            _colorList.Clear();
        }