public void Apply(MeshManager manager) { VoxelBlob targetData = manager.m_blob; byte newMat = material; material = targetData[width, layer, depth]; targetData[width, layer, depth] = newMat; manager.MarkChunksForRegenForPoint(width, layer, depth); }
IEnumerator DoApply(MeshManager manager, DeltaDoneDelegate onDone, bool undo) { VoxelBlob targetData = manager.m_blob; foreach (BlobHolder holder in blobs.Values) { if (Scheduler.ShouldYield()) { yield return(null); } VoxelBlob blob = holder.blob; if (holder.unDone == undo) { continue; } for (int x = 0; x < blob.width; ++x) { for (int y = 0; y < blob.height; ++y) { for (int z = 0; z < blob.depth; ++z) { byte newMat = blob[x, y, z]; if (newMat != System.Convert.ToByte(0)) { int dataX = x + (int)blob.position.x; int dataY = y + (int)blob.position.y; int dataZ = z + (int)blob.position.z; byte oldMat = targetData[dataX, dataY, dataZ]; if (oldMat == 0) { oldMat = MeshManager.kVoxelSubtract; } if (newMat == MeshManager.kVoxelSubtract) { newMat = 0; } blob[x, y, z] = oldMat; targetData[dataX, dataY, dataZ] = newMat; manager.MarkChunksForRegenForPoint(dataX, dataY, dataZ); } } } } holder.unDone = undo; } if (onDone != null) { onDone(); } }
/// <summary> /// Creates the voxels from ray casting through the face from rayDirection /// </summary> private IEnumerator CreateVoxelsFromDirection(int rayDirection) { int3 blobBounds = new int3(m_manager.m_blob.width, m_manager.m_blob.height, m_manager.m_blob.depth); DirData dirData = m_data.dirData[rayDirection]; int3 dataIndex = new int3(); int planeAxis0 = ((rayDirection + 1) % 3); int planeAxis1 = ((rayDirection + 2) % 3); for (; dirData.i < m_voxelEnd[planeAxis0]; ++dirData.i) { int i = dirData.i; if (i < 0 || i >= blobBounds[planeAxis0]) { continue; } for (; dirData.j < m_voxelEnd[planeAxis1]; ++dirData.j) { int j = dirData.j; if (j < 0 || j >= blobBounds[planeAxis1]) { continue; } if (Scheduler.ShouldYield()) { yield return(null); } if (shouldAbort) { yield break; } Vector3 point = Vector3.zero; point[planeAxis0] = i + 0.5f; point[planeAxis1] = j + 0.5f; SortedDictionary <float, bool> intersections = BuildIntersections(rayDirection, planeAxis0, planeAxis1, point); int3 index = new int3(Mathf.FloorToInt(point.x), Mathf.FloorToInt(point.y), Mathf.FloorToInt(point.z)); Stack <float> startPoints = new Stack <float>(); float last = -1f; foreach (KeyValuePair <float, bool> kvp in intersections) { if (Mathf.Approximately(kvp.Key, last)) { continue; } if (kvp.Value) { if (startPoints.Count > 0) { for (int k = Mathf.RoundToInt(startPoints.Peek()); k < Mathf.RoundToInt(kvp.Key); ++k) { index[rayDirection] = k; for (int dim = 0; dim < 3; ++dim) { dataIndex[dim] = index[dim] - m_voxelStart[dim]; } m_data.voxelData[dataIndex[0], dataIndex[1], dataIndex[2]]++; } if (startPoints.Count > 1) { startPoints.Pop(); } } } else { startPoints.Push(kvp.Key); } last = kvp.Key; } for (int k = m_voxelStart[rayDirection]; k < m_voxelEnd[rayDirection]; ++k) { index[rayDirection] = k; for (int dim = 0; dim < 3; ++dim) { dataIndex[dim] = index[dim] - m_voxelStart[dim]; } m_data.voxelData[dataIndex.x, dataIndex.y, dataIndex.z] += 10; if (m_data.voxelData[dataIndex.x, dataIndex.y, dataIndex.z] >= 32) { if (m_manager.m_blob.IsValidPoint(index)) { byte oldMat = m_manager.m_blob[index]; if (oldMat == MeshManager.kVoxelEmpty) { oldMat = MeshManager.kVoxelSubtract; } m_delta.blobDelta[index] = oldMat; m_manager.m_blob[index] = m_data.material; m_manager.MarkChunksForRegenForPoint(index); } } } } dirData.j = m_voxelStart[planeAxis1]; } yield return(null); }