public void Split(VGenConfig vGenConfig, int minSplit = 2, float areaTolerance = .9f) { Queue <SubChunkRange> subChunksRanges = new Queue <SubChunkRange>(); subChunksRanges.Enqueue(new SubChunkRange() { voxels = storage, subChunkSize = new IntBounds3() { start = new IntVector3(0, 0, 0), size = vGenConfig.ChunkSize } }); bool splitOnce = false; while (subChunksRanges.Count > 0) { SubChunkRange subChunkRange = subChunksRanges.Dequeue(); if (splitOnce && subChunkRange.subChunkSize.size.SurfaceArea / (Mathf.Pow(vGenConfig.Mip64Divisor, 3) * 2) * areaTolerance < subChunkRange.voxels.Count) { ranges.Add(subChunkRange.voxels); } else { if (subChunkRange.subChunkSize.size.MinComponent <= minSplit) { Debug.Log("hit min: " + subChunkRange.subChunkSize.size.MinComponent); continue; } List <IntVoxel3>[] octants = new List <IntVoxel3> [8]; for (int i = 0; i < subChunkRange.voxels.Count; ++i) { int octantIndex = subChunkRange.subChunkSize.OctanctIndexOf(subChunkRange.voxels[i].intVector3); if (octants[octantIndex] == null) { octants[octantIndex] = new List <IntVoxel3>(); } octants[octantIndex].Add(subChunkRange.voxels[i]); } for (int i = 0; i < 8; ++i) { if (octants[i] != null) { subChunksRanges.Enqueue(new SubChunkRange() { voxels = octants[i], subChunkSize = new IntBounds3() { start = subChunkRange.subChunkSize.start + subChunkRange.subChunkSize.OffsetForOctantIndex(i), size = subChunkRange.subChunkSize.size / 2 } }); } } } splitOnce = true; } }
public CVoxelMapData(ComputeShader shader, VGenConfig vGenConfig) { perlinGen = shader; this.vGenConfig = vGenConfig; initKernels(); initTemporaryBuffers(); }
// Kernel fills ShownVoxels // Face copy kernel can run in a group (of size ShownVoxels.count) // And use an interlocked increment #region init public CVoxelNeighborFormat(ComputeShader neighborFormat, VGenConfig vGenConfig) { this.neighborFormat = neighborFormat; this.vGenConfig = vGenConfig; InitKernels(); initBuffers(); SetKernelBuffers(); }
public CVoxelFaceCopy(ComputeShader shader, CVoxelNeighborFormat cvoxelNeighborFormat, VGenConfig vGenConfig) { faceCopy = shader; this.vGenConfig = vGenConfig; this.cvoxelNeighborFormat = cvoxelNeighborFormat; initKernels(); initTemporaryBuffers(); setTemporaryBuffers(); }
public CVoxelMapFormat(ComputeShader shader, ComputeShader hilbertIndexShader, CVoxelMapData data, VGenConfig vGenConfig) { meshGen = shader; this.vGenConfig = vGenConfig; initKernels(); initTemporaryBuffers(); voxelMapData = data; setTemporaryBuffers(); SolidVoxels = null; }
public static List <IntVoxel3> PackedVoxelsToIndexedIntVoxel(uint[] uis, VGenConfig vGenConfig) { var points = new List <IntVoxel3>(uis.Length * 4); for (int i = 0; i < uis.Length; ++i) { for (int j = 0; j < 4; ++j) { IntVoxel3 ivox = IntVoxel3.FromPackedVoxel(i, j, uis[i], vGenConfig); points.Add(ivox); } } return(points); }
//private static bool getVoxel(int x, int y, int z, VGenConfig vGenConfig, List<IntVoxel3> storage, out IntVoxel3 voxi) //{ // int index = IntVector3.FlatXYZIndex(x, y, z, vGenConfig.ChunkSize); // if(index >= storage.Count) // { // voxi = new IntVoxel3(0,0); // return false; // } // voxi = storage[index]; // return true; //} public static uint[] ExposedVoxels(uint[] foundVoxels, VGenConfig vGenConfig) { var iVoxels = PackedVoxelsToIndexedIntVoxel(foundVoxels, vGenConfig); var exposedVoxels = new List <uint>(); int[] sideCheckIncrs = new int[] { 1, -1, // y vGenConfig.ChunkSizeY, -vGenConfig.ChunkSizeY, // x vGenConfig.ChunkSizeY *vGenConfig.ChunkSizeX, -vGenConfig.ChunkSizeY * vGenConfig.ChunkSizeX, }; for (int i = 0; i < iVoxels.Count; ++i) { var voxi = iVoxels[i]; if (voxi.voxelType < 1) { continue; } if (voxi.intVector3.IsOnAFace(vGenConfig.ChunkSize)) { exposedVoxels.Add((uint)voxi.voxel); continue; } int DBUGWithinStorageCount = 0; foreach (int incr in sideCheckIncrs) { int sideIndex = i + incr; if (sideIndex < 0 || sideIndex >= iVoxels.Count) { continue; } var sideVoxi = iVoxels[sideIndex]; DBUGWithinStorageCount++; if (sideVoxi.voxelType == 0) { Debug.Log("found one"); exposedVoxels.Add((uint)voxi.voxel); break; } } Debug.Log("within storage: " + DBUGWithinStorageCount); } return(exposedVoxels.ToArray()); }
public void Init( ComputeShader shader, ComputeBuffer _display, ComputeBuffer ledger, Vector3 chunkGlobalPos, VGenConfig vGenConfig) { this.shader = shader; this._display = _display; this.ledger = ledger; this.chunkGlobalPos = chunkGlobalPos; this.buffResolution = vGenConfig.ReverseCastBufferResolutionXY; this.vGenConfig = vGenConfig; reverseCastKernel = shader.FindKernel("ReverseCast"); InitShader(); }
public TargetCenteredChunkBounds(Transform target, IntVector3 size, IntVector3 boundsSize, VGenConfig vg, Action moveMethod = null) { this.target = target; this.size = size; vGenConfig = vg; iterator = new ProximityIterator3(size, boundsSize); if (moveMethod == null) { this.moveMethod = () => { target.position += new IntVector3(size.x * boundsSize.x, 0, 0).ToVector3(); }; } else { this.moveMethod = moveMethod; } }
uint GetVoxelFromGenDataArray(NativeArray <VoxelGenDataMirror> data, IntVector3 posOverDiv) { int opI; if (lodIndex == 0) { opI = posOverDiv.ToFlatZXYIndex(chunkSize); int opMod = opI % voxelsPerMapData; opI /= voxelsPerMapData; uint voxels = data[opI].voxel; return((uint)VGenConfig.DecodeMapGenType(voxels, opMod)); } else { int div = (int)Mathf.Pow(2, lodIndex); opI = (posOverDiv).ToFlatXYZIndex(chunkSize / div); return((uint)VGenConfig.DecodeVoxelType(data[opI].voxel)); //This is a type-x-y-z encoded voxel if set. if not, its just EmptyVoxel } }
public void initialize( ComputeBuffer buffer, VGenConfig vGenConfig = null ) { this.buffer = buffer; globalLight = GameObject.Find("GlobalLight").transform; this.vGenConfig = vGenConfig; if (!this.vGenConfig) { this.vGenConfig = FindObjectOfType <VGenConfig>(); } bounds = new Bounds( transform.position + this.vGenConfig.ChunkSize / 2f * size, this.vGenConfig.ChunkSize * size); material = new Material(geometryShader); setMaterialConstants(); enabled = true; }
public static bool TestDisplayBufferFromPerlinGen(uint[] uis, out MapDisplay.DisplayBuffers displayBuffers, VGenConfig vGenConfig) { List <IntVector3.IndexedIntVector3> points; if (!PackedVoxelsToIndexedIntVector3s(uis, out points, vGenConfig)) { displayBuffers = new MapDisplay.DisplayBuffers(); return(false); } displayBuffers = GenTestBuffers(vGenConfig.ChunkDimension, points); return(true); }
public static bool PackedVoxelsToIndexedIntVector3s(uint[] uis, out List <IntVector3.IndexedIntVector3> points, VGenConfig vGenConfig) { points = new List <IntVector3.IndexedIntVector3>(uis.Length * 4); for (int i = 0; i < uis.Length; ++i) { for (int j = 0; j < 4; ++j) { // PGen encodes voxel position by index: z, x, y (y least significant). // Voxel values encoded in 8 bits, packed 4 per uint. int voxel = ((int)uis[i] >> (8 * j)) & 0xFF; if (voxel < 1) { continue; } IntVector3 v = new IntVector3(); int vi = i * 4 + j; v.z = vi / (vGenConfig.ChunkSizeY * vGenConfig.ChunkSizeX); v.x = (vi % (vGenConfig.ChunkSizeZ * vGenConfig.ChunkSizeY)) / vGenConfig.ChunkSizeY; v.y = vi % vGenConfig.ChunkSizeY; // ?? (vGenConfig.ChunkSizeZ * vGenConfig.ChunkSizeX); points.Add(new IntVector3.IndexedIntVector3() { index = i, v = v }); } } return(points.Count > 0); }
public static IntVoxel3 FromPackedVoxel(int i, int bytePosition, uint fourVoxels, VGenConfig vGenConfig) { // PGen encodes voxel position by index: z, x, y (y least significant). // Voxel values encoded in 8 bits, packed 4 per uint. int vi = i * 4 + bytePosition; int z = vi / (vGenConfig.ChunkSizeY * vGenConfig.ChunkSizeX); int x = (vi % (vGenConfig.ChunkSizeZ * vGenConfig.ChunkSizeY)) / vGenConfig.ChunkSizeY; int y = vi % vGenConfig.ChunkSizeY; // ?? (vGenConfig.ChunkSizeZ * vGenConfig.ChunkSizeX); int voxel = ((((int)fourVoxels >> (8 * bytePosition)) & 0xFF) << 24) | ((x & 0xFF) << 16) | ((y & 0xFF) << 8) | (z & 0xFF); return(new IntVoxel3(voxel, vi)); }
private void Start() { vGenConfig = FindObjectOfType <VGenConfig>(); FakeInit(); }