/// <summary> /// 開いて初期化 /// </summary> /// <param name="filePath">PMXファイルのパス</param> /// <param name="loader">テクスチャなどのパスの解決インターフェース</param> /// <param name="panel">レンダリングコンテキスト</param> /// <returns>MMDModel.</returns> public static PMXModel OpenLoad(string filePath, ISubresourceLoader loader, RenderControl panel) { PMXModel model = FromFile(filePath, loader); model.Load(panel.RenderContext); return(model); }
public new static PMXModel FromFile(string filePath, ISubresourceLoader loader) { using (FileStream fs = File.OpenRead(filePath)) { return(new PMXModelWithPhysics(ModelData.GetModel(fs), loader, Path.GetFileName(filePath))); } }
/// <summary> /// AssimpModelを初期化します /// </summary> /// <param name="context"></param> /// <param name="fileName"></param> /// <param name="modelScene">PostProcessSteps.Triangulate|PostProcessSteps.GenerateNormalsを指定して読み込んだSceneである必要があります。</param> /// <param name="loader"></param> public AssimpModel(RenderContext context, string fileName, Scene modelScene, ISubresourceLoader loader) { this.modelScene = modelScene; this.context = context; this.FileName = fileName; this.loader = loader; Visibility = true; Initialize(); }
public AssimpModel(RenderContext context, string fileName) { this.FileName = Path.GetFileName(fileName); this.context = context; loader = new BasicSubresourceLoader(Path.GetDirectoryName(fileName)); var importer = new AssimpContext(); modelScene = importer.ImportFile(fileName, PostProcessSteps.Triangulate | PostProcessSteps.GenerateSmoothNormals); Visibility = true; Initialize(); }
public AssimpSubset(RenderContext context, ISubresourceLoader loader, IDrawable drawable, Scene scene, int index) { this.SubsetId = index; this.context = context; this.DoCulling = false; this.Drawable = drawable; Material material = scene.Materials[scene.Meshes[index].MaterialIndex]; this.mesh = scene.Meshes[index]; Initialize(); this.MaterialInfo = MaterialInfo.FromMaterialData(drawable, material, context, loader); }
/// <summary> /// Initializes a new instance of the <see cref="PMXModel"/> class. /// </summary> /// <param name="modeldata">The modeldata.</param> /// <param name="subResourceLoader">The sub resource loader.</param> /// <param name="filename">The filename.</param> public PMXModel(ModelData modeldata, ISubresourceLoader subResourceLoader, string filename) { Model = modeldata; SubresourceLoader = subResourceLoader; Transformer = new BasicTransformer(); SubsetManager = new PMXSubsetManager(this, modeldata); //PhongConstantBuffer = new PhongShadingConstantBufferManager(); ToonManager = new PMXToonTextureManager(); SelfShadowColor = new Vector4(0, 0, 0, 1); GroundShadowColor = new Vector4(0, 0, 0, 1); FileName = filename; Visibility = true; }
public void Initialize(RenderContext context, ISubresourceLoader subresourceManager) { _device = context.DeviceManager.Device; _subresourceManager = subresourceManager; for (int i = 0; i < 11; i++) { string path = CGHelper.ToonDir + string.Format(@"toon{0}.bmp", i); if (File.Exists(path)) { resourceViewsList.Add(ShaderResourceView.FromFile(context.DeviceManager.Device, path)); } else { } } }
public static MaterialInfo FromMaterialData(IDrawable drawable, Material material, RenderContext context, ISubresourceLoader loader) { MaterialInfo info = new MaterialInfo(); info.AmbientColor = material.ColorAmbient.ToSlimDX(); if (info.AmbientColor == Vector4.Zero) { info.AmbientColor = new Vector4(1f, 1f, 1f, 1f); } info.DiffuseColor = material.ColorDiffuse.ToSlimDX(); if (info.DiffuseColor == Vector4.Zero) { info.DiffuseColor = new Vector4(1f, 1f, 1f, 1f); } else if (info.DiffuseColor.W == 0f) { info.DiffuseColor.W = 1f; } info.EmissiveColor = material.ColorEmissive.ToSlimDX(); info.SpecularColor = material.ColorSpecular.ToSlimDX(); info.SpecularPower = material.ShininessStrength; if (info.SpecularColor == Vector4.Zero) { info.SpecularColor = new Vector4(0.1f); } info.GroundShadowColor = drawable.GroundShadowColor; info.isEdgeEnable = false; info.isGroundShadowEnable = true; info.SphereMode = SphereMode.Disable; info.IsToonUsed = false; if (material.GetTextures(TextureType.Diffuse) != null) { Stream stream = loader.getSubresourceByName(material.GetTextures(TextureType.Diffuse)[0].FilePath); if (stream != null) { using ( Texture2D texture = Texture2D.FromStream(context.DeviceManager.Device, stream, (int)stream.Length)) { info.MaterialTexture = new ShaderResourceView(context.DeviceManager.Device, texture); } } stream.Close(); } return(info); }
public void Initialze(RenderContext context, MMEEffectManager effectManager, ISubresourceLoader subresourceManager, IToonTextureManager ToonManager) { MMEEffect = effectManager; toonManager = ToonManager; Subsets = new List <ISubset>(); this.device = context.DeviceManager.Device; this.subresourceManager = subresourceManager; ModelData model = this.model; int vertexSum = 0; for (int i = 0; i < model.MaterialList.MaterialCount; i++) { MaterialData material = model.MaterialList.Materials[i];//サブセットデータを作成する PMXSubset dr = new PMXSubset(Drawable, material, i); dr.DoCulling = !material.bitFlag.HasFlag(RenderFlag.CullNone); dr.VertexCount = material.VertexNumber / 3; dr.StartIndex = vertexSum; if (material.textureIndex >= toonManager.ResourceViews.Length) { if (toonManager.ResourceViews.Length == 0) { int index = ToonManager.LoadToon(model.TextureList.TexturePathes[material.textureIndex]); dr.MaterialInfo.ToonTexture = ToonManager.ResourceViews[index]; dr.MaterialInfo.IsToonUsed = false; } else { dr.MaterialInfo.ToonTexture = ToonManager.ResourceViews[0]; dr.MaterialInfo.IsToonUsed = false; } } else { if (material.ShareToonFlag == 1) { dr.MaterialInfo.ToonTexture = ToonManager.ResourceViews[material.textureIndex + 1]; dr.MaterialInfo.IsToonUsed = true; } else if (material.textureIndex != -1) { if (model.TextureList.TexturePathes.Count < material.textureIndex + 1) { dr.MaterialInfo.ToonTexture = ToonManager.ResourceViews[0]; dr.MaterialInfo.IsToonUsed = true; } else { int index = ToonManager.LoadToon(model.TextureList.TexturePathes[material.textureIndex]); dr.MaterialInfo.ToonTexture = ToonManager.ResourceViews[index]; dr.MaterialInfo.IsToonUsed = true; } } else { dr.MaterialInfo.ToonTexture = ToonManager.ResourceViews[0]; dr.MaterialInfo.IsToonUsed = true; } } vertexSum += material.VertexNumber; //テクスチャの読み込み if (material.TextureTableReferenceIndex != -1) { dr.MaterialInfo.MaterialTexture = getSubresourceById(model.TextureList.TexturePathes[material.TextureTableReferenceIndex]); } //スフィアマップの読み込み if (material.SphereTextureTableReferenceIndex != -1) { dr.MaterialInfo.SphereMode = material.SphereMode; dr.MaterialInfo.MaterialSphereMap = getSubresourceById(model.TextureList.TexturePathes[material.SphereTextureTableReferenceIndex]); } Subsets.Add(dr); } }
public PMXModelWithPhysics(ModelData modeldata, ISubresourceLoader subResourceLoader, string filename) : base(modeldata, subResourceLoader, filename) { }
/// <summary> /// MMEのエフェクトとして読み込む /// </summary> /// <param name="str">エフェクトのパス</param> /// <param name="model">使用対象のモデル</param> /// <param name="context"></param> /// <param name="loader"></param> /// <param name="device">デバイス</param> /// <returns>MME仕様のエフェクト</returns> internal static MMEEffectManager LoadFromResource(string str, IDrawable model, RenderContext context, ISubresourceLoader loader) { SlimDX.Direct3D11.Effect effect = CGHelper.CreateEffectFx5FromResource(str, context.DeviceManager.Device); return(new MMEEffectManager(str, context, effect, model, loader)); }
/// <summary> /// MMEのエフェクトとして読み込む /// </summary> /// <param name="str">エフェクトのパス</param> /// <param name="model">使用対象のモデル</param> /// <param name="context"></param> /// <param name="loader"></param> /// <param name="device">デバイス</param> /// <returns>MME仕様のエフェクト</returns> public static MMEEffectManager Load(string str, IDrawable model, RenderContext context, ISubresourceLoader loader) { return(new MMEEffectManager(str, context, CGHelper.CreateEffectFx5(str, context.DeviceManager.Device), model, loader)); }
/// <summary> /// コンストラクタ /// </summary> /// <param name="fileName"></param> /// <param name="context"></param> /// <param name="effect">解釈対象のエフェクト</param> /// <param name="model">使用対象のモデル</param> /// <param name="loader"></param> private MMEEffectManager(string fileName, RenderContext context, SlimDX.Direct3D11.Effect effect, IDrawable model, ISubresourceLoader loader) { this.fileName = fileName; SubresourceLoader = loader; if (!fileName.Equals(MMFDefaultShaderResourcePath)) { //ファイルパスがデフォルトのシェーダーと等しくない時は、デフォルトのシェーダーも読み込んでおく。 DefaultShader = LoadFromResource(MMFDefaultShaderResourcePath, model, context, new BasicSubresourceLoader("Shader")); } else { DefaultShader = this; //等しい時は自身がデフォルトのシェーダーと等しい。 } Context = context; EffectFile = effect; EffectInfo = new MMEEffectInfo(effect); ActiveSubscriberByMaterial = new Dictionary <EffectVariable, SubscriberBase>(); ActiveSubscriberByModel = new Dictionary <EffectVariable, SubscriberBase>(); ActivePeculiarSubscriber = new Dictionary <EffectVariable, PeculiarValueSubscriberBase>(); Techniques = new List <MMEEffectTechnique>(); RenderColorTargetViewes = new Dictionary <string, RenderTargetView>(); RenderDepthStencilTargets = new Dictionary <string, DepthStencilView>(); this.model = model; //グローバル変数の登録 int valCount = effect.Description.GlobalVariableCount; for (int i = 0; i < valCount; i++) { string semantic = Regex.Replace(effect.GetVariableByIndex(i).Description.Semantic, "[0-9]", ""); string semanticIndexStr = Regex.Replace(effect.GetVariableByIndex(i).Description.Semantic, "[^0-9]", ""); int semanticIndex = string.IsNullOrEmpty(semanticIndexStr)?0:int.Parse(semanticIndexStr); string typeName = effect.GetVariableByIndex(i).GetVariableType().Description.TypeName.ToLower(); semantic = semantic.ToUpper(); //小文字でもいいようにするため大文字に全て変換 if (EffectSubscriber.ContainsKey(semantic)) { //通常はセマンティクスに応じて登録 SubscriberBase subs = EffectSubscriber[semantic]; EffectVariable variable = effect.GetVariableByIndex(i); subs.CheckType(variable); if (subs.UpdateTiming == UpdateBy.Material) { ActiveSubscriberByMaterial.Add(variable, subs.GetSubscriberInstance(variable, context, this, semanticIndex)); } else { ActiveSubscriberByModel.Add(variable, subs.GetSubscriberInstance(variable, context, this, semanticIndex)); } } else if (typeName.Equals("texture") || typeName.Equals("texture2d") || typeName.Equals("texture3d") || typeName.Equals("texturecube")) //テクスチャのみ例外で、変数型に応じて登録する { SubscriberBase subs = new TextureSubscriber(); EffectVariable variable = effect.GetVariableByIndex(i); subs.CheckType(variable); ActiveSubscriberByModel.Add(variable, subs.GetSubscriberInstance(variable, context, this, semanticIndex)); } else//特殊変数は変数名に応じて登録する { string name = effect.GetVariableByIndex(i).Description.Name; name = name.ToLower(); if (PeculiarEffectSubscriber.ContainsKey(name)) { ActivePeculiarSubscriber.Add(effect.GetVariableByIndex(i), PeculiarEffectSubscriber[name]); } } } //定数バッファの登録 valCount = effect.Description.ConstantBufferCount; for (int i = 0; i < valCount; i++) { string name = effect.GetConstantBufferByIndex(i).Description.Name; name = name.ToUpper(); if (EffectSubscriber.ContainsKey(name)) { SubscriberBase subs = EffectSubscriber[name]; //定数バッファにはセマンティクスが付けられないため、変数名で代用 EffectConstantBuffer variable = effect.GetConstantBufferByIndex(i); subs.CheckType(variable); if (subs.UpdateTiming == UpdateBy.Material) { ActiveSubscriberByMaterial.Add(variable, subs.GetSubscriberInstance(variable, context, this, 0)); } else { ActiveSubscriberByModel.Add(variable, subs.GetSubscriberInstance(variable, context, this, 0)); } } } int subsetCount = model is ISubsetDivided ? ((ISubsetDivided)model).SubsetCount : 1; foreach (EffectTechnique t in EffectInfo.SortedTechnique)//MMEEffectTechniqueもソートしておく { //テクニックをすべて読みだす Techniques.Add(new MMEEffectTechnique(this, t, subsetCount, context)); } }
/// <summary> /// Constructor /// </summary> /// <param name="fileName"></param> /// <param name="context"></param> /// <param name="effect">Effects of being interpreted</param> /// <param name="model">Want to use models</param> /// <param name="loader"></param> private MMEEffectManager(string fileName, RenderContext context, Effect effect, IDrawable model, ISubresourceLoader loader) { this.fileName = fileName; this.SubresourceLoader = loader; if (!fileName.Equals(MMFDefaultShaderResourcePath)) { //The default shaders loaded and the default file path is not equal:。 this.DefaultShader = LoadFromResource(MMFDefaultShaderResourcePath, model, context, new BasicSubresourceLoader("Shader")); } else { this.DefaultShader = this; //Equal to itself is equal to the default shader。 } this.Context = context; this.EffectFile = effect; this.EffectInfo = new MMEEffectInfo(effect); this.ActiveSubscriberByMaterial = new Dictionary <EffectVariable, SubscriberBase>(); this.ActiveSubscriberByModel = new Dictionary <EffectVariable, SubscriberBase>(); this.ActivePeculiarSubscriber = new Dictionary <EffectVariable, PeculiarValueSubscriberBase>(); this.Techniques = new List <MMEEffectTechnique>(); this.RenderColorTargetViewes = new Dictionary <string, RenderTargetView>(); this.RenderDepthStencilTargets = new Dictionary <string, DepthStencilView>(); this.model = model; //Creating global variables int valCount = effect.Description.GlobalVariableCount; for (int i = 0; i < valCount; i++) { string semantic = Regex.Replace(effect.GetVariableByIndex(i).Description.Semantic, "[0-9]", ""); string semanticIndexStr = Regex.Replace(effect.GetVariableByIndex(i).Description.Semantic, "[^0-9]", ""); int semanticIndex = string.IsNullOrEmpty(semanticIndexStr)?0:int.Parse(semanticIndexStr); string typeName = effect.GetVariableByIndex(i).GetVariableType().Description.TypeName.ToLower(); semantic = semantic.ToUpper(); //A good case for all converted to uppercase if (EffectSubscriber.ContainsKey(semantic)) { //Usually the register according to the semantics SubscriberBase subs = EffectSubscriber[semantic]; EffectVariable variable = effect.GetVariableByIndex(i); subs.CheckType(variable); if (subs.UpdateTiming == UpdateBy.Material) { this.ActiveSubscriberByMaterial.Add(variable, subs.GetSubscriberInstance(variable, context, this, semanticIndex)); } else { this.ActiveSubscriberByModel.Add(variable, subs.GetSubscriberInstance(variable, context, this, semanticIndex)); } } else if (typeName.Equals("texture") || typeName.Equals("texture2d") || typeName.Equals("texture3d") || typeName.Equals("texturecube")) //Textures only in exceptions, depending on the variable type to register { SubscriberBase subs = new TextureSubscriber(); EffectVariable variable = effect.GetVariableByIndex(i); subs.CheckType(variable); this.ActiveSubscriberByModel.Add(variable, subs.GetSubscriberInstance(variable, context, this, semanticIndex)); } else//Special variable to register according to the variable name { string name = effect.GetVariableByIndex(i).Description.Name; name = name.ToLower(); if (PeculiarEffectSubscriber.ContainsKey(name)) { this.ActivePeculiarSubscriber.Add(effect.GetVariableByIndex(i), PeculiarEffectSubscriber[name]); } } } //Constant buffer register valCount = effect.Description.ConstantBufferCount; for (int i = 0; i < valCount; i++) { string name = effect.GetConstantBufferByIndex(i).Description.Name; name = name.ToUpper(); if (EffectSubscriber.ContainsKey(name)) { SubscriberBase subs = EffectSubscriber[name]; //A constant buffer semantics, so instead the variable name EffectConstantBuffer variable = effect.GetConstantBufferByIndex(i); subs.CheckType(variable); if (subs.UpdateTiming == UpdateBy.Material) { this.ActiveSubscriberByMaterial.Add(variable, subs.GetSubscriberInstance(variable, context, this, 0)); } else { this.ActiveSubscriberByModel.Add(variable, subs.GetSubscriberInstance(variable, context, this, 0)); } } } int subsetCount = model is ISubsetDivided ? ((ISubsetDivided)model).SubsetCount : 1; foreach (EffectTechnique t in this.EffectInfo.SortedTechnique)//MMEEffectTechnique are sorted by { //All techniques will be read this.Techniques.Add(new MMEEffectTechnique(this, t, subsetCount, context)); } }