Ejemplo n.º 1
0
 public SerializedChunk(IntVector3 ChunkPos, MapDisplay.LODBuffers DBuffers, uint[] Voxels, Chunk.MetaData metaData)
 {
     this.ChunkPos = ChunkPos;
     this.Voxels   = Voxels;
     this.DBuffers = DBuffers;
     this.metaData = metaData;
 }
Ejemplo n.º 2
0
        void RenderPoints()
        {
            IntVector3 size   = new IntVector3(_chunkSize);
            var        points = GetPoints(size);

            //int bits = (int) Mathf.Log( _chunkSize * _chunkSize * _chunkSize, 8);
            points = points.OrderBy(v => HilbertTables.XYZToHilbertIndex[v.index]).ToList(); // v.v.ToUint3().CoordsToFlatHilbertIndex(bits)).ToList();

            var verts = new List <Vertex>(points.Count);

            for (int i = 0; i < points.Count; i++)
            {
                Vertex a = new Vertex()
                {
                    pos = (Vector3)points[i].v, color = ColorUtil.roygbivMod(i)
                };
                verts.Add(a);
            }
            TestHilbertBackToXYZ(points);
            lsr.Apply(verts.ToArray());

            //cr.Apply(verts);

            var        dbuffers = new MapDisplay.LODBuffers(); // GenerateMapBuffers(points);
            MapDisplay md       = Instantiate(displayPrefab);

            md.transform.SetParent(transform);
            md.transform.localPosition = transform.localPosition + Vector3.right * 10f; // just nudge right a bit
            md.initialize(dbuffers);
            //voxelShader,
            //ColorUtil.roygbiv,
            //spriteTex,
            //dbuffers);
        }
Ejemplo n.º 3
0
 public static void WriteBuffer(MapDisplay.LODBuffers dbuffers, string path)
 {
     for (int i = 0; i < ChunkGenData.LODLevels; ++i)
     {
         WriteBuffAtIndex(dbuffers, path, i);
     }
 }
Ejemplo n.º 4
0
            public static MapDisplay.LODBuffers ReadBufferAt(string path)
            {
                var dbuffers = new MapDisplay.LODBuffers();

                for (int i = 0; i < 3; ++i)
                {
                    dbuffers[i] = CVoxelMapFormat.BufferCountArgs.CreateBuffer(ReadUintsFromFile(FilePathForBuffer(path, i)));
                }
                return(dbuffers);
            }
Ejemplo n.º 5
0
        public MapDisplay.LODBuffers takeDisplayBuffers()
        {
            var lodBuffers = new MapDisplay.LODBuffers();

            lodBuffers.display = SolidVoxels;
            lodBuffers.lod2    = SolidVoxelsLOD2;
            lodBuffers.lod4    = SolidVoxelsLOD4;

            SolidVoxels     = null;
            SolidVoxelsLOD2 = null;
            SolidVoxelsLOD4 = null;
            return(lodBuffers);
        }
Ejemplo n.º 6
0
        MapDisplay GenerateMapDisplay(IntVector3 chunkPos, MapDisplay.LODBuffers dbuffers) // MapDisplay.DisplayBuffers dbuffers)
        {
            GameObject go = (GameObject)Instantiate(displayPrefab);

            go.transform.SetParent(transform);
            go.transform.localPosition = transform.localPosition + Vector3.Scale(chunkPos, vGenConfig.ChunkSize);
            MapDisplay md = go.GetComponent <MapDisplay>();

            md.initialize(
                //geometryShader,
                //ColorUtil.roygbiv,
                //voxelTexture,
                dbuffers,
                vGenConfig);
            return(md);
        }
Ejemplo n.º 7
0
        public static SerializedChunk Read(IntVector3 chunkPos)
        {
            if (!File.Exists(GetFileFullPath(chunkPos)))
            {
                return(null);
            }
            try
            {
                MapDisplay.LODBuffers dbuffers = null;
                Chunk.MetaData        metaData = new Chunk.MetaData()
                {
                    Dirty = true
                };
                metaData = XMLOp.Deserialize <Chunk.MetaData>(GetMetaDataFullPath(chunkPos));

                try
                {
                    dbuffers = SerializedDisplayBuffers.ReadBufferAt(GetDBufferFullPath(chunkPos)); // SerializedDisplayBuffers.Deserialize(GetDBufferFullPath(chunkPos)).ToDisplayBuffers();
                } catch (Exception e)
                {
                    Debug.LogWarning("Excptn while reading dbuffers " + e.ToString());
                    return(null);
                }

                FileStream chunkFile = File.Open(GetFileFullPath(chunkPos), FileMode.Open);
                using (BinaryReader br = new BinaryReader(chunkFile))
                {
                    int    pos    = 0;
                    int    length = (int)br.BaseStream.Length;
                    uint[] voxels = new uint[length / VoxelStorageSize];
                    while (pos * VoxelStorageSize < length)
                    {
                        voxels[pos++] = br.ReadUInt32();
                    }

                    return(new SerializedChunk(chunkPos, dbuffers, voxels, metaData));
                }
            } catch (Exception e)
            {
                Debug.LogWarning("Excptn while reading: " + e.ToString());
                return(null);
            }
        }
Ejemplo n.º 8
0
 static void WriteBuffAtIndex(MapDisplay.LODBuffers dbuffers, string path, int index)
 {
     WriteUintsToFile(CVoxelMapFormat.BufferCountArgs.GetData <uint>(dbuffers[index]), FilePathForBuffer(path, index));
 }
Ejemplo n.º 9
0
        public static void ReadAsync(IntVector3 chunkPos, Action <SerializedChunk> callback)
        {
            /*
             * if (!File.Exists(GetFileFullPath(chunkPos)))
             * {
             *  Debug.Log("No file at" + chunkPos);
             *  callback(null);
             *  return;
             * }
             */
            if (!Chunk.MetaData.SavedChunkExists(chunkPos))
            {
                callback(null);
                return;
            }

            GameObject jcbGO = new GameObject();

            SerializedDisplayBuffers.DeserJob dsbuffJob = default(SerializedDisplayBuffers.DeserJob);
            MapDisplay.LODBuffers             dbuffers  = null;
            try
            {
                Chunk.MetaData metaData = new Chunk.MetaData()
                {
                    Dirty = true
                };
                metaData = XMLOp.Deserialize <Chunk.MetaData>(GetMetaDataFullPath(chunkPos));

                if (metaData.isInvalid)
                {
                    Debug.Log("invalid meta data at: " + chunkPos);
                    callback(null);
                    return;
                }

                dsbuffJob = new SerializedDisplayBuffers.DeserJob();

                var all = Allocator.TempJob;
                dsbuffJob.path  = new NativeArray <byte>(StringToBytes.ToBytes(GetDBufferFullPath(chunkPos)), all);
                dsbuffJob.rLOD0 = new NativeArray <VoxelGeomDataMirror>(metaData.LODBufferLengths[0], all);
                dsbuffJob.rLOD1 = new NativeArray <VoxelGeomDataMirror>(metaData.LODBufferLengths[1], all);
                dsbuffJob.rLOD2 = new NativeArray <VoxelGeomDataMirror>(metaData.LODBufferLengths[2], all);

                var jcb = jcbGO.AddComponent <JobCall>();
                jcb.Schedule(dsbuffJob, () =>
                {
                    dbuffers    = new MapDisplay.LODBuffers();
                    dbuffers[0] = CVoxelMapFormat.BufferCountArgs.CreateBuffer(dsbuffJob.rLOD0);
                    dbuffers[1] = CVoxelMapFormat.BufferCountArgs.CreateBuffer(dsbuffJob.rLOD1);
                    dbuffers[2] = CVoxelMapFormat.BufferCountArgs.CreateBuffer(dsbuffJob.rLOD2);

                    dsbuffJob.DisposeNArrays();


                    uint[] voxelsFakeEmpty = new uint[1];
                    callback(new SerializedChunk(chunkPos, dbuffers, voxelsFakeEmpty, metaData));

                    /*
                     * FileStream chunkFile = File.Open(GetFileFullPath(chunkPos), FileMode.Open);
                     * //
                     * // Maybe read the full voxel array later, as needed
                     * //
                     * using (BinaryReader br = new BinaryReader(chunkFile))
                     * {
                     *  int pos = 0;
                     *  int length = (int)br.BaseStream.Length;
                     *  uint[] voxels = new uint[length / VoxelStorageSize];
                     *  while (pos * VoxelStorageSize < length)
                     *  {
                     *      voxels[pos++] = br.ReadUInt32();
                     *  }
                     *  callback(new SerializedChunk(chunkPos, dbuffers, voxels, metaData));
                     * }
                     */
                    GameObject.Destroy(jcbGO);
                });
            }
            catch (Exception e)
            {
                Debug.LogWarning("Excptn while reading: " + e.ToString());
                dsbuffJob.DisposeNArrays();
                if (dbuffers != null)
                {
                    dbuffers.release();
                }
                GameObject.Destroy(jcbGO);
                callback(null);
            }
        }