public static MeshGeometry3D Merge(params MeshGeometry3D[] meshes)
        {
            var positions = new Vector3Collection();
            var indices   = new IntCollection();

            var normals      = meshes.All(x => x.Normals != null) ? new Vector3Collection() : null;
            var colors       = meshes.All(x => x.Colors != null) ? new Color4Collection() : null;
            var textureCoods = meshes.All(x => x.TextureCoordinates != null) ? new Vector2Collection() : null;
            var tangents     = meshes.All(x => x.Tangents != null) ? new Vector3Collection() : null;
            var bitangents   = meshes.All(x => x.BiTangents != null) ? new Vector3Collection() : null;

            int index = 0;

            foreach (var part in meshes)
            {
                positions.AddRange(part.Positions);
                indices.AddRange(part.Indices.Select(x => x + index));
                index += part.Positions.Count;
            }

            if (normals != null)
            {
                normals = new Vector3Collection(meshes.SelectMany(x => x.Normals));
            }

            if (colors != null)
            {
                colors = new Color4Collection(meshes.SelectMany(x => x.Colors));
            }

            if (textureCoods != null)
            {
                textureCoods = new Vector2Collection(meshes.SelectMany(x => x.TextureCoordinates));
            }

            if (tangents != null)
            {
                tangents = new Vector3Collection(meshes.SelectMany(x => x.Tangents));
            }

            if (bitangents != null)
            {
                bitangents = new Vector3Collection(meshes.SelectMany(x => x.BiTangents));
            }

            var mesh = new MeshGeometry3D()
            {
                Positions = positions,
                Indices   = indices,
            };

            mesh.Normals            = normals;
            mesh.Colors             = colors;
            mesh.TextureCoordinates = textureCoods;
            mesh.Tangents           = tangents;
            mesh.BiTangents         = bitangents;

            return(mesh);
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="center"></param>
        /// <param name="xlength"></param>
        /// <param name="ylength"></param>
        /// <param name="zlength"></param>
        public void AddBox(Point3D center, double xlength, double ylength, double zlength)
        {
            int i0 = positions.Count;
            var dx = new Vector3D((float)xlength / 2f, 0, 0);
            var dy = new Vector3D(0, (float)ylength / 2f, 0);
            var dz = new Vector3D(0, 0, (float)zlength / 2f);

            this.Add(true, center - dx - dy - dz, center + dx - dy - dz, center + dx + dy - dz, center - dx + dy - dz);
            this.Add(true, center - dx - dy + dz, center + dx - dy + dz, center + dx + dy + dz, center - dx + dy + dz);
            lineListIndices.AddRange(new[] { i0 + 0, i0 + 4, i0 + 1, i0 + 5, i0 + 2, i0 + 6, i0 + 3, i0 + 7 });
        }
        public void Test_AddRange_with_values()
        {
            var collection = new IntCollection()
            {
                1, 2, 3, 4, 5
            };

            collection.AddRange(new int[] { 6, 7 });

            Assert.Equal(7, collection.Count);
        }
        public void Test_AddRange_with_null_values()
        {
            var collection = new IntCollection()
            {
                1, 2, 3, 4, 5
            };

            collection.AddRange(null);

            Assert.Equal(5, collection.Count);
        }
        public static BoneSkinnedMeshGeometry3D CreateSkeletonMesh(IList <Animations.Bone> bones, float scale = 1f)
        {
            var builder = new MeshBuilder(true, false);

            builder.AddPyramid(new Vector3(0, scale / 2, 0), Vector3.UnitZ, Vector3.UnitY, scale, 0, true);
            var singleBone = builder.ToMesh();
            var boneIds    = new List <BoneIds>();
            var positions  = new Vector3Collection(bones.Count * singleBone.Positions.Count);
            var tris       = new IntCollection(bones.Count * singleBone.Indices.Count);

            int offset = 0;

            for (int i = 0; i < bones.Count; ++i)
            {
                if (bones[i].ParentIndex >= 0)
                {
                    int currPos = positions.Count;
                    tris.AddRange(singleBone.Indices.Select(x => x + offset));
                    int j = 0;
                    for (; j < singleBone.Positions.Count - 6; j += 3)
                    {
                        positions.Add(Vector3.TransformCoordinate(singleBone.Positions[j], bones[bones[i].ParentIndex].BindPose));
                        positions.Add(Vector3.TransformCoordinate(singleBone.Positions[j + 1], bones[bones[i].ParentIndex].BindPose));
                        positions.Add(bones[i].BindPose.TranslationVector);
                        boneIds.Add(new BoneIds()
                        {
                            Bone1 = bones[i].ParentIndex, Weights = new Vector4(1, 0, 0, 0)
                        });
                        boneIds.Add(new BoneIds()
                        {
                            Bone1 = bones[i].ParentIndex, Weights = new Vector4(1, 0, 0, 0)
                        });
                        boneIds.Add(new BoneIds()
                        {
                            Bone1 = i, Weights = new Vector4(1, 0, 0, 0)
                        });
                    }
                    for (; j < singleBone.Positions.Count; ++j)
                    {
                        positions.Add(Vector3.TransformCoordinate(singleBone.Positions[j], bones[bones[i].ParentIndex].BindPose));
                        boneIds.Add(new BoneIds()
                        {
                            Bone1 = bones[i].ParentIndex, Weights = new Vector4(1, 0, 0, 0)
                        });
                    }
                    offset += singleBone.Positions.Count;
                }
            }

            builder = new MeshBuilder(true, false);
            for (int i = 0; i < bones.Count; ++i)
            {
                int currPos = builder.Positions.Count;
                builder.AddSphere(Vector3.Zero, scale / 2, 12, 12);
                for (int j = currPos; j < builder.Positions.Count; ++j)
                {
                    builder.Positions[j] = Vector3.TransformCoordinate(builder.Positions[j], bones[i].BindPose);
                    boneIds.Add(new BoneIds()
                    {
                        Bone1 = i, Weights = new Vector4(1, 0, 0, 0)
                    });
                }
            }
            positions.AddRange(builder.Positions);
            tris.AddRange(builder.TriangleIndices.Select(x => x + offset));
            var mesh = new BoneSkinnedMeshGeometry3D()
            {
                Positions = positions, Indices = tris, VertexBoneIds = boneIds
            };

            mesh.Normals = mesh.CalculateNormals();
            return(mesh);
        }