///<summary>
		///  Adds an Entity to the static geometry.
		///  <remarks>
		///    This method takes an existing Entity and adds its details to the list of elements to include when building. Note that the Entity itself is not copied or referenced in this method; an Entity is passed simply so that you can change the materials of attached SubEntity objects if you want. You can add the same Entity instance multiple times with different material settings completely safely, and destroy the Entity before destroying this InstancedGeometry if you like. The Entity passed in is simply Must be called before 'Build'.
		///  </remarks>
		///</summary>
		///<param name="ent"> The Entity to use as a definition (the Mesh and Materials referenced will be recorded for the build call). </param>
		///<param name="position"> The world position at which to add this Entity </param>
		///<param name="orientation"> The world orientation at which to add this Entity </param>
		///<param name="scale"> </param>
		public virtual void AddEntity( Entity ent, Vector3 position, Quaternion orientation, Vector3 scale )
		{
			Mesh msh = ent.Mesh;

			// Validate
			if ( msh.IsLodManual )
			{
				LogManager.Instance.Write(
					"(InstancedGeometry): Manual LOD is not supported. Using only highest LOD level for mesh " + msh.Name );
			}

			//get the skeleton of the entity, if that's not already done
			if ( ent.Mesh.Skeleton != null && mBaseSkeleton == null )
			{
				mBaseSkeleton = ent.Mesh.Skeleton;
				mSkeletonInstance = new SkeletonInstance( mBaseSkeleton );
				mSkeletonInstance.Load();
				mAnimationState = ent.GetAllAnimationStates();
			}

			BoundingBox sharedWorldBounds;
			// queue this entities submeshes and choice of material
			// also build the lists of geometry to be used for the source of lods


			for ( int i = 0; i < ent.SubEntityCount; ++i )
			{
				SubEntity se = ent.GetSubEntity( i );
				var q = new QueuedSubMesh();

				// Get the geometry for this SubMesh
				q.submesh = se.SubMesh;
				q.geometryLodList = DetermineGeometry( q.submesh );
				q.materialName = se.MaterialName;
				q.orientation = orientation;
				q.position = position;
				q.scale = scale;
				q.ID = mObjectCount;
			}

			mObjectCount++;
		}
        protected void SetMesh(Mesh mesh)
        {
            this.mesh = mesh;

            if (mesh.HasSkeleton && mesh.Skeleton != null) {
                skeletonInstance = new SkeletonInstance(mesh.Skeleton);
                skeletonInstance.Load();
            } else
                skeletonInstance = null;

            this.subEntityList.Clear();
            BuildSubEntities();

            lodEntityList.Clear();
            // Check if mesh is using manual LOD
            if (mesh.IsLodManual) {
                for (int i = 1; i < mesh.LodLevelCount; i++) {
                    MeshLodUsage usage = mesh.GetLodLevel(i);

                    // manually create entity
                    Entity lodEnt = new Entity(string.Format("{0}Lod{1}", name, i), usage.manualMesh, sceneMgr);
                    lodEntityList.Add(lodEnt);
                }
            }

            animationState.RemoveAllAnimationStates();
            // init the AnimationState, if the mesh is animated
            if (HasSkeleton) {
                numBoneMatrices = skeletonInstance.BoneCount;
                boneMatrices = new Matrix4[numBoneMatrices];
            }
            if (HasSkeleton || mesh.HasVertexAnimation) {
                mesh.InitAnimationState(animationState);
                PrepareTempBlendedBuffers();
            }

            ReevaluateVertexProcessing();

            // LOD default settings
            meshLodFactorInv = 1.0f;
            // Backwards, remember low value = high detail
            minMeshLodIndex = 99;
            maxMeshLodIndex = 0;

            // Material LOD default settings
            materialLodFactorInv = 1.0f;
            maxMaterialLodIndex = 0;
            minMaterialLodIndex = 99;

            // Do we have a mesh where edge lists are not going to be available?
            if (((sceneMgr.ShadowTechnique == ShadowTechnique.StencilAdditive) || (sceneMgr.ShadowTechnique == ShadowTechnique.StencilModulative)) &&
                !mesh.IsEdgeListBuilt && !mesh.AutoBuildEdgeLists) {
                this.CastShadows = false;
            }
        }