Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- namespace XNA_Model
- {
- public interface IRenderable
- {
- void Draw(Matrix View, Matrix Projection, Vector3 CameraPosition);
- }
- public class CModel : IRenderable
- {
- private GraphicsDevice m_graphicsDevice;
- public Model Model { get; private set; }
- public Vector3 Position { get; set; }
- public Vector3 Rotation { get; set; }
- public Vector3 Scale { get; set; }
- private Matrix[] m_transforms;
- private BoundingSphere m_boundingSphere;
- public BoundingSphere BoundingSphere
- {
- get { return m_boundingSphere.Transform(Matrix.CreateScale(Scale) * Matrix.CreateTranslation(Position)); }
- }
- public CModel(Model Model, Vector3 Position, Vector3 Rotation, Vector3 Scale, GraphicsDevice graphicsDevice)
- {
- this.m_graphicsDevice = graphicsDevice;
- this.Model = Model;
- m_transforms = new Matrix[Model.Bones.Count];
- Model.CopyAbsoluteBoneTransformsTo(m_transforms);
- BuildBoundingSphere();
- GenerateTags();
- this.Position = Position;
- this.Rotation = Rotation;
- this.Scale = Scale;
- }
- private void BuildBoundingSphere()
- {
- BoundingSphere sphere = new BoundingSphere(Vector3.Zero, 0);
- foreach (ModelMesh mesh in Model.Meshes)
- {
- BoundingSphere transformed = mesh.BoundingSphere.Transform(
- m_transforms[mesh.ParentBone.Index]);
- sphere = BoundingSphere.CreateMerged(sphere, transformed);
- }
- this.m_boundingSphere = sphere;
- }
- private void GenerateTags()
- {
- foreach (ModelMesh mesh in Model.Meshes)
- foreach (ModelMeshPart part in mesh.MeshParts)
- if (part.Effect is BasicEffect)
- {
- BasicEffect effect = (BasicEffect)part.Effect;
- MeshTag tag = new MeshTag(effect.DiffuseColor, effect.Texture, effect.SpecularPower);
- part.Tag = tag;
- }
- }
- public void Draw(Matrix View, Matrix Projection, Vector3 CameraPosition)
- {
- Matrix baseWorld = Matrix.CreateScale(Scale) * Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z) * Matrix.CreateTranslation(Position);
- foreach (ModelMesh mesh in Model.Meshes)
- {
- Matrix localWorld = m_transforms[mesh.ParentBone.Index] * baseWorld;
- foreach (ModelMeshPart meshPart in mesh.MeshParts)
- {
- Effect effect = meshPart.Effect;
- if (effect is BasicEffect)
- {
- ((BasicEffect)effect).World = localWorld;
- ((BasicEffect)effect).View = View;
- ((BasicEffect)effect).Projection = Projection;
- ((BasicEffect)effect).EnableDefaultLighting();
- }
- else
- {
- SetEffectParameter(effect, "World", localWorld);
- SetEffectParameter(effect, "View", View);
- SetEffectParameter(effect, "Projection", Projection);
- SetEffectParameter(effect, "CameraPosition", CameraPosition);
- ((MeshTag)meshPart.Tag).Material.SetEffectParameters(effect);
- }
- }
- mesh.Draw();
- }
- }
- void SetEffectParameter(Effect effect, string paraName, object val)
- {
- if (effect.Parameters[paraName] == null)
- return;
- if (val is Vector3)
- effect.Parameters[paraName].SetValue((Vector3)val);
- else if (val is bool)
- effect.Parameters[paraName].SetValue((bool)val);
- else if (val is Matrix)
- effect.Parameters[paraName].SetValue((Matrix)val);
- else if (val is Texture2D)
- effect.Parameters[paraName].SetValue((Texture2D)val);
- }
- public void SetModelEffect(Effect effect, bool CopyEffect)
- {
- foreach (ModelMesh mesh in Model.Meshes)
- SetMeshEffect(mesh.Name, effect, CopyEffect);
- }
- public void SetModelMaterial(Material material)
- {
- foreach (ModelMesh mesh in Model.Meshes)
- SetMeshMaterial(mesh.Name, material);
- }
- public void SetMeshEffect(string MeshName, Effect effect, bool CopyEffect)
- {
- foreach (ModelMesh mesh in Model.Meshes)
- {
- if (mesh.Name != MeshName)
- continue;
- foreach (ModelMeshPart part in mesh.MeshParts)
- {
- Effect toSet = effect;
- if (CopyEffect)
- toSet = effect.Clone();
- MeshTag tag = ((MeshTag)part.Tag);
- if (tag.Texture != null)
- {
- SetEffectParameter(toSet, "BasicTexture", tag.Texture);
- SetEffectParameter(toSet, "TextureEnabled", true);
- }
- else
- SetEffectParameter(toSet, "TextureEnabled", false);
- SetEffectParameter(toSet, "DiffuseColor", tag.Color);
- SetEffectParameter(toSet, "SpecularPower", tag.SpecularPower);
- part.Effect = toSet;
- }
- }
- }
- public void SetMeshMaterial(string MeshName, Material material)
- {
- foreach (ModelMesh mesh in Model.Meshes)
- {
- if (mesh.Name != MeshName)
- continue;
- foreach (ModelMeshPart meshPart in mesh.MeshParts)
- ((MeshTag)meshPart.Tag).Material = material;
- }
- }
- public void VomitBoneNames()
- {
- foreach (ModelMesh mesh in Model.Meshes)
- {
- Console.WriteLine(mesh.Name);
- }
- }
- public void CacheEffects()
- {
- foreach (ModelMesh mesh in Model.Meshes)
- foreach (ModelMeshPart part in mesh.MeshParts)
- ((MeshTag)part.Tag).CachedEffect = part.Effect;
- }
- public void RestoreEffects()
- {
- foreach (ModelMesh mesh in Model.Meshes)
- foreach (ModelMeshPart part in mesh.MeshParts)
- if (((MeshTag)part.Tag).CachedEffect != null)
- part.Effect = ((MeshTag)part.Tag).CachedEffect;
- }
- }
- public class MeshTag
- {
- public Vector3 Color;
- public Texture2D Texture;
- public float SpecularPower;
- public Effect CachedEffect = null;
- public Material Material = new Material();
- public MeshTag(Vector3 Color, Texture2D Texture, float SpecularPower)
- {
- this.Color = Color;
- this.Texture = Texture;
- this.SpecularPower = SpecularPower;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement