public void GenerateMesh(Voxel[] voxels, float voxelSize,float overlapAmount, int xOffset, int yOffset, int zOffset, int xSize, int ySize, int zSize, int ub0, int ub1, int ub2, float selfShadeIntensity, MeshingMode mode, MeshingMode colliderMode, bool immediate) #endif { if (mf == null) mf = GetComponent<MeshFilter>(); if (mc == null) mc = GetComponent<MeshCollider>(); if (immediate) { #if UNITY_EDITOR Generate(ref voxels, voxelSize, overlapAmount, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity, mode, paintMode); #else Generate(ref voxels, voxelSize, overlapAmount, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity, mode); #endif SetMesh(); if (colliderMode != mode &&transform.parent.parent.parent.GetComponent<Volume>().CollisionMode != CollisionMode.None) { #if UNITY_EDITOR Generate(ref voxels, voxelSize, overlapAmount, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity, mode, paintMode); #else Generate(ref voxels, voxelSize, overlapAmount, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity, mode); #endif } if (transform.parent.parent.parent.GetComponent<Volume>().CollisionMode != CollisionMode.None) SetColliderMesh(); } else { if (status != ChunkStatus.NoChange) return; #if UNITY_WINRT && !UNITY_EDITOR System.Threading.Tasks.Task.Run(() => { GenerateThreaded(ref voxels, voxelSize, xOffset, yOffset, zOffset, xSize, ySize, zSize,ub0, ub1, ub2, selfShadeIntensity, mode); }); #else #if UNITY_EDITOR ThreadPool.QueueUserWorkItem(delegate { GenerateThreaded(ref voxels, voxelSize, overlapAmount, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity, mode, paintMode); }); #else ThreadPool.QueueUserWorkItem(delegate { GenerateThreaded(ref voxels, voxelSize, overlapAmount, xOffset, yOffset, zOffset, xSize, ySize, zSize,ub0, ub1, ub2, selfShadeIntensity, mode); }); #endif #endif } }
public void SpawnSingle(Vector3 worldPos, Voxel voxel, float voxelSize, Vector3 velocity) { System.Emit(new ParticleSystem.EmitParams() { position = worldPos, velocity = velocity, startSize = voxelSize, startLifetime = ParticleLifetime, startColor = voxel.Color }, 1); //worldPos, velocity, voxelSize, ParticleLifetime, voxel.Color)}; }
// Use DetectCollision to detect hits manually public bool DetectCollision(Vector3 worldPos, out Voxel voxel, out Volume hitObject) { foreach (GameObject o in GameObject.FindGameObjectsWithTag("PicaVoxelObject")) { Volume pvo = o.GetComponent<Volume>(); Voxel? pv = pvo.GetVoxelAtWorldPosition(worldPos); if (pv.HasValue && pv.Value.Active) { hitObject = pvo; voxel = pv.Value; return true; } } hitObject = null; voxel = new Voxel(); return false; }
/// <summary> /// Attempts to set a voxel within this frame, at a specified x,y,z array position /// </summary> /// <param name="x">X array position</param> /// <param name="y">Y array position</param> /// <param name="z">Z array position</param> /// <param name="vox">The new voxel to set to</param> public void SetVoxelAtArrayPosition(int x, int y, int z, Voxel vox) { if (x < 0 || y < 0 || z < 0 || x >= XSize || y >= YSize || z >= ZSize) return; bool updateVox = false; int index = x + XSize*(y + YSize*z); #if UNITY_EDITOR if (EditingVoxels != null) { EditingVoxels[index] = vox; updateVox = true; } else { #endif if (vox.Active != Voxels[index].Active) updateVox = true; if (Voxels[index].Active && (vox.Color.r != Voxels[index].Color.r || vox.Color.g!=Voxels[index].Color.g || vox.Color.b!=Voxels[index].Color.b || vox.Value != Voxels[index].Value)) updateVox = true; Voxels[index] = vox; #if UNITY_EDITOR } #endif if (!updateVox) return; if(chunks==null) GetChunkReferences(); chunks[x / ParentVolume.XChunkSize, y / ParentVolume.YChunkSize, z / ParentVolume.ZChunkSize].IsUpdated = true; // If we're at the edge of a chunk, we should update the next voxel as well if (x % ParentVolume.XChunkSize == 0 && (x - 1) >=0) chunks[(x - 1) / ParentVolume.XChunkSize, y / ParentVolume.YChunkSize, z / ParentVolume.ZChunkSize].IsUpdated = true; if (x % ParentVolume.XChunkSize == ParentVolume.XChunkSize - 1 && (x + 1) < XSize) chunks[(x + 1) / ParentVolume.XChunkSize, y / ParentVolume.YChunkSize, z / ParentVolume.ZChunkSize].IsUpdated = true; if (y % ParentVolume.YChunkSize == 0 && (y - 1) >= 0) chunks[x / ParentVolume.XChunkSize, (y - 1) / ParentVolume.YChunkSize, z / ParentVolume.ZChunkSize].IsUpdated = true; if (y % ParentVolume.YChunkSize == ParentVolume.YChunkSize - 1 && (y + 1) < YSize) chunks[x / ParentVolume.XChunkSize, (y + 1) / ParentVolume.YChunkSize, z / ParentVolume.ZChunkSize].IsUpdated = true; if (z % ParentVolume.ZChunkSize == 0 && (z - 1) >= 0) chunks[x / ParentVolume.XChunkSize, y / ParentVolume.YChunkSize, (z - 1) / ParentVolume.ZChunkSize].IsUpdated = true; if (z % ParentVolume.ZChunkSize == ParentVolume.ZChunkSize - 1 && (z + 1) < ZSize) chunks[x / ParentVolume.XChunkSize, y / ParentVolume.YChunkSize, (z + 1) / ParentVolume.ZChunkSize].IsUpdated = true; }
/// <summary> /// Scroll voxels along Z axis /// </summary> /// <param name="amount">The amount of voxels to scroll by</param> /// This shouldn't really be used at runtime public void ScrollZ(int amount) { Voxel[] tempVoxels = new Voxel[XSize* YSize* ZSize]; int reverse = -amount; if (amount < 0) { Helper.CopyVoxelsInBox(ref Voxels, ref tempVoxels, new PicaVoxelBox(0, 0, 0, XSize - 1, YSize - 1, reverse - 1), new PicaVoxelBox(0, 0, 0, XSize - 1, YSize - 1, reverse - 1), new PicaVoxelPoint(XSize, YSize, ZSize), new PicaVoxelPoint(XSize, YSize, ZSize), false); Helper.CopyVoxelsInBox(ref Voxels, ref Voxels, new PicaVoxelBox(0, 0, reverse, XSize - 1, YSize - 1, ZSize - 1), new PicaVoxelBox(0, 0, 0, XSize - 1, YSize - 1, ZSize - reverse - 1), new PicaVoxelPoint(XSize, YSize, ZSize), new PicaVoxelPoint(XSize, YSize, ZSize), false); Helper.CopyVoxelsInBox(ref tempVoxels, ref Voxels, new PicaVoxelBox(0, 0, 0, XSize - 1, YSize - 1, reverse - 1), new PicaVoxelBox(0, 0, ZSize - reverse, XSize - 1, YSize - 1, ZSize - 1), new PicaVoxelPoint(XSize, YSize, ZSize), new PicaVoxelPoint(XSize, YSize, ZSize), false); } else { Helper.CopyVoxelsInBox(ref Voxels, ref tempVoxels, new PicaVoxelBox(0, 0, 0, XSize - 1, YSize - 1, ZSize - 1 - amount), new PicaVoxelBox(0, 0, 0, XSize - 1, YSize - 1, ZSize - 1 - amount), new PicaVoxelPoint(XSize, YSize, ZSize), new PicaVoxelPoint(XSize, YSize, ZSize), false); Helper.CopyVoxelsInBox(ref Voxels, ref Voxels, new PicaVoxelBox(0, 0, ZSize - amount, XSize - 1, YSize - 1, ZSize - 1), new PicaVoxelBox(0, 0, 0, XSize - 1, YSize - 1, amount - 1), new PicaVoxelPoint(XSize, YSize, ZSize), new PicaVoxelPoint(XSize, YSize, ZSize), false); Helper.CopyVoxelsInBox(ref tempVoxels, ref Voxels, new PicaVoxelBox(0, 0, 0, XSize - 1, YSize - 1, ZSize - 1 - amount), new PicaVoxelBox(0, 0, amount, XSize - 1, YSize - 1, ZSize - 1), new PicaVoxelPoint(XSize, YSize, ZSize), new PicaVoxelPoint(XSize, YSize, ZSize), false); } UpdateAllChunks(); SaveForSerialize(); }
private void NudgeSelection(int dx, int dy, int dz) { PicaVoxelBox dest = new PicaVoxelBox(EditorPersistence.SelectBox.BottomLeftFront.X+dx, EditorPersistence.SelectBox.BottomLeftFront.Y+dy, EditorPersistence.SelectBox.BottomLeftFront.Z+dz, EditorPersistence.SelectBox.TopRightBack.X+dx, EditorPersistence.SelectBox.TopRightBack.Y+dy, EditorPersistence.SelectBox.TopRightBack.Z+dz); if (dest.BottomLeftFront.X < 0 || dest.BottomLeftFront.Y < 0 || dest.BottomLeftFront.Z < 0 || dest.TopRightBack.X >= voxelObject.XSize || dest.TopRightBack.Y >= voxelObject.YSize || dest.TopRightBack.Z >= voxelObject.ZSize) return; int destWidth = (dest.TopRightBack.X - dest.BottomLeftFront.X) +1; int destHeight = (dest.TopRightBack.Y - dest.BottomLeftFront.Y)+1; int destDepth = (dest.TopRightBack.Z - dest.BottomLeftFront.Z)+1; Voxel[] tempVox = new Voxel[destWidth *destHeight * destDepth]; RegisterUndo(); if (propogateAllFrames) { foreach (Frame f in voxelObject.Frames) { Helper.CopyVoxelsInBox(ref f.Voxels, ref tempVox, EditorPersistence.SelectBox, new PicaVoxelBox(0, 0, 0, destWidth - 1, destHeight - 1, destDepth - 1), new PicaVoxelPoint(voxelObject.XSize, voxelObject.YSize, voxelObject.ZSize), new PicaVoxelPoint(destWidth, destHeight, destDepth), false); f.EditingVoxels = null; for (int x = EditorPersistence.SelectBox.BottomLeftFront.X; x <= EditorPersistence.SelectBox.TopRightBack.X; x++) for (int y = EditorPersistence.SelectBox.BottomLeftFront.Y; y <= EditorPersistence.SelectBox.TopRightBack.Y; y++) for (int z = EditorPersistence.SelectBox.BottomLeftFront.Z; z <= EditorPersistence.SelectBox.TopRightBack.Z; z++) f.Voxels[x + voxelObject.XSize * (y + voxelObject.YSize * z)] = new Voxel() { State = VoxelState.Inactive, Color = voxelObject.PaletteColors[EditorPersistence.SelectedColor], Value = EditorPersistence.SelectedValue }; Helper.CopyVoxelsInBox(ref tempVox, ref f.Voxels, new PicaVoxelBox(0, 0, 0, destWidth - 1, destHeight - 1, destDepth - 1), dest, new PicaVoxelPoint(destWidth, destHeight, destDepth), new PicaVoxelPoint(voxelObject.XSize, voxelObject.YSize, voxelObject.ZSize), false); f.SaveForSerialize(); f.UpdateAllChunks(); } EditorPersistence.SelectBox = dest; } else { Helper.CopyVoxelsInBox(ref voxelObject.Frames[voxelObject.CurrentFrame].Voxels, ref tempVox, EditorPersistence.SelectBox, new PicaVoxelBox(0, 0, 0, destWidth-1, destHeight-1, destDepth-1), new PicaVoxelPoint(voxelObject.XSize, voxelObject.YSize, voxelObject.ZSize), new PicaVoxelPoint(destWidth, destHeight, destDepth), false); voxelObject.Frames[voxelObject.CurrentFrame].EditingVoxels = null; for (int x = EditorPersistence.SelectBox.BottomLeftFront.X; x <= EditorPersistence.SelectBox.TopRightBack.X; x++) for (int y = EditorPersistence.SelectBox.BottomLeftFront.Y; y <= EditorPersistence.SelectBox.TopRightBack.Y; y++) for (int z = EditorPersistence.SelectBox.BottomLeftFront.Z; z <= EditorPersistence.SelectBox.TopRightBack.Z; z++) voxelObject.Frames[voxelObject.CurrentFrame].Voxels[x + voxelObject.XSize * (y + voxelObject.YSize * z)] = new Voxel() { State = VoxelState.Inactive, Color = voxelObject.PaletteColors[EditorPersistence.SelectedColor], Value = EditorPersistence.SelectedValue }; Helper.CopyVoxelsInBox(ref tempVox, ref voxelObject.Frames[voxelObject.CurrentFrame].Voxels, new PicaVoxelBox(0, 0, 0, destWidth - 1, destHeight - 1, destDepth - 1), dest, new PicaVoxelPoint(destWidth, destHeight, destDepth), new PicaVoxelPoint(voxelObject.XSize, voxelObject.YSize, voxelObject.ZSize), false); EditorPersistence.SelectBox = dest; voxelObject.Frames[voxelObject.CurrentFrame].SaveForSerialize(); voxelObject.Frames[voxelObject.CurrentFrame].UpdateAllChunks(); } }
public BatchVoxel(Voxel voxel, PicaVoxelPoint arrayPoint, Vector3 worldPos) { Voxel = voxel; ArrayPosition = arrayPoint; WorldPosition = worldPos; }
public static void GenerateMarching(List<Vector3> vertices, List<Vector2> uvs, List<Color32> colors, List<int> indexes, ref Vector3[] vertArray, ref Vector2[] uvArray, ref Color32[] colorArray, ref int[] indexArray, ref Voxel[] invoxels, float voxelSize, int xOffset, int yOffset, int zOffset, int xSize, int ySize, int zSize, int ub0, int ub1, int ub2, float selfShadeIntensity) #endif { vertices.Clear(); uvs.Clear(); colors.Clear(); indexes.Clear(); bool[] corners = new bool[8]; Vector3[] positions = new Vector3[8]; int doubleXsize = xSize * 2; int doubleYsize = ySize * 2; int doubleZsize = zSize * 2; for (int subz = 0; subz < doubleZsize; subz++) for (int suby = 0; suby < doubleYsize; suby++) for (int subx = 0; subx < doubleXsize; subx++) { int cubeIndex = 0; corners[0] = IsVoxelAtMarching(ref invoxels, subx, suby, subz + 1, xOffset, yOffset, zOffset, ub0, ub1, ub2); corners[1] = IsVoxelAtMarching(ref invoxels, subx + 1, suby, subz + 1, xOffset, yOffset, zOffset, ub0, ub1, ub2); corners[2] = IsVoxelAtMarching(ref invoxels, subx + 1, suby, subz, xOffset, yOffset, zOffset, ub0, ub1, ub2); corners[3] = IsVoxelAtMarching(ref invoxels, subx, suby, subz, xOffset, yOffset, zOffset, ub0, ub1, ub2); corners[4] = IsVoxelAtMarching(ref invoxels, subx, suby + 1, subz + 1, xOffset, yOffset, zOffset, ub0, ub1, ub2); corners[5] = IsVoxelAtMarching(ref invoxels, subx + 1, suby + 1, subz + 1, xOffset, yOffset, zOffset, ub0, ub1, ub2); corners[6] = IsVoxelAtMarching(ref invoxels, subx + 1, suby + 1, subz, xOffset, yOffset, zOffset, ub0, ub1, ub2); corners[7] = IsVoxelAtMarching(ref invoxels, subx, suby + 1, subz, xOffset, yOffset, zOffset, ub0, ub1, ub2); if (corners[0]) cubeIndex += 1; if (corners[1]) cubeIndex += 2; if (corners[2]) cubeIndex += 4; if (corners[3]) cubeIndex += 8; if (corners[4]) cubeIndex += 16; if (corners[5]) cubeIndex += 32; if (corners[6]) cubeIndex += 64; if (corners[7]) cubeIndex += 128; if (cubeIndex == 0 || cubeIndex == 255) continue; Vector3 worldOffset = (new Vector3(subx, suby, subz));// + (Vector3.one * hs); positions[0] = worldOffset + new Vector3(0, 0, 1); positions[1] = worldOffset + new Vector3(1, 0, 1); positions[2] = worldOffset + new Vector3(1, 0, 0); positions[3] = worldOffset + new Vector3(0, 0, 0); positions[4] = worldOffset + new Vector3(0, 1, 1); positions[5] = worldOffset + new Vector3(1, 1, 1); positions[6] = worldOffset + new Vector3(1, 1, 0); positions[7] = worldOffset + new Vector3(0, 1, 0); Vector3[] vertlist = new Vector3[12]; if (IsBitSet(edgeTable[cubeIndex], 1)) vertlist[0] = VertexInterp(positions[0], positions[1], corners[0], corners[1]); if (IsBitSet(edgeTable[cubeIndex], 2)) vertlist[1] = VertexInterp(positions[1], positions[2], corners[1], corners[2]); if (IsBitSet(edgeTable[cubeIndex], 4)) vertlist[2] = VertexInterp(positions[2], positions[3], corners[2], corners[3]); if (IsBitSet(edgeTable[cubeIndex], 8)) vertlist[3] = VertexInterp(positions[3], positions[0], corners[3], corners[0]); if (IsBitSet(edgeTable[cubeIndex], 16)) vertlist[4] = VertexInterp(positions[4], positions[5], corners[4], corners[5]); if (IsBitSet(edgeTable[cubeIndex], 32)) vertlist[5] = VertexInterp(positions[5], positions[6], corners[5], corners[6]); if (IsBitSet(edgeTable[cubeIndex], 64)) vertlist[6] = VertexInterp(positions[6], positions[7], corners[6], corners[7]); if (IsBitSet(edgeTable[cubeIndex], 128)) vertlist[7] = VertexInterp(positions[7], positions[4], corners[7], corners[4]); if (IsBitSet(edgeTable[cubeIndex], 256)) vertlist[8] = VertexInterp(positions[0], positions[4], corners[0], corners[4]); if (IsBitSet(edgeTable[cubeIndex], 512)) vertlist[9] = VertexInterp(positions[1], positions[5], corners[1], corners[5]); if (IsBitSet(edgeTable[cubeIndex], 1024)) vertlist[10] = VertexInterp(positions[2], positions[6], corners[2], corners[6]); if (IsBitSet(edgeTable[cubeIndex], 2048)) vertlist[11] = VertexInterp(positions[3], positions[7], corners[3], corners[7]); for (int i = 0; triTable[cubeIndex][i] != -1; i += 3) { int index = vertices.Count; vertices.Add(vertlist[triTable[cubeIndex][i]] * voxelSize * 0.5f); vertices.Add(vertlist[triTable[cubeIndex][i + 1]] * voxelSize * 0.5f); vertices.Add(vertlist[triTable[cubeIndex][i + 2]] * voxelSize * 0.5f); indexes.Add(index + 2); indexes.Add(index + 1); indexes.Add(index + 0); // Convert to x,y,z position int x = Mathf.FloorToInt(((float)subx) / 2.0f); int y = Mathf.FloorToInt(((float)suby) / 2.0f); int z = Mathf.FloorToInt(((float)subz) / 2.0f); x = Mathf.Max(x, 0); y = Mathf.Max(y, 0); z = Mathf.Max(z, 0); Color useColor = Color.red; try { if (invoxels[(x + xOffset) + (ub0 + 1)*((y + yOffset) + (ub1 + 1)*(z + zOffset))].Active) useColor = invoxels[(x + xOffset) + (ub0 + 1)*((y + yOffset) + (ub1 + 1)*(z + zOffset))] .Color; else if (x + xOffset < ub0 && invoxels[ (x + 1 + xOffset) + (ub0 + 1)*((y + yOffset) + (ub1 + 1)*(z + zOffset)) ].Active) useColor = invoxels[ (x + 1 + xOffset) + (ub0 + 1)*((y + yOffset) + (ub1 + 1)*(z + zOffset))] .Color; else if (y + yOffset < ub1 && invoxels[ (x + xOffset) + (ub0 + 1)*((y + 1 + yOffset) + (ub1 + 1)*(z + zOffset))] .Active) useColor = invoxels[ (x + xOffset) + (ub0 + 1)*((y + 1 + yOffset) + (ub1 + 1)*(z + zOffset))].Color; else if (z + zOffset < ub2 && invoxels[ (x + xOffset) + (ub0 + 1)*((y + yOffset) + (ub1 + 1)*(z + 1 + zOffset))].Active) useColor = invoxels[ (x + xOffset) + (ub0 + 1)*((y + yOffset) + (ub1 + 1)*(z + 1 + zOffset))].Color; //else if (x + xOffset >0 && invoxels[((x - 1) + xOffset) + (ub0 + 1) * ((y + yOffset) + (ub1 + 1) * (z + zOffset))].Active) // useColor = invoxels[((x - 1) + xOffset) + (ub0 + 1) * ((y + yOffset) + (ub1 + 1) * (z + zOffset))].Color; //else if (y + yOffset >0 && invoxels[(x + xOffset) + (ub0 + 1) * (((y - 1) + yOffset) + (ub1 + 1) * (z + zOffset))].Active) // useColor = invoxels[(x + xOffset) + (ub0 + 1) * (((y - 1) + yOffset) + (ub1 + 1) * (z + zOffset))].Color; //else if (z + zOffset >0 && invoxels[(x + xOffset) + (ub0 + 1) * ((y + yOffset) + (ub1 + 1) * ((z - 1) + zOffset))].Active) // useColor = invoxels[(x + xOffset) + (ub0 + 1) * ((y + yOffset) + (ub1 + 1) * ((z - 1) + zOffset))].Color; else if (x + xOffset < ub0 && y + yOffset < ub1 && invoxels[ (x + 1 + xOffset) + (ub0 + 1)*((y + 1 + yOffset) + (ub1 + 1)*(z + zOffset))].Active) useColor = invoxels[ (x + 1 + xOffset) + (ub0 + 1)*((y + 1 + yOffset) + (ub1 + 1)*(z + zOffset))] .Color; else if (x + xOffset < ub0 && z + zOffset < ub2 && invoxels[ (x + 1 + xOffset) + (ub0 + 1)*((y + yOffset) + (ub1 + 1)*(z + 1 + zOffset))] .Active) useColor = invoxels[ (x + 1 + xOffset) + (ub0 + 1)*((y + yOffset) + (ub1 + 1)*(z + 1 + zOffset))] .Color; else if (y + yOffset < ub1 && z + zOffset < ub2 && invoxels[ (x + xOffset) + (ub0 + 1)* ((y + 1 + yOffset) + (ub1 + 1)*(z + 1 + zOffset))] .Active) useColor = invoxels[ (x + xOffset) + (ub0 + 1)* ((y + 1 + yOffset) + (ub1 + 1)*(z + 1 + zOffset))] .Color; //else if (x + xOffset > 0 && y + yOffset > 0 && invoxels[((x - 1) + xOffset) + (ub0 + 1) * (((y - 1) + yOffset) + (ub1 + 1) * (z + zOffset))].Active) // useColor = invoxels[((x - 1) + xOffset) + (ub0 + 1) * (((y - 1) + yOffset) + (ub1 + 1) * (z + zOffset))].Color; //else if (x + xOffset > 0 && z + zOffset > 0 && invoxels[((x - 1) + xOffset) + (ub0 + 1) * ((y + yOffset) + (ub1 + 1) * ((z - 1) + zOffset))].Active) // useColor = invoxels[((x - 1) + xOffset) + (ub0 + 1) * ((y + yOffset) + (ub1 + 1) * ((z - 1) + zOffset))].Color; //else if (y + yOffset >0 && z + zOffset >0 && invoxels[(x + xOffset) + (ub0 + 1) * (((y - 1) + yOffset) + (ub1 + 1) * ((z - 1) + zOffset))].Active) // useColor = invoxels[(x + xOffset) + (ub0 + 1) * (((y - 1) + yOffset) + (ub1 + 1) * ((z - 1) + zOffset))].Color; else if (x + xOffset < ub0 && y + yOffset < ub1 && z + zOffset < ub2 && invoxels[ (x + 1 + xOffset) + (ub0 + 1)* ((y + 1 + yOffset) + (ub1 + 1)*(z + 1 + zOffset))] .Active) useColor = invoxels[ (x + 1 + xOffset) + (ub0 + 1)* ((y + 1 + yOffset) + (ub1 + 1)*(z + 1 + zOffset)) ].Color; } catch (Exception) { Debug.Log("x: " + x + " y: " + y + " z: " + z + " ub0: " + ub0 + " ub1: " + ub1 + " ub2: " +ub2); } colors.Add(useColor); colors.Add(useColor); colors.Add(useColor); //colors.Add(Color.white); //colors.Add(Color.white); //colors.Add(Color.white); uvs.Add(Vector2.zero); uvs.Add(Vector2.zero); uvs.Add(Vector2.zero); } } vertArray = vertices.ToArray(); uvArray = uvs.ToArray(); colorArray = colors.ToArray(); indexArray = indexes.ToArray(); }
public static void GenerateGreedy(List<Vector3> vertices, List<Vector2> uvs, List<Color32> colors, List<int> indexes, ref Vector3[] vertArray, ref Vector2[] uvArray, ref Color32[] colorArray, ref int[] indexArray, ref Voxel[] invoxels, float voxelSize,float overlapAmount, int xOffset, int yOffset, int zOffset, int xSize, int ySize, int zSize, int ub0, int ub1, int ub2, float selfShadeIntensity) #endif { vertices.Clear(); uvs.Clear(); colors.Clear(); indexes.Clear(); int i, j, k, l, w, h, u, v, n, side = 0; int[] x = new int[] { 0, 0, 0 }; int[] q = new int[] { 0, 0, 0 }; int[] du = new int[] { 0, 0, 0 }; int[] dv = new int[] { 0, 0, 0 }; int[] size = { xSize, ySize, zSize }; VoxelFace[] mask = new VoxelFace[(xSize * ySize * zSize) * 2]; VoxelFace voxelFace, voxelFace1; for (bool backFace = true, b = false; b != backFace; backFace = backFace && b, b = !b) { for (int d = 0; d < 3; d++) { u = (d + 1) % 3; v = (d + 2) % 3; x[0] = 0; x[1] = 0; x[2] = 0; q[0] = 0; q[1] = 0; q[2] = 0; q[d] = 1; if (d == 0) { side = backFace ? WEST : EAST; } else if (d == 1) { side = backFace ? BOTTOM : TOP; } else if (d == 2) { side = backFace ? SOUTH : NORTH; } for (x[d] = -1; x[d] < size[d];) { n = 0; for (x[v] = 0; x[v] < size[v]; x[v]++) { for (x[u] = 0; x[u] < size[u]; x[u]++) { if (x[d] >= 0) { voxelFace = new VoxelFace(); #if UNITY_EDITOR GetVoxelFace(voxelFace, x[0], x[1], x[2], side, ref invoxels, voxelSize, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity, paintMode); #else GetVoxelFace(voxelFace, x[0], x[1], x[2], side, ref invoxels, voxelSize, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity); #endif } else voxelFace = null; if (x[d] < size[d] - 1) { voxelFace1 = new VoxelFace(); #if UNITY_EDITOR GetVoxelFace(voxelFace1, x[0] + q[0], x[1] + q[1], x[2] + q[2], side, ref invoxels, voxelSize, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity, paintMode); #else GetVoxelFace(voxelFace1, x[0] + q[0], x[1] + q[1], x[2] + q[2], side, ref invoxels, voxelSize, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity); #endif } else voxelFace1 = null; if (voxelFace != null && voxelFace1 != null && voxelFace.Equals(voxelFace1)) mask[n++] = null; else { if(!backFace && voxelFace!=null) mask[n++] = new VoxelFace() { Active = voxelFace.Active, Color = voxelFace.Color, Side = voxelFace.Side, VertShade = voxelFace.VertShade }; if(backFace && voxelFace1!=null) mask[n++] = new VoxelFace() { Active = voxelFace1.Active, Color = voxelFace1.Color, Side = voxelFace1.Side, VertShade = voxelFace1.VertShade }; } } } x[d]++; n = 0; for (j = 0; j < size[v]; j++) { for (i = 0; i < size[u];) { if (mask[n] != null) { for (w = 1; i + w < size[u] && mask[n + w] != null && mask[n + w].Equals(mask[n]); w++) { } bool done = false; for (h = 1; j + h < size[v]; h++) { for (k = 0; k < w; k++) { if (mask[n + k + h*size[u]] == null || !mask[n + k + h*size[u]].Equals(mask[n])) { done = true; break; } } if (done) { break; } } if (mask[n].Active) { x[u] = i; x[v] = j; du[0] = 0; du[1] = 0; du[2] = 0; du[u] = w; dv[0] = 0; dv[1] = 0; dv[2] = 0; dv[v] = h; Quad(new Vector3(x[0] + du[0], x[1] + du[1], x[2] + du[2]), new Vector3(x[0] + du[0] + dv[0], x[1] + du[1] + dv[1], x[2] + du[2] + dv[2]), new Vector3(x[0] + dv[0], x[1] + dv[1], x[2] + dv[2]), new Vector3(x[0], x[1], x[2]), side, voxelSize, overlapAmount, mask[n], backFace, selfShadeIntensity, vertices, indexes, colors, uvs); } for (l = 0; l < h; ++l) { for (k = 0; k < w; ++k) { mask[n + k + l*size[u]] = null; } } i += w; n += w; } else { i++; n++; } } } } } } vertArray = vertices.ToArray(); uvArray = uvs.ToArray(); colorArray = colors.ToArray(); indexArray = indexes.ToArray(); }
public static void GenerateCulled(List<Vector3> vertices, List<Vector2> uvs, List<Color32> colors, List<int> indexes, ref Vector3[] vertArray, ref Vector2[] uvArray, ref Color32[] colorArray, ref int[] indexArray, ref Voxel[] invoxels, float voxelSize,float overlapAmount, int xOffset, int yOffset, int zOffset, int xSize, int ySize, int zSize, int ub0, int ub1, int ub2, float selfShadeIntensity) #endif { vertices.Clear(); uvs.Clear(); colors.Clear(); indexes.Clear(); VoxelFace vf = new VoxelFace(); for (int z = 0; z < zSize; z++) for (int y = 0; y < ySize; y++) for (int x = 0; x < xSize; x++) { if (invoxels[xOffset + x + (ub0 + 1) * (yOffset + y + (ub1 + 1) * (zOffset + z))].Active == false) continue; Vector3 worldOffset = (new Vector3(x, y, z)); for (int f = 0; f < 6; f++) { #if UNITY_EDITOR GetVoxelFace(vf, x, y, z, f, ref invoxels, voxelSize, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity, paintMode); #else GetVoxelFace(vf, x, y, z, f, ref invoxels, voxelSize, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity); #endif if (vf.Active) { switch (f) { case 0: Quad(worldOffset + new Vector3(1, 0, 0), worldOffset + new Vector3(1, 1, 0), worldOffset + new Vector3(0, 1, 0), worldOffset + new Vector3(0, 0, 0), f, voxelSize, overlapAmount, vf, true, selfShadeIntensity, vertices, indexes, colors, uvs); break; case 1: Quad(worldOffset + new Vector3(1, 0, 1), worldOffset + new Vector3(1, 1, 1), worldOffset + new Vector3(0, 1, 1), worldOffset + new Vector3(0, 0, 1), f, voxelSize, overlapAmount, vf, false, selfShadeIntensity, vertices, indexes, colors, uvs); break; case 2: Quad(worldOffset + new Vector3(1, 1, 0), worldOffset + new Vector3(1, 1, 1), worldOffset + new Vector3(1, 0, 1), worldOffset + new Vector3(1, 0, 0), f, voxelSize, overlapAmount, vf, false, selfShadeIntensity, vertices, indexes, colors, uvs); break; case 3: Quad(worldOffset + new Vector3(0, 1, 0), worldOffset + new Vector3(0, 1, 1), worldOffset + new Vector3(0, 0, 1), worldOffset + new Vector3(0, 0, 0), f, voxelSize, overlapAmount, vf, true, selfShadeIntensity, vertices, indexes, colors, uvs); break; case 4: Quad(worldOffset + new Vector3(0, 1, 1), worldOffset + new Vector3(1, 1, 1), worldOffset + new Vector3(1, 1, 0), worldOffset + new Vector3(0, 1, 0), f, voxelSize, overlapAmount, vf, false, selfShadeIntensity, vertices, indexes, colors, uvs); break; case 5: Quad(worldOffset + new Vector3(0, 0, 1), worldOffset + new Vector3(1, 0, 1), worldOffset + new Vector3(1, 0, 0), worldOffset + new Vector3(0, 0, 0), f, voxelSize, overlapAmount, vf, true, selfShadeIntensity, vertices, indexes, colors, uvs); break; } } } } vertArray = vertices.ToArray(); uvArray = uvs.ToArray(); colorArray = colors.ToArray(); indexArray = indexes.ToArray(); }
public void FromCompressedByteArray(byte[] compressed) { byte[] streambuff = new byte[XSize * YSize * ZSize * Voxel.BYTE_SIZE]; using (var ms = new MemoryStream(compressed)) { using (var gzs = new GZipInputStream(ms)) { gzs.Read(streambuff, 0, streambuff.Length); } } Voxels = new Voxel[XSize * YSize * ZSize]; int o = 0; byte[] buffer = new byte[Voxel.BYTE_SIZE]; for (int x = 0; x < XSize; x++) for (int y = 0; y < YSize; y++) for (int z = 0; z < ZSize; z++) { for (int i = 0; i < Voxel.BYTE_SIZE; i++) buffer[i] = streambuff[o + i]; Voxels[x + XSize * (y + YSize * z)] = new Voxel(buffer); o += Voxel.BYTE_SIZE; } }
private void Generate(ref Voxel[] voxels, float voxelSize,float overlapAmount, int xOffset, int yOffset, int zOffset, int xSize, int ySize, int zSize, int ub0, int ub1, int ub2, float selfShadeIntensity, MeshingMode meshMode) #endif { #if UNITY_EDITOR switch (meshMode) { case MeshingMode.Culled: MeshGenerator.GenerateCulled(vertices, uvs, colors, indexes, ref vertArray, ref uvArray, ref colorArray, ref indexArray, ref voxels, voxelSize, overlapAmount, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity, paintMode); break; case MeshingMode.Greedy: MeshGenerator.GenerateGreedy(vertices, uvs, colors, indexes, ref vertArray, ref uvArray, ref colorArray, ref indexArray, ref voxels, voxelSize, overlapAmount, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity, paintMode); break; case MeshingMode.Marching: MeshGenerator.GenerateMarching(vertices, uvs, colors, indexes, ref vertArray, ref uvArray, ref colorArray, ref indexArray, ref voxels, voxelSize, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity, paintMode); break; } #else switch (meshMode) { case MeshingMode.Culled: MeshGenerator.GenerateCulled(vertices, uvs, colors, indexes, ref vertArray, ref uvArray, ref colorArray, ref indexArray, ref voxels, voxelSize, overlapAmount,xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity); break; case MeshingMode.Greedy: MeshGenerator.GenerateGreedy(vertices, uvs, colors, indexes, ref vertArray, ref uvArray, ref colorArray, ref indexArray, ref voxels, voxelSize, overlapAmount,xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity); break; case MeshingMode.Marching: MeshGenerator.GenerateMarching(vertices, uvs, colors, indexes, ref vertArray, ref uvArray, ref colorArray, ref indexArray, ref voxels, voxelSize, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity); break; } #endif }
public void GenerateMesh(Voxel[] voxels, float voxelSize, float overlapAmount, int xOffset, int yOffset, int zOffset, int xSize, int ySize, int zSize, int ub0, int ub1, int ub2, float selfShadeIntensity, MeshingMode mode, MeshingMode colliderMode, bool immediate, EditorPaintMode paintMode)
private void Generate(ref Voxel[] voxels, float voxelSize, float overlapAmount, int xOffset, int yOffset, int zOffset, int xSize, int ySize, int zSize, int ub0, int ub1, int ub2, float selfShadeIntensity, MeshingMode meshMode, EditorPaintMode paintMode)
private void GenerateThreaded(ref Voxel[] voxels, float voxelSize,float overlapAmount, int xOffset, int yOffset, int zOffset, int xSize, int ySize, int zSize, int ub0, int ub1, int ub2, float selfShadeIntensity, MeshingMode meshMode) #endif { status = ChunkStatus.CalculatingMesh; #if UNITY_EDITOR Generate(ref voxels, voxelSize, overlapAmount, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity, meshMode, paintMode); #else Generate(ref voxels, voxelSize, overlapAmount, xOffset, yOffset, zOffset, xSize, ySize, zSize, ub0, ub1, ub2, selfShadeIntensity, meshMode); #endif status = ChunkStatus.Ready; }
public void Add(Voxel voxel, int arrayX, int arrayY, int arrayZ, Vector3 worldPos) { Add(voxel, new PicaVoxelPoint(arrayX, arrayY, arrayZ), worldPos); }
public void FromCompressedByteArray(byte[] compressed) { byte[] streambuff = new byte[XSize * YSize * ZSize * Voxel.BYTE_SIZE]; using (var ms = new MemoryStream(compressed)) { using (var gzs = new GZipInputStream(ms)) { gzs.Read(streambuff, 0, streambuff.Length); } } bool convertAlpha = true; for (int i = 0; i < (streambuff.Length<50?streambuff.Length:50); i++) { if (streambuff[(streambuff.Length - 1) - i] != 0) convertAlpha = false; } Voxels = new Voxel[XSize * YSize * ZSize]; int o = 0; byte[] buffer = new byte[Voxel.BYTE_SIZE]; for (int x = 0; x < XSize; x++) for (int y = 0; y < YSize; y++) for (int z = 0; z < ZSize; z++) { if (!convertAlpha) { for (int i = 0; i < Voxel.BYTE_SIZE; i++) buffer[i] = streambuff[o + i]; Voxels[x + XSize * (y + YSize * z)] = new Voxel(buffer); o += Voxel.BYTE_SIZE; } else { for (int i = 0; i < Voxel.BYTE_SIZE - 1; i++) buffer[i] = streambuff[o + i]; buffer[5] = 255; Voxels[x + XSize * (y + YSize * z)] = new Voxel(buffer); o += Voxel.BYTE_SIZE - 1; } } }
static void GetVoxelFace(VoxelFace voxelFace, int x, int y, int z, int side, ref Voxel[] invoxels, float voxelSize, int xOffset, int yOffset, int zOffset, int xSize, int ySize, int zSize, int ub0, int ub1, int ub2, float selfShadeIntensity) #endif { Voxel v = invoxels[(x + xOffset) + (ub0 + 1)*((y + yOffset) + (ub1 + 1)*(z + zOffset))]; voxelFace.Active = v.Active; #if UNITY_EDITOR if(paintMode == EditorPaintMode.Color) voxelFace.Color = ColorToInt(v.Color); else voxelFace.Color = ColorToInt(new Color32(v.Value,v.Value,v.Value,255)); #else voxelFace.Color = ColorToInt(v.Color); #endif voxelFace.VertShade = 0; voxelFace.Side = side; if (!voxelFace.Active) return; if (selfShadeIntensity > 0f) { switch (side) { case 0: case 1: if (IsVoxelAt(x + xOffset -1, y + yOffset, z + zOffset + (side==0?-1:1), ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset - 1, y + yOffset - 1, z + zOffset + (side == 0 ? -1 : 1), ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset, y + yOffset - 1, z + zOffset + (side == 0 ? -1 : 1), ref invoxels, ub0, ub1, ub2)) voxelFace.VertShade |= Corners.TopLeft; if (IsVoxelAt(x + xOffset + 1, y + yOffset, z + zOffset + (side == 0 ? -1 : 1), ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset + 1, y + yOffset - 1, z + zOffset + (side == 0 ? -1 : 1), ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset, y + yOffset - 1, z + zOffset + (side == 0 ? -1 : 1), ref invoxels, ub0, ub1, ub2)) voxelFace.VertShade |= Corners.TopRight; if (IsVoxelAt(x + xOffset - 1, y + yOffset, z + zOffset + (side == 0 ? -1 : 1), ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset - 1, y + yOffset + 1, z + zOffset + (side == 0 ? -1 : 1), ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset, y + yOffset + 1, z + zOffset + (side == 0 ? -1 : 1), ref invoxels, ub0, ub1, ub2)) voxelFace.VertShade |= Corners.BottomLeft; if (IsVoxelAt(x + xOffset + 1, y + yOffset, z + zOffset + (side == 0 ? -1 : 1), ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset + 1, y + yOffset + 1, z + zOffset + (side == 0 ? -1 : 1), ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset, y + yOffset + 1, z + zOffset + (side == 0 ? -1 : 1), ref invoxels, ub0, ub1, ub2)) voxelFace.VertShade |= Corners.BottomRight; break; case 2: case 3: if (IsVoxelAt(x + xOffset + (side == 3 ? -1 : 1), y + yOffset, z + zOffset-1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset + (side == 3 ? -1 : 1), y + yOffset - 1, z + zOffset-1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset + (side == 3 ? -1 : 1), y + yOffset - 1, z + zOffset, ref invoxels, ub0, ub1, ub2)) voxelFace.VertShade |= Corners.TopLeft; if (IsVoxelAt(x + xOffset + (side == 3 ? -1 : 1), y + yOffset, z + zOffset + 1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset + (side == 3 ? -1 : 1), y + yOffset - 1, z + zOffset + 1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset + (side == 3 ? -1 : 1), y + yOffset - 1, z + zOffset, ref invoxels, ub0, ub1, ub2)) voxelFace.VertShade |= Corners.BottomLeft; if (IsVoxelAt(x + xOffset + (side == 3 ? -1 : 1), y + yOffset, z + zOffset - 1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset + (side == 3 ? -1 : 1), y + yOffset + 1, z + zOffset - 1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset + (side == 3 ? -1 : 1), y + yOffset + 1, z + zOffset, ref invoxels, ub0, ub1, ub2)) voxelFace.VertShade |= Corners.TopRight; if (IsVoxelAt(x + xOffset + (side == 3 ? -1 : 1), y + yOffset, z + zOffset + 1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset + (side == 3 ? -1 : 1), y + yOffset + 1, z + zOffset + 1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset + (side == 3 ? -1 : 1), y + yOffset + 1, z + zOffset, ref invoxels, ub0, ub1, ub2)) voxelFace.VertShade |= Corners.BottomRight; break; case 4: case 5: if (IsVoxelAt(x + xOffset, y + yOffset + (side == 5 ? -1 : 1), z + zOffset - 1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset-1, y + yOffset + (side == 5 ? -1 : 1), z + zOffset - 1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset-1, y + yOffset + (side == 5 ? -1 : 1), z + zOffset, ref invoxels, ub0, ub1, ub2)) voxelFace.VertShade |= Corners.TopLeft; if (IsVoxelAt(x + xOffset, y + yOffset + (side == 5 ? -1 : 1), z + zOffset + 1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset-1, y + yOffset + (side == 5 ? -1 : 1), z + zOffset + 1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset-1, y + yOffset + (side == 5 ? -1 : 1), z + zOffset, ref invoxels, ub0, ub1, ub2)) voxelFace.VertShade |= Corners.TopRight; if (IsVoxelAt(x + xOffset, y + yOffset + (side == 5 ? -1 : 1), z + zOffset - 1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset+1, y + yOffset + (side == 5 ? -1 : 1), z + zOffset - 1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset+1, y + yOffset + (side == 5 ? -1 : 1), z + zOffset, ref invoxels, ub0, ub1, ub2)) voxelFace.VertShade |= Corners.BottomLeft; if (IsVoxelAt(x + xOffset, y + yOffset + (side == 5 ? -1 : 1), z + zOffset + 1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset+1, y + yOffset + (side == 5 ? -1 : 1), z + zOffset + 1, ref invoxels, ub0, ub1, ub2) || IsVoxelAt(x + xOffset+1, y + yOffset + (side == 5 ? -1 : 1), z + zOffset, ref invoxels, ub0, ub1, ub2)) voxelFace.VertShade |= Corners.BottomRight; break; } } switch (side) { case 0: voxelFace.Active = !IsVoxelAt(x + xOffset, y + yOffset, z+zOffset - 1, ref invoxels, ub0, ub1, ub2); break; case 1: voxelFace.Active = !IsVoxelAt(x + xOffset, y + yOffset, z + zOffset + 1, ref invoxels, ub0, ub1, ub2); break; case 2: voxelFace.Active = !IsVoxelAt(x + xOffset + 1, y + yOffset, z + zOffset, ref invoxels, ub0, ub1, ub2); break; case 3: voxelFace.Active = !IsVoxelAt(x + xOffset - 1, y + yOffset, z + zOffset, ref invoxels, ub0, ub1, ub2); break; case 4: voxelFace.Active = !IsVoxelAt(x + xOffset, y + yOffset + 1, z + zOffset, ref invoxels, ub0, ub1, ub2); break; case 5: voxelFace.Active = !IsVoxelAt(x + xOffset, y + yOffset - 1, z + zOffset, ref invoxels, ub0, ub1, ub2); break; } }
/// <summary> /// Attempts to set a voxel within this volume's current frame, at a given world position, to the supplied voxel value /// </summary> /// <param name="pos">The world position in the scene</param> /// <param name="vox">The new voxel to set to</param> /// <returns>The array position of the voxel</returns> public Vector3 SetVoxelAtWorldPosition(Vector3 pos, Voxel vox) { return Frames[CurrentFrame].SetVoxelAtWorldPosition(pos, vox); }
public void Add(Voxel voxel, PicaVoxelPoint arrayPoint, Vector3 worldPos) { Voxels.Add(new BatchVoxel(voxel, arrayPoint, worldPos)); }
static void GetVoxelFace(VoxelFace voxelFace, int x, int y, int z, int side, ref Voxel[] invoxels, float voxelSize, int xOffset, int yOffset, int zOffset, int xSize, int ySize, int zSize, int ub0, int ub1, int ub2, float selfShadeIntensity, EditorPaintMode paintMode)
/// <summary> /// Attempts to set a voxel within this volume's current frame, at a specified array position /// </summary> /// <param name="pos">A PicaVoxelPoint location within the 3D array of voxels</param> /// <param name="vox">The new voxel to set to</param> public void SetVoxelAtArrayPosition(PicaVoxelPoint pos, Voxel vox) { Frames[CurrentFrame].SetVoxelAtArrayPosition(pos, vox); }
static bool IsVoxelAt(int x, int y, int z, ref Voxel[] invoxels, int ub0, int ub1, int ub2) { if (x < 0 || x > ub0 || y < 0 || y > ub1 || z < 0 || z > ub2) return false; return invoxels[x + (ub0 + 1)*(y + (ub1 + 1)*z)].Active; }
/// <summary> /// Attempts to set a voxel within this volume's current frame, at a specified x,y,z array position /// </summary> /// <param name="x">X array position</param> /// <param name="y">Y array position</param> /// <param name="z">Z array position</param> /// <param name="vox">The new voxel to set to</param> public void SetVoxelAtArrayPosition(int x, int y, int z, Voxel vox) { Frames[CurrentFrame].SetVoxelAtArrayPosition(x, y, z, vox); }
public static void GenerateGreedy(List<Vector3> vertices, List<Vector2> uvs, List<Color32> colors, List<int> indexes, ref Vector3[] vertArray, ref Vector2[] uvArray, ref Color32[] colorArray, ref int[] indexArray, ref Voxel[] invoxels, float voxelSize, float overlapAmount, int xOffset, int yOffset, int zOffset, int xSize, int ySize, int zSize, int ub0, int ub1, int ub2, float selfShadeIntensity, EditorPaintMode paintMode)
public void OnGUI() { EditorGUILayout.Space(); EditorGUILayout.LabelField( "Volume Size: " + voxelObject.name + " (" + voxelObject.XSize + "," + voxelObject.YSize + "," + voxelObject.ZSize + ")", new GUIStyle() {fontStyle = FontStyle.Bold}); EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("X:", new[] {GUILayout.Width(30)}); xSize = EditorGUILayout.IntField(xSize); EditorGUILayout.LabelField("Y:", new[] {GUILayout.Width(30)}); ySize = EditorGUILayout.IntField(ySize); EditorGUILayout.LabelField("Z:", new[] {GUILayout.Width(30)}); zSize = EditorGUILayout.IntField(zSize); EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); anchorX = (AnchorX) EditorGUILayout.EnumPopup("X Anchor: ", anchorX); anchorY = (AnchorY) EditorGUILayout.EnumPopup("Y Anchor: ", anchorY); anchorZ = (AnchorZ) EditorGUILayout.EnumPopup("Z Anchor: ", anchorZ); EditorGUILayout.Space(); fillVoxels = EditorGUILayout.ToggleLeft(" Fill any added space", fillVoxels); EditorGUILayout.Space(); EditorGUILayout.LabelField( "Chunk Size: " + voxelObject.name + " (" + voxelObject.XChunkSize + "," + voxelObject.YChunkSize + "," + voxelObject.ZChunkSize + ")", new GUIStyle() { fontStyle = FontStyle.Bold }); EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("X:", new[] { GUILayout.Width(30) }); xChunkSize = EditorGUILayout.IntField(xChunkSize); EditorGUILayout.LabelField("Y:", new[] { GUILayout.Width(30) }); yChunkSize = EditorGUILayout.IntField(yChunkSize); EditorGUILayout.LabelField("Z:", new[] { GUILayout.Width(30) }); zChunkSize = EditorGUILayout.IntField(zChunkSize); EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Resize") && (xSize != voxelObject.XSize || ySize != voxelObject.YSize || zSize != voxelObject.ZSize || xChunkSize != voxelObject.XChunkSize || yChunkSize != voxelObject.YChunkSize || zChunkSize != voxelObject.ZChunkSize)) { if (xSize < 1) xSize = 1; if (ySize < 1) ySize = 1; if (zSize < 1) zSize = 1; if (xChunkSize < 1) xChunkSize = 1; if (yChunkSize < 1) yChunkSize = 1; if (zChunkSize < 1) zChunkSize = 1; int totalChunkSize = xChunkSize*yChunkSize*zChunkSize; if (totalChunkSize > 16*16*16) { UnityEditor.EditorUtility.DisplayDialog("PicaVoxel", "The largest chunk size is 16*16*16 voxels TOTAL. Decrease size in one axis to increase the other two.", "OK"); } else { voxelObject.XChunkSize = xChunkSize; voxelObject.YChunkSize = yChunkSize; voxelObject.ZChunkSize = zChunkSize; if (xSize != voxelObject.XSize || ySize != voxelObject.YSize || zSize != voxelObject.ZSize) { List<Object> undoObjects = new List<Object>(); foreach (Frame frame in voxelObject.Frames) { undoObjects.Add(frame); } undoObjects.Add(voxelObject); Undo.RecordObjects(undoObjects.ToArray(), "Resize Voxel Object"); foreach (Frame frame in voxelObject.Frames) UnityEditor.EditorUtility.SetDirty(frame); UnityEditor.EditorUtility.SetDirty(voxelObject); PicaVoxelBox copyDestBox = new PicaVoxelBox( anchorX == AnchorX.Left ? 0 : anchorX == AnchorX.Center ? (xSize/2) - (voxelObject.XSize/2) : xSize - voxelObject.XSize, anchorY == AnchorY.Bottom ? 0 : anchorY == AnchorY.Center ? (ySize/2) - (voxelObject.YSize/2) : ySize - voxelObject.YSize, anchorZ == AnchorZ.Front ? 0 : anchorZ == AnchorZ.Center ? (zSize/2) - (voxelObject.ZSize/2) : zSize - voxelObject.ZSize, (anchorX == AnchorX.Left ? 0 : anchorX == AnchorX.Center ? (xSize/2) - (voxelObject.XSize/2) : xSize - voxelObject.XSize) + (voxelObject.XSize - 1), (anchorY == AnchorY.Bottom ? 0 : anchorY == AnchorY.Center ? (ySize/2) - (voxelObject.YSize/2) : ySize - voxelObject.YSize) + (voxelObject.YSize - 1), (anchorZ == AnchorZ.Front ? 0 : anchorZ == AnchorZ.Center ? (zSize/2) - (voxelObject.ZSize/2) : zSize - voxelObject.ZSize) + (voxelObject.ZSize - 1)); foreach (Frame frame in voxelObject.Frames) { Voxel[] newVox = new Voxel[xSize*ySize*zSize]; if (fillVoxels) { for (int x = 0; x < xSize; x++) for (int y = 0; y < ySize; y++) for (int z = 0; z < zSize; z++) newVox[x + xSize*(y + ySize*z)] = new Voxel() { State = VoxelState.Active, Color = voxelObject.PaletteColors[0], Value = 128 }; } int destX = copyDestBox.BottomLeftFront.X; int destY = copyDestBox.BottomLeftFront.Y; int destZ = copyDestBox.BottomLeftFront.Z; for (int x = 0; x < voxelObject.XSize; x++) { for (int y = 0; y < voxelObject.YSize; y++) { for (int z = 0; z < voxelObject.ZSize; z++) { if (destX < 0 || destY < 0 || destZ < 0 || destX >= xSize || destY >= ySize || destZ >= zSize) { destZ++; continue; } newVox[destX + xSize*(destY + ySize*destZ)] = frame.Voxels[x + frame.XSize*(y + frame.YSize*z)]; destZ++; } destZ = copyDestBox.BottomLeftFront.Z; destY++; } destY = copyDestBox.BottomLeftFront.Y; destX++; } frame.XSize = xSize; frame.YSize = ySize; frame.ZSize = zSize; frame.EditingVoxels = null; frame.Voxels = newVox; } voxelObject.XSize = xSize; voxelObject.YSize = ySize; voxelObject.ZSize = zSize; } voxelObject.CreateChunks(); voxelObject.SaveForSerialize(); //EditorUtility.SetDirty(voxelObject); Close(); } } // } if (GUILayout.Button("Cancel")) Close(); EditorGUILayout.EndHorizontal(); }
static bool IsVoxelAtMarching(ref Voxel[] voxels, int subx, int suby, int subz, int xOffset, int yOffset, int zOffset, int ub0, int ub1, int ub2) { // Convert to x,y,z position int x = Mathf.FloorToInt(((float)subx) / 2.0f); int y = Mathf.FloorToInt(((float)suby) / 2.0f); int z = Mathf.FloorToInt(((float)subz) / 2.0f); if (x + xOffset < 0 || y + yOffset < 0 || z + zOffset < 0 || x + xOffset > ub0 || y + yOffset > ub1 || z + zOffset > ub2) return false; if (x + xOffset == 0 && voxels[(x + xOffset) + (ub0 + 1) * ((y + yOffset) + (ub1 + 1) * (z + zOffset))].Active) return false; if (y + yOffset == 0 && voxels[(x + xOffset) + (ub0 + 1) * ((y + yOffset) + (ub1 + 1) * (z + zOffset))].Active) return false; if (z + zOffset == 0 && voxels[(x + xOffset) + (ub0 + 1) * ((y + yOffset) + (ub1 + 1) * (z + zOffset))].Active) return false; // if (x == 0 || y == 0 || z == 0) return false; if (voxels[(x + xOffset) + (ub0 + 1) * ((y + yOffset) + (ub1 + 1) * (z + zOffset))].Active) { return true; } return false; }
/// <summary> /// Attempts to set a voxel within this frame, at a given world position, to the supplied voxel value /// </summary> /// <param name="pos">The world position in the scene</param> /// <param name="vox">The new voxel to set to</param> public Vector3 SetVoxelAtWorldPosition(Vector3 pos, Voxel vox) { Vector3 localPos = transform.InverseTransformPoint(pos); Vector3 arrayPos = new Vector3((int) (localPos.x/ParentVolume.VoxelSize), (int) (localPos.y/ParentVolume.VoxelSize), (int) (localPos.z/ParentVolume.VoxelSize)); SetVoxelAtArrayPosition((int)arrayPos.x, (int)arrayPos.y, (int)arrayPos.z,vox); return arrayPos; }
public void SpawnSingle(Vector3 worldPos, Voxel voxel, float voxelSize, Vector3 velocity) { System.Emit(worldPos, velocity, voxelSize, ParticleLifetime, voxel.Color); }
/// <summary> /// Attempts to set a voxel within this frame, at a specified array position /// </summary> /// <param name="pos">A PicaVoxelPoint location within the 3D array of voxels</param> /// <param name="vox">The new voxel to set to</param> public void SetVoxelAtArrayPosition(PicaVoxelPoint pos, Voxel vox) { SetVoxelAtArrayPosition(pos.X, pos.Y, pos.Z, vox); }