Example #1
0
        /// <summary>
        /// Refresh the internal states when a model changes.
        /// </summary>
        private void OnInitialized()
        {
            skeleton          = null;
            geometryPositions = null;
            geometryIndices   = null;
            animations        = null;
            boundingBox       = new BoundingBox();
            if (orientedBoundingBoxOverride.HasValue)
            {
                OrientedBoundingBox = orientedBoundingBoxOverride.Value;
            }
            else
            {
                orientedBoundingBox = new BoundingBox();
            }

            if (source != null)
            {
                if (source.Meshes.Count <= 0 || source.Meshes[0].MeshParts.Count <= 0 || source.Meshes[0].MeshParts[0].VertexBuffer == null)
                {
                    throw new ArgumentException("The input model is must have at least 1 valid mesh part.");
                }

                // Initialize bounds
                if (!orientedBoundingBoxOverride.HasValue)
                {
                    orientedBoundingBox = source.ComputeBoundingBox();
                }
                boundingBox = orientedBoundingBox.CreateAxisAligned(AbsoluteTransform);

                // Initialize animations
                skeleton  = new ModelSkeleton(source);
                IsSkinned = source.IsSkinned();

                var animationNames = source.GetAnimations();
                if (animationNames.Count > 0)
                {
                    animations = new AnimationPlayer();
                    animationNames.ForEach(anim => animations.Animations.Add(anim, new BoneAnimation(Skeleton, source.GetAnimation(anim))));
                    animations.Play();
                }

                // Initialize parts
                var iPart = 0;
                foreach (var mesh in source.Meshes)
                {
                    foreach (var part in mesh.MeshParts)
                    {
                        if (modelMeshes.Count <= iPart)
                        {
                            modelMeshes.Add(new ModelMesh());
                        }
                        if (modelMeshes[iPart] == null)
                        {
                            modelMeshes[iPart] = new ModelMesh();
                        }
                        modelMeshes[iPart].Attach(this, mesh, part);
                        iPart++;
                    }
                }

                // Populate child nodes
                children.Clear();
                for (int i = 0; i < modelMeshes.Count; i++)
                {
                    children.Add(modelMeshes[i]);
                }
                if (attachments != null)
                {
                    for (int i = 0; i < attachments.Count; ++i)
                    {
                        if (attachments[i].Transformable != null)
                        {
                            children.Add(attachments[i].Transformable);
                        }
                    }
                }

                UpdateBoneTransforms();
            }
        }