private MeshGeometryModel3D ConvertVoxelizedSolidtoObject3D(VoxelizedSolid vs) { if (false) { var ts = vs.ConvertToTessellatedSolid( new Color(KnownColors.MediumSeaGreen) { Af = 0.80f }); return(ConvertTessellatedSolidtoObject3D(ts)); } var normalsTemplate = new[] { new float[] { -1, 0, 0 }, new float[] { -1, 0, 0 }, new float[] { 1, 0, 0 }, new float[] { 1, 0, 0 }, new float[] { 0, -1, 0 }, new float[] { 0, -1, 0 }, new float[] { 0, 1, 0 }, new float[] { 0, 1, 0 }, new float[] { 0, 0, -1 }, new float[] { 0, 0, -1 }, new float[] { 0, 0, 1 }, new float[] { 0, 0, 1 } }; var coordOffsets = new[] { new[] { new float[] { 0, 0, 0 }, new float[] { 0, 0, 1 }, new float[] { 0, 1, 0 } }, new[] { new float[] { 0, 1, 0 }, new float[] { 0, 0, 1 }, new float[] { 0, 1, 1 } }, //x-neg new[] { new float[] { 1, 0, 0 }, new float[] { 1, 1, 0 }, new float[] { 1, 0, 1 } }, new[] { new float[] { 1, 1, 0 }, new float[] { 1, 1, 1 }, new float[] { 1, 0, 1 } }, //x-pos new[] { new float[] { 0, 0, 0 }, new float[] { 1, 0, 0 }, new float[] { 0, 0, 1 } }, new[] { new float[] { 1, 0, 0 }, new float[] { 1, 0, 1 }, new float[] { 0, 0, 1 } }, //y-neg new[] { new float[] { 0, 1, 0 }, new float[] { 0, 1, 1 }, new float[] { 1, 1, 0 } }, new[] { new float[] { 1, 1, 0 }, new float[] { 0, 1, 1 }, new float[] { 1, 1, 1 } }, //y-pos new[] { new float[] { 0, 0, 0 }, new float[] { 0, 1, 0 }, new float[] { 1, 0, 0 } }, new[] { new float[] { 1, 0, 0 }, new float[] { 0, 1, 0 }, new float[] { 1, 1, 0 } }, //z-neg new[] { new float[] { 0, 0, 1 }, new float[] { 1, 0, 1 }, new float[] { 0, 1, 1 } }, new[] { new float[] { 1, 0, 1 }, new float[] { 1, 1, 1 }, new float[] { 0, 1, 1 } }, //z-pos }; var positions = new Vector3Collection(); var normals = new Vector3Collection(); var lowestLevel = (int)vs.VoxelSideLengths.Length - 1; foreach (var v in vs.Voxels()) //VoxelDiscretization.ExtraCoarse)) // var v = vs.Voxels(VoxelDiscretization.ExtraCoarse).First(); //VoxelDiscretization.ExtraCoarse)) { if (v.Role == VoxelRoleTypes.Partial && v.Level < lowestLevel) { continue; } var neighbors = vs.GetNeighbors(v).ToList(); if (neighbors.All(n => n != null && (n.Role == VoxelRoleTypes.Full || (n.Role == VoxelRoleTypes.Partial && v.Level == lowestLevel)))) { continue; } var x = (float)v.BottomCoordinate[0]; var y = (float)v.BottomCoordinate[1]; var z = (float)v.BottomCoordinate[2]; var s = (float)v.SideLength; for (int i = 0; i < 12; i++) { // if (neighbors[i / 2] != null && neighbors[i / 2].Role == VoxelRoleTypes.Full) continue; if (neighbors[i / 2] != null && (neighbors[i / 2].Role == VoxelRoleTypes.Full || (neighbors[i / 2].Role == VoxelRoleTypes.Partial && v.Level == lowestLevel))) { continue; } for (int j = 0; j < 3; j++) { positions.Add(new Vector3(x + coordOffsets[i][j][0] * s, y + coordOffsets[i][j][1] * s, z + coordOffsets[i][j][2] * s)); normals.Add(new Vector3(normalsTemplate[i][0], normalsTemplate[i][1], normalsTemplate[i][2])); } } } return(new MeshGeometryModel3D { Material = new PhongMaterial() { DiffuseColor = new SharpDX.Color4(vs.SolidColor.Rf, vs.SolidColor.Gf, vs.SolidColor.Bf, vs.SolidColor.Af) //(float)0.75 * vs.SolidColor.Af) }, Geometry = new HelixToolkit.Wpf.SharpDX.MeshGeometry3D { Positions = positions, Indices = new IntCollection(Enumerable.Range(0, positions.Count)), Normals = normals } }); }
private static Visual3D MakeModelVisual3D(VoxelizedSolid vs) { var positions = new Point3DCollection(); var normals = new Vector3DCollection(); var s = vs.VoxelSideLength; for (var i = 0; i < vs.VoxelsPerSide[0]; i++) { for (var j = 0; j < vs.VoxelsPerSide[1]; j++) { for (var k = 0; k < vs.VoxelsPerSide[2]; k++) { if (vs[i, j, k] == 0) { continue; } if (!vs.GetNeighbors(i, j, k, out var neighbors)) { continue; } var x = i * s + vs.Offset[0]; var y = j * s + vs.Offset[1]; var z = k * s + vs.Offset[2]; for (var m = 0; m < 12; m++) { if (neighbors[m / 2] != null) { continue; } for (var n = 0; n < 3; n++) { positions.Add(new Point3D(x + coordOffsets[m][n][0] * s, y + coordOffsets[m][n][1] * s, z + coordOffsets[m][n][2] * s)); normals.Add(new Vector3D(normalsTemplate[m][0], normalsTemplate[m][1], normalsTemplate[m][2])); } } } } } return(new ModelVisual3D { Content = new GeometryModel3D { Geometry = new MeshGeometry3D { Positions = positions, Normals = normals }, Material = MaterialHelper.CreateMaterial( new System.Windows.Media.Color { A = vs.SolidColor.A, B = vs.SolidColor.B, G = vs.SolidColor.G, R = vs.SolidColor.R }) } }); }
private MeshGeometryModel3D ConvertVoxelizedSolidtoObject3D(VoxelizedSolid vs) { if (false) { var ts = vs.ConvertToTessellatedSolid( new Color(KnownColors.MediumSeaGreen) { Af = 0.80f }); return(ConvertTessellatedSolidtoObject3D(ts)); } var normalsTemplate = new[] { new float[] { -1, 0, 0 }, new float[] { -1, 0, 0 }, new float[] { 1, 0, 0 }, new float[] { 1, 0, 0 }, new float[] { 0, -1, 0 }, new float[] { 0, -1, 0 }, new float[] { 0, 1, 0 }, new float[] { 0, 1, 0 }, new float[] { 0, 0, -1 }, new float[] { 0, 0, -1 }, new float[] { 0, 0, 1 }, new float[] { 0, 0, 1 } }; var coordOffsets = new[] { new[] { new float[] { 0, 0, 0 }, new float[] { 0, 0, 1 }, new float[] { 0, 1, 0 } }, new[] { new float[] { 0, 1, 0 }, new float[] { 0, 0, 1 }, new float[] { 0, 1, 1 } }, //x-neg new[] { new float[] { 1, 0, 0 }, new float[] { 1, 1, 0 }, new float[] { 1, 0, 1 } }, new[] { new float[] { 1, 1, 0 }, new float[] { 1, 1, 1 }, new float[] { 1, 0, 1 } }, //x-pos new[] { new float[] { 0, 0, 0 }, new float[] { 1, 0, 0 }, new float[] { 0, 0, 1 } }, new[] { new float[] { 1, 0, 0 }, new float[] { 1, 0, 1 }, new float[] { 0, 0, 1 } }, //y-neg new[] { new float[] { 0, 1, 0 }, new float[] { 0, 1, 1 }, new float[] { 1, 1, 0 } }, new[] { new float[] { 1, 1, 0 }, new float[] { 0, 1, 1 }, new float[] { 1, 1, 1 } }, //y-pos new[] { new float[] { 0, 0, 0 }, new float[] { 0, 1, 0 }, new float[] { 1, 0, 0 } }, new[] { new float[] { 1, 0, 0 }, new float[] { 0, 1, 0 }, new float[] { 1, 1, 0 } }, //z-neg new[] { new float[] { 0, 0, 1 }, new float[] { 1, 0, 1 }, new float[] { 0, 1, 1 } }, new[] { new float[] { 1, 0, 1 }, new float[] { 1, 1, 1 }, new float[] { 0, 1, 1 } }, //z-pos }; var positions = new Vector3Collection(); var normals = new Vector3Collection(); var s = (float)vs.VoxelSideLength; for (var i = 0; i < vs.VoxelsPerSide[0]; i++) { for (var j = 0; j < vs.VoxelsPerSide[1]; j++) { for (var k = 0; k < vs.VoxelsPerSide[2]; k++) { if (vs[i, j, k] == 0) { continue; } if (!vs.GetNeighbors(i, j, k, out var neighbors)) { continue; } var x = i * s + (float)vs.Offset[0]; var y = j * s + (float)vs.Offset[1]; var z = k * s + (float)vs.Offset[2]; for (var m = 0; m < 12; m++) { if (neighbors[m / 2] != null) { continue; } for (var n = 0; n < 3; n++) { positions.Add(new Vector3(x + (coordOffsets[m][n][0] * s), y + coordOffsets[m][n][1] * s, z + coordOffsets[m][n][2] * s)); normals.Add(new Vector3(normalsTemplate[m][0], normalsTemplate[m][1], normalsTemplate[m][2])); } } } } } return(new MeshGeometryModel3D { Material = new PhongMaterial() { DiffuseColor = new SharpDX.Color4(vs.SolidColor.Rf, vs.SolidColor.Gf, vs.SolidColor.Bf, vs.SolidColor.Af) //(float)0.75 * vs.SolidColor.Af) }, Geometry = new HelixToolkit.Wpf.SharpDX.MeshGeometry3D { Positions = positions, Indices = new IntCollection(Enumerable.Range(0, positions.Count)), Normals = normals } }); }