Exemplo n.º 1
0
        static void Initialize(this MMDRendererComponent renderer, ModelPack modelPack)
        {
            renderer.bones.Clear();
            renderer.boneMatricesData = new Matrix4x4[modelPack.bones.Count];

            foreach (var bone in modelPack.bones)
            {
                renderer.bones.Add(bone.GetClone());
            }


            renderer.rigidBodyDescs.Clear();
            renderer.rigidBodyDescs.AddRange(modelPack.rigidBodyDescs);
            for (int i = 0; i < modelPack.rigidBodyDescs.Count; i++)
            {
                var rigidBodyDesc = renderer.rigidBodyDescs[i];

                if (rigidBodyDesc.Type != RigidBodyType.Kinematic && rigidBodyDesc.AssociatedBoneIndex != -1)
                {
                    renderer.bones[rigidBodyDesc.AssociatedBoneIndex].IsPhysicsFreeBone = true;
                }
            }
            renderer.jointDescs.Clear();
            renderer.jointDescs.AddRange(modelPack.jointDescs);
            renderer.Bake();
        }
Exemplo n.º 2
0
        public static void Reload(this MMDRendererComponent rendererComponent, ModelPack modelPack)
        {
            rendererComponent.POSkinning      = null;
            rendererComponent.PODraw          = null;
            rendererComponent.ParticleCompute = null;
            rendererComponent.POParticleDraw  = null;

            ReloadModel(rendererComponent, modelPack);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public static void LoadPmx(this GameObject gameObject, ModelPack modelPack)
        {
            gameObject.Name        = modelPack.name;
            gameObject.Description = modelPack.description;

            var renderer = new MMDRendererComponent();

            gameObject.AddComponent(renderer);
            renderer.skinning = true;
            renderer.morphs.Clear();
            renderer.morphs.AddRange(modelPack.morphs);
            var animationState = new AnimationStateComponent();

            gameObject.AddComponent(animationState);
            animationState.LoadAnimationStates(modelPack);

            renderer.Initialize(modelPack);
            renderer.LoadMesh(modelPack);
            renderer.SetTransform(gameObject.Transform);
        }
Exemplo n.º 5
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
            //    {

            //    }
            //}
        }