Example #1
0
        void LoadChunkFromDisk(Int3 pos)
        {
            var savePath = WorldSavePath;

            using (var file = File.OpenRead(savePath + pos.ToString())) {
                using (var reader = new BinaryReader(file)) {
                    var data = ChunkSerializer.Deserialize(reader);
                    Task.Factory.StartNew(() => { UnpackChunk(pos, data); });
                }
            }
        }
Example #2
0
        void SaveChunkToDisk(Int3 pos, ChunkData data)
        {
            var savePath = WorldSavePath;

            try {
                using (var file = File.Create(savePath + pos.ToString())) {
                    using (var chunkWriter = new BinaryWriter(file)) {
                        ChunkSerializer.Serialize(data, chunkWriter, true);
                    }
                }
            } catch (Exception e) {
                UnityEngine.Debug.LogError(e.ToString());
                _chunkSaveQueue.Enqueue(new KeyValuePair <Int3, ChunkData>(pos, data));                //Если по какой-то причине не получилось сохраниться на диск, то возвращаем чанк в конец очереди.
            }

            _saveData.ChunkLibrary.Add(pos);
        }
Example #3
0
    void DoIt()
    {
        startNode = null;
        endNode   = null;
#if UNITY_EDITOR
        line.ClearLines();
#endif

        Stopwatch watch = new Stopwatch();
        watch.Start();

        fixResult.Clear();
        if (!machine.FindPath(from, to, ref fixResult, TwGame.Team.Neutral))
        {
            UnityEngine.Debug.Log("ms " + watch.ElapsedMilliseconds);
            to = PathFindingMachine.Instance.GetNearestPosition(to);
            bool ret = machine.FindPath(from, to, ref fixResult, TwGame.Team.Neutral);
            if (!ret)
            {
                UnityEngine.Debug.Log("cannot find a path to " + to.ToString());
            }
        }

        if (fixResult.Count > 0)
        {
#if UNITY_EDITOR
            line.ClearLines();
            line.AddLine(fixResult.ToArray(), Color.red);
#endif
        }

        watch.Stop();
        if (fixResult.Count > 0)
        {
            UnityEngine.Debug.Log("ms " + watch.ElapsedMilliseconds);
        }
    }
Example #4
0
    VoxelizedMesh VoxelizeMesh(Transform t, float voxelResolution, int voxelizeLayer)
    {
        // TODO ray cast from right / left and top/ down
        Physics.queriesHitBackfaces = false;

        MeshRenderer mr = t.GetComponent <MeshRenderer>();

        if (mr == null)
        {
            return(null);
        }

        MeshFilter mf = t.GetComponent <MeshFilter>();

        if (mf == null)
        {
            return(null);
        }

        Mesh mesh = mf.sharedMesh;

        if (mesh == null)
        {
            return(null);
        }

        VoxelizedMesh vm = new VoxelizedMesh();

        voxelizedLookup[mesh] = vm;

        Transform  oldParent = t.parent;
        Vector3    oldPos    = t.position;
        Quaternion oldRot    = t.rotation;
        Vector3    oldScale  = t.localScale;

        t.parent     = null;
        t.position   = Vector3.zero;
        t.rotation   = Quaternion.identity;
        t.localScale = Vector3.one;
        int oldLayer = t.gameObject.layer;

        t.gameObject.layer = voxelizeLayer;

        LayerMask voxelizeLayerMask = 1 << voxelizeLayer;

        Bounds bounds = mr.bounds;

        Vector3 size   = bounds.size;
        Int3    voxels = new Int3(Mathf.CeilToInt(size.x / voxelResolution), Mathf.CeilToInt(size.y / voxelResolution), Mathf.CeilToInt(size.z / voxelResolution));

        voxels += new Int3(2, 2, 2);
        int voxelsX = Mathf.CeilToInt(voxels.x / 8f);

        vm.voxels = voxels;

        size        = new Vector3(voxels.x * voxelResolution, voxels.y * voxelResolution, voxels.z * voxelResolution);
        bounds.size = size;
        vm.bounds   = bounds;

        byte[,,] volume = new byte[voxelsX, voxels.y, voxels.z];

        Ray ray  = new Ray();
        Ray ray2 = new Ray();

        ray.direction  = Vector3.forward;
        ray2.direction = Vector3.back;
        Vector3 pos  = bounds.min;
        Vector3 pos2 = pos;

        pos2.z = bounds.max.z;

        Debug.Log(PrintVector3(mr.bounds.size) + " new size " + PrintVector3(size) + " voxels " + voxels.ToString());
        int voxelCount = 0;

        Vector3 halfVoxel      = Vector3.one * voxelResolution * 0.5f;
        Vector3 minBoundsVoxel = pos + halfVoxel;

        // voxels.y = 2;

        try
        {
            for (int x = 0; x < voxels.x; x++)
            {
                int  xGrid = x / 8;
                byte bit   = bits[x - (xGrid * 8)];

                for (int y = 0; y < voxels.y; y++)
                {
                    Vector3 origin = pos + new Vector3((x + 0.5f) * voxelResolution, (y + 0.5f) * voxelResolution, 0);
                    ray.origin  = origin;
                    origin.z    = pos2.z;
                    ray2.origin = origin;

                    intersectList.Clear();

                    MultiCast(ray, intersectList, 0.001f, size.z, voxelizeLayerMask);
                    MultiCast(ray2, intersectList, -0.001f, size.z, voxelizeLayerMask);

                    intersectList.Sort();

                    float half = (float)intersectList.Count / 2;
                    if (half != (int)half)
                    {
                        continue;
                    }

                    // Debug.Log(hitInfos.Length + " " + hitInfos2.Length + " " + list.Count);

                    for (int i = 0; i < intersectList.Count; i += 2)
                    {
                        int z1 = Mathf.RoundToInt((intersectList[i] - pos.z) / voxelResolution);
                        int z2 = Mathf.RoundToInt((intersectList[i + 1] - pos.z) / voxelResolution);

                        for (int z = z1; z < z2; z++)
                        {
                            Vector3 voxelPos = new Vector3(x * voxelResolution, y * voxelResolution, z * voxelResolution) + minBoundsVoxel;
                            voxelPos = t.TransformPoint(voxelPos);

                            volume[xGrid, y, z] |= bit;
                            ++voxelCount;
                            //if (!Physics.CheckBox(voxelPos, halfVoxel, Quaternion.identity, voxelizeLayerMask))
                            //{
                            //    volume[xGrid, y, z] |= bit;
                            //    ++voxelCount;
                            //}
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            Debug.LogError(e.ToString());
        }

        Debug.Log(t.name + " voxels " + voxelCount);

        vm.volume = volume;

        t.gameObject.layer = oldLayer;
        t.parent           = oldParent;
        t.position         = oldPos;
        t.rotation         = oldRot;
        t.localScale       = oldScale;

        return(vm);
    }