/// <summary> /// Connect the cell vertex to adjacent non-empty cells. /// </summary> private void ConnectCellVertexWithQuad(Int3 cellCoord, int cellFillsMask, FILL_VALUE_TYPE[] cellFillValues) { // Add triangles in three dimensions for (int axis = 0; axis < 3; axis++) { // If no intersection along this axis, skip. if (intersectionAxisLookup[cellFillsMask, axis] == 0) { continue; } int axis_1 = (axis + 1) % 3; int axis_2 = (axis + 2) % 3; var vi0 = GetVertIndexAtCoord(cellCoord); var vi1 = GetVertIndexAtCoord(cellCoord.Offset(axis_1, -1)); var vi2 = GetVertIndexAtCoord(cellCoord.Offset(axis_1, -1).Offset(axis_2, -1)); var vi3 = GetVertIndexAtCoord(cellCoord.Offset(axis_2, -1)); if (vi0 < 0 || vi1 < 0 || vi2 < 0 || vi3 < 0) { continue; } // Flip faces based on corner value. if ((cellFillsMask & 1) == 1) { AddQuadToTriangleBuffer(vi0, vi1, vi2, vi3); } else { AddQuadToTriangleBuffer(vi0, vi3, vi2, vi1); } } }
private void CollectCellMesh(Int3 cellCoord) { var cellKey = GetVertDataBufferKey(cellCoord); if (cellToVertDataLookup.ContainsKey(cellKey)) { var vertData = cellToVertDataLookup[cellKey]; // Add triangles in three dimensions for (int axis = 0; axis < 3; axis++) { // If no intersection along this axis, skip. if (intersectionAxisLookup[vertData.cellFillsMask, axis] == 0) { continue; } int axis_1 = (axis + 1) % 3; int axis_2 = (axis + 2) % 3; var vi0 = GetVertIndexAtCoord(cellCoord); var vi1 = GetVertIndexAtCoord(cellCoord.Offset(axis_1, -1)); var vi2 = GetVertIndexAtCoord(cellCoord.Offset(axis_1, -1).Offset(axis_2, -1)); var vi3 = GetVertIndexAtCoord(cellCoord.Offset(axis_2, -1)); if (vi0 < 0 || vi1 < 0 || vi2 < 0 || vi3 < 0) { continue; } // Flip faces based on corner value. if ((vertData.cellFillsMask & 1) == 1) { AddQuadToTriangleBuffer(vi0, vi1, vi2, vi3); } else { AddQuadToTriangleBuffer(vi0, vi3, vi2, vi1); } } } }
private void UpdateCellTriangles(Int3 cellCoord, int cellFillsMask, FILL_VALUE_TYPE[] cellFillValues) { // Add triangles in three dimensions for (int axis = 0; axis < 3; axis++) { // If no intersection along this axis, skip. if (intersectionAxisLookup[cellFillsMask, axis] == 0) { continue; } int axis_1 = (axis + 1) % 3; int axis_2 = (axis + 2) % 3; var vi0 = GetVertIndexAtCoord(cellCoord); var vi1 = GetVertIndexAtCoord(cellCoord.Offset(axis_1, -1)); var vi2 = GetVertIndexAtCoord(cellCoord.Offset(axis_1, -1).Offset(axis_2, -1)); var vi3 = GetVertIndexAtCoord(cellCoord.Offset(axis_2, -1)); if (vi0 < 0 || vi1 < 0 || vi2 < 0 || vi3 < 0) { continue; } // For old vertices, only add triangles which contain newly created vertices. if (newlyAddedVertices.Contains(vi0) || newlyAddedVertices.Contains(vi1) || newlyAddedVertices.Contains(vi2) || newlyAddedVertices.Contains(vi3)) { // Flip faces based on corner value. if ((cellFillsMask & 1) == 1) { AddQuadToTriangleBuffer(vi0, vi1, vi2, vi3); } else { AddQuadToTriangleBuffer(vi0, vi3, vi2, vi1); } } } }
private Vector3 ComputeCellNormal(Int3 cellCoord, FILL_VALUE_TYPE[] cellFillValues, Vector3 averagedLocalPos) { Vector3 normal; // Smoother normal normal.x = (GetCellFillsSum(cellCoord.Offset(-1, 0, 0), averagedLocalPos, 0) - GetCellFillsSum(cellCoord.Offset(1, 0, 0), averagedLocalPos, 0)); normal.y = (GetCellFillsSum(cellCoord.Offset(0, -1, 0), averagedLocalPos, 1) - GetCellFillsSum(cellCoord.Offset(0, 1, 0), averagedLocalPos, 1)); normal.z = (GetCellFillsSum(cellCoord.Offset(0, 0, -1), averagedLocalPos, 2) - GetCellFillsSum(cellCoord.Offset(0, 0, 1), averagedLocalPos, 2)); return(normal.normalized); }
public void GetNearbyTrunksAtPos(Vector3 worldPos, Vector3 distanceLimit, List <VoxelTrunk> trunks) { trunks.Clear(); if (ownerEntity == null) { trunks.Add(this); return; } var cellCoord = GetCoordByWorldPos(worldPos); var limit = new Int3( Mathf.CeilToInt(distanceLimit.x / cellSize.x), Mathf.CeilToInt(distanceLimit.y / cellSize.y), Mathf.CeilToInt(distanceLimit.z / cellSize.z) ); for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { for (int k = -1; k <= 1; k++) { var offset = new Int3(i * limit.x, j * limit.y, k * limit.z); var nearbyCellCoord = cellCoord + offset; if (!data.ContainsCell(nearbyCellCoord)) { var neighbor = ownerEntity.GetTrunk(coordinate.Offset(i, j, k)); if (neighbor && !trunks.Contains(neighbor)) { trunks.Add(neighbor); } } else if (i == 0 && j == 0 && k == 0) { trunks.Add(this); } } } } }
public void SetAllDirty() { dirtyRegionMin = Int3.Zero; dirtyRegionMax = dimension.Offset(-1, -1, -1); }