/// <summary> /// Clone the this instance /// </summary> /// <returns>The cloned instance</returns> public InternalStaticModel Clone() { var newModel = new InternalStaticModel(); newModel.AssetPath = this.AssetPath; newModel.graphics = this.graphics; newModel.BoundingBox = this.BoundingBox; newModel.MeshBonePairs = new Dictionary <int, int>(this.MeshBonePairs); newModel.Bones = new List <Bone>(this.Bones); for (int i = 0; i < this.Meshes.Count; i++) { Mesh currentMesh = this.Meshes[i]; VertexBuffer currentBuffer = currentMesh.VertexBuffer as VertexBuffer; var newBuffer = new VertexBuffer(currentBuffer.VertexBufferFormat); Matrix identity = Matrix.Identity; newBuffer.AppendBuffer(currentBuffer, ref identity); var newIndexBuffer = new IndexBuffer(currentMesh.IndexBuffer.Data); var newMesh = new Mesh( currentMesh.VertexOffset, currentMesh.NumVertices, currentMesh.IndexOffset, currentMesh.NumPrimitives, newBuffer, newIndexBuffer, currentMesh.PrimitiveType); newMesh.Name = currentMesh.Name; newModel.Meshes.Add(newMesh); } return(newModel); }
/// <summary> /// Refresh primitive model /// </summary> private void RefreshPrimitive() { switch (this.modelType) { case ModelType.Custom: this.isPrimitive = false; this.UnloadModel(); this.LoadModel(); break; case ModelType.Capsule: this.InternalModel = new InternalStaticModel(); this.InternalModel.FromPrimitive(WaveServices.GraphicsDevice, new Capsule(1.0f, 0.5f, 16)); break; case ModelType.Cone: this.InternalModel = new InternalStaticModel(); this.InternalModel.FromPrimitive(WaveServices.GraphicsDevice, new Cone(1.0f, 1.0f, 16)); break; case ModelType.Cube: this.InternalModel = new InternalStaticModel(); this.InternalModel.FromPrimitive(WaveServices.GraphicsDevice, new Cube(1.0f)); break; case ModelType.Cylinder: this.InternalModel = new InternalStaticModel(); this.InternalModel.FromPrimitive(WaveServices.GraphicsDevice, new Cylinder(1.0f, 1.0f, 16)); break; case ModelType.Plane: this.InternalModel = new InternalStaticModel(); this.InternalModel.FromPrimitive(WaveServices.GraphicsDevice, new Primitives.Plane(Vector3.UnitY, 1.0f)); break; case ModelType.Pyramid: this.InternalModel = new InternalStaticModel(); this.InternalModel.FromPrimitive(WaveServices.GraphicsDevice, new Pyramid(1.0f)); break; case ModelType.Sphere: this.InternalModel = new InternalStaticModel(); this.InternalModel.FromPrimitive(WaveServices.GraphicsDevice, new Sphere(1.0f, 16)); break; case ModelType.Teapot: this.InternalModel = new InternalStaticModel(); this.InternalModel.FromPrimitive(WaveServices.GraphicsDevice, new Teapot(1.0f, 16)); break; case ModelType.Torus: this.InternalModel = new InternalStaticModel(); this.InternalModel.FromPrimitive(WaveServices.GraphicsDevice, new Torus(1.0f, 0.333f, 16)); break; default: break; } }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { if (this.isPrimitive && this.InternalModel != null) { this.InternalModel.Unload(); this.InternalModel = null; } }
/// <summary> /// Performs further custom initialization for this instance. /// </summary> protected override void Initialize() { if (string.IsNullOrEmpty(this.ModelPath)) { this.BoundingBox = this.InternalModel.BoundingBox; } else { this.InternalModel = Assets.LoadAsset <InternalStaticModel>(this.ModelPath); this.BoundingBox = this.InternalModel.BoundingBox; } }
/// <summary> /// Draws the model. /// </summary> /// <param name="gameTime">The elapsed game time.</param> public override void Draw(TimeSpan gameTime) { if (this.lastModelId != this.Model.GetHashCode()) { this.meshMaterials = new Material[this.Model.InternalModel.Meshes.Count]; this.lastModelId = this.Model.GetHashCode(); } float zOrder = Vector3.DistanceSquared(this.RenderManager.CurrentDrawingCamera3D.Position, this.Transform.Position); for (int i = 0; i < this.Model.InternalModel.Meshes.Count; i++) { StaticMesh currentMesh = this.Model.InternalModel.Meshes[i]; Material currentMaterial; if (currentMesh.Name != null && this.MaterialMap.Materials.ContainsKey(currentMesh.Name)) { currentMaterial = this.MaterialMap.Materials[currentMesh.Name]; } else { currentMaterial = this.MaterialMap.DefaultMaterial; } this.meshMaterials[i] = currentMaterial; if (currentMaterial != null) { Matrix world; if (!this.Owner.IsStatic) { // Obtain world matrix from scrach InternalStaticModel internalModel = this.Model.InternalModel; int index = internalModel.MeshBonePairs[i]; Matrix absoluteTransform = internalModel.Bones[index].AbsoluteTransform; Matrix worldTransform = this.Transform.WorldTransform; Matrix.Multiply(ref absoluteTransform, ref worldTransform, out world); } else { // obtain world from cached array world = this.cachedWorlds[i]; } currentMesh.ZOrder = zOrder; // Draw mesh this.RenderManager.DrawMesh(currentMesh, currentMaterial, ref world, this.Owner.IsStatic); } } }
/// <summary> /// Load the static model /// </summary> protected override void LoadModel() { if (this.isPrimitive) { return; } if (this.Assets != null && !string.IsNullOrEmpty(this.ModelPath)) { this.InternalModel = this.Assets.LoadAsset <InternalStaticModel>(this.ModelPath); this.BoundingBox = this.InternalModel.BoundingBox; } }
/// <summary> /// Initialize model renderer /// </summary> protected override void Initialize() { base.Initialize(); // For static entities create a world cache if (this.Owner.IsFinalStatic) { this.cachedWorlds = new Matrix[this.Model.MeshCount]; for (int i = 0; i < this.cachedWorlds.Length; i++) { InternalStaticModel internalModel = this.Model.InternalModel; int index = internalModel.MeshBonePairs[i]; Matrix absoluteTransform = internalModel.Bones[index].AbsoluteTransform; Matrix world = this.Transform.WorldTransform; Matrix.Multiply(ref absoluteTransform, ref world, out this.cachedWorlds[i]); } } }
/// <summary> /// Unload the static model /// </summary> protected override void UnloadModel() { if (this.InternalModel == null) { return; } if (!string.IsNullOrEmpty(this.InternalModel.AssetPath)) { if (this.Assets != null) { this.Assets.UnloadAsset(this.InternalModel.AssetPath); } } else { this.InternalModel.Unload(); } this.InternalModel = null; this.BoundingBox = new BoundingBox(); }