Пример #1
0
        static async Task LoadPMX(StorageFile file, StorageFolder folder, ModelPack pack, ProcessingList processingList)
        {
            string       path   = file.Path;
            BinaryReader reader = new BinaryReader((await file.OpenReadAsync()).AsStreamForRead());

            pack.Reload2(reader);
            pack.fullPath     = path;
            pack.folder       = folder;
            pack.relativePath = file.Name;
            reader.Dispose();
            processingList.AddObject(pack.GetMesh());
            pack.Status = GraphicsObjectStatus.loaded;

            pack.LoadTask = null;
        }
Пример #2
0
        static void LoadMesh(this MMDRendererComponent renderer, ModelPack modelPack)
        {
            renderer.Materials.Clear();
            for (int i = 0; i < modelPack.Materials.Count; i++)
            {
                var mat = modelPack.Materials[i].GetClone();
                renderer.Materials.Add(mat);
            }
            renderer.weights = new float[modelPack.morphs.Count];

            var mesh = modelPack.GetMesh();

            renderer.meshPath          = modelPack.fullPath;
            renderer.meshPositionCache = new Vector3[mesh.GetVertexCount()];
            new Span <Vector3>(modelPack.position).CopyTo(renderer.meshPositionCache);
        }
Пример #3
0
        public static async Task LoadEntityIntoScene(Coocoo3DMain appBody, Scene scene, StorageFile pmxFile, StorageFolder storageFolder)
        {
            string    pmxPath    = pmxFile.Path;
            string    relatePath = pmxFile.Name;
            ModelPack pack       = null;

            lock (appBody.mainCaches.ModelPackCaches)
            {
                pack = appBody.mainCaches.ModelPackCaches.GetOrCreate(pmxPath);
                if (pack.LoadTask == null && pack.Status != GraphicsObjectStatus.loaded)
                {
                    pack.LoadTask = Task.Run(async() =>
                    {
                        BinaryReader reader   = new BinaryReader((await pmxFile.OpenReadAsync()).AsStreamForRead());
                        pack.lastModifiedTime = (await pmxFile.GetBasicPropertiesAsync()).DateModified;
                        pack.Reload2(reader);
                        pack.folder       = storageFolder;
                        pack.relativePath = relatePath;
                        reader.Dispose();
                        appBody.ProcessingList.AddObject(pack.GetMesh());
                        pack.Status   = GraphicsObjectStatus.loaded;
                        pack.LoadTask = null;
                    });
                }
            }
            if (pack.Status != GraphicsObjectStatus.loaded && pack.LoadTask != null)
            {
                await pack.LoadTask;
            }
            MMD3DEntity entity = new MMD3DEntity();

            entity.Reload2(appBody.ProcessingList, pack, GetTextureList(appBody, storageFolder, pack.pmx), pmxPath);
            scene.AddSceneObject(entity);
            appBody.RequireRender();

            appBody.mainCaches.ReloadTextures(appBody.ProcessingList, appBody.RequireRender);
        }
Пример #4
0
        public static void ReloadModel(this MMDRendererComponent rendererComponent, ModelPack modelPack)
        {
            rendererComponent.Materials.Clear();
            rendererComponent.materialsBaseData.Clear();
            rendererComponent.computedMaterialsData.Clear();

            rendererComponent.mesh            = modelPack.GetMesh();
            rendererComponent.meshPosData     = modelPack.verticesDataPosPart;
            rendererComponent.meshVertexCount = rendererComponent.mesh.m_vertexCount;
            rendererComponent.meshIndexCount  = rendererComponent.mesh.m_indexCount;
            //rendererComponent.meshParticleBuffer = new TwinBuffer();
            //rendererComponent.meshParticleBuffer.Reload(rendererComponent.mesh.m_vertexCount * 32);
            rendererComponent.meshPosData1 = new Vector3[rendererComponent.mesh.m_vertexCount];
            rendererComponent.meshPosData2 = new Vector3[rendererComponent.mesh.m_vertexCount];

            rendererComponent.meshAppend.Reload(rendererComponent.meshVertexCount);

            var modelResource = modelPack.pmx;

            for (int i = 0; i < modelResource.Materials.Count; i++)
            {
                var mmdMat = modelResource.Materials[i];

                RuntimeMaterial mat = new RuntimeMaterial
                {
                    Name        = mmdMat.Name,
                    NameEN      = mmdMat.NameEN,
                    texIndex    = mmdMat.TextureIndex,
                    indexCount  = mmdMat.TriangeIndexNum,
                    innerStruct =
                    {
                        DiffuseColor  = mmdMat.DiffuseColor,
                        SpecularColor = mmdMat.SpecularColor,
                        EdgeSize      = mmdMat.EdgeScale,
                        EdgeColor     = mmdMat.EdgeColor,
                        AmbientColor  = new Vector3(MathF.Pow(mmdMat.AmbientColor.X, 2.2f), MathF.Pow(mmdMat.AmbientColor.Y, 2.2f), MathF.Pow(mmdMat.AmbientColor.Z, 2.2f)),
                        Roughness     =                                        0.5f,
                        Specular      =                                        0.5f,
                    },
                    DrawFlags = (DrawFlag)mmdMat.DrawFlags,
                    toonIndex = mmdMat.ToonIndex,
                };
                rendererComponent.Materials.Add(mat);
                rendererComponent.materialsBaseData.Add(mat.innerStruct);
                rendererComponent.computedMaterialsData.Add(mat.innerStruct);
            }

            int morphCount = modelResource.Morphs.Count;

            rendererComponent.vertexMorphsA = new List <MorphVertexDesc[]>();
            rendererComponent.vertexMorphsB = new List <MorphVertexDesc[]>();
            for (int i = 0; i < morphCount; i++)
            {
                if (modelResource.Morphs[i].Type == PMX_MorphType.Vertex)
                {
                    MorphVertexDesc[]     morphVertexStructs = new MorphVertexDesc[modelResource.Morphs[i].MorphVertexs.Length];
                    PMX_MorphVertexDesc[] sourceMorph        = modelResource.Morphs[i].MorphVertexs;
                    for (int j = 0; j < morphVertexStructs.Length; j++)
                    {
                        morphVertexStructs[j].VertexIndex = sourceMorph[j].VertexIndex;
                    }
                    rendererComponent.vertexMorphsA.Add(morphVertexStructs);
                    rendererComponent.vertexMorphsB.Add(morphVertexStructs);
                }
                else
                {
                    rendererComponent.vertexMorphsA.Add(null);
                    rendererComponent.vertexMorphsB.Add(null);
                }
            }
            //Dictionary<int, int> reportFrequency = new Dictionary<int, int>(10000);
            //for (int i = 0; i < morphCount; i++)
            //{
            //    if (modelResource.Morphs[i].Type == PMX_MorphType.Vertex)
            //    {
            //        PMX_MorphVertexDesc[] sourceMorph = modelResource.Morphs[i].MorphVertexs;
            //        for (int j = 0; j < sourceMorph.Length; j++)
            //        {
            //            if (!reportFrequency.TryAdd(sourceMorph[j].VertexIndex, 1))
            //            {
            //                reportFrequency[sourceMorph[j].VertexIndex]++;
            //            }
            //        }
            //    }
            //}
            //int[] freqResult = new int[32];
            //foreach (int value1 in reportFrequency.Values)
            //{
            //    if (value1 < 32)
            //    {
            //        freqResult[value1]++;
            //    }
            //    else
            //    {

            //    }
            //}
        }