Beispiel #1
0
        private StaticMeshComponent CreateMesh()
        {
            var mesh = Mesh.CreateCube().ToPrimitive(MeshFaceType.Triangle);

            mesh.CreateFacesAndIndicies();
            mesh.Scale(10);
            var ind  = mesh.GetIndiciesArray();
            var vert = mesh.GetComponent <MeshPosition3Component>().ToArray().Select(s => new Vector3d(s.X, s.Y, s.Z)).ToArray();
            var box  = new Net3dBool.Solid(vert, ind);

            mesh = Mesh.CreateCube().ToPrimitive(MeshFaceType.Triangle);
            mesh.CreateFacesAndIndicies();
            mesh.Scale(3);
            ind  = mesh.GetIndiciesArray();
            vert = mesh.GetComponent <MeshPosition3Component>().ToArray().Select(s => new Vector3d(s.X, s.Y, s.Z)).ToArray();
            var box2 = new Net3dBool.Solid(vert, ind);

            mesh = Mesh.CreateCube().ToPrimitive(MeshFaceType.Triangle);
            mesh.CreateFacesAndIndicies();
            mesh.Scale(2);
            mesh.Translate(1, 0, 0);
            ind  = mesh.GetIndiciesArray();
            vert = mesh.GetComponent <MeshPosition3Component>().ToArray().Select(s => new Vector3d(s.X, s.Y, s.Z)).ToArray();
            var box3 = new Net3dBool.Solid(vert, ind);

            var modeller = new Net3dBool.BooleanModeller(box, box2);
            var tmp      = modeller.GetDifference();

            modeller = new Net3dBool.BooleanModeller(tmp, box3);
            tmp      = modeller.GetDifference();

            VertexDataPosNormalColor[] data = tmp.GetVertices().Select(v => new VertexDataPosNormalColor(new Vector3((float)v.X, (float)v.Y, (float)v.Z), new Vector3(1, 0, 0), new Vector4(1, 1, 0, 1))).ToArray();
            for (var i = 0; i < data.Length; i++)
            {
                var face   = i / 3;
                var vertex = i % 3;
                // switch (vertex)
                // {
                //     case 0:
                //         data[i].Color = new Vector4(1, 0, 0, 1);
                //         break;
                //     case 1:
                //         data[i].Color = new Vector4(0, 1, 0, 1);
                //         break;
                //     case 2:
                //         data[i].Color = new Vector4(0, 0, 1, 1);
                //         break;
                // }
                data[i].Normal = Vector3.UnitX;
            }
            var meshData = Mesh.CreateFromVertices(data, tmp.GetIndices().ToArray());

            meshData.Expand();
            meshData.RecalculateNormals(25f);

            var material = new Material
            {
                Ambient        = 0.5f,
                Color          = new Vector4(1, 1, 1, 1),
                UseVertexColor = true,
                PipelineType   = PipelineType.Forward,
            };

            var comp = new StaticMeshComponent()
            {
                Name                = "BoolMesh",
                RelativeRotation    = new Vector3(0, 0, 0.5f).ToQuaternion(),
                RelativeScale       = new Vector3(1),
                RelativeTranslation = new Vector3(2, 0, 0.5f),
                Material            = material,
            };

            comp.SetMesh(meshData);
            return(comp);
        }