コード例 #1
0
ファイル: P3D.cs プロジェクト: forkerer/P3DTool
        //        public void LoadP3DAssimp(Scene scene)
        //        {
        //            Version = 0x02;
        //            Size = new Extent();
        //
        //            TextureChunk = new TextureChunk(this);
        //            foreach (Material mat in scene.Materials)
        //            {
        //                if (!TextureChunk.IsTextureInList(mat.TextureDiffuse.FilePath.ToLower()))
        //                {
        //                    TextureChunk.TextureNames.Add(new TextureName(TextureChunk,mat.TextureDiffuse.FilePath.ToLower()));
        //                }
        //            }
        //            TextureChunk.CalculateSizeFromTexturesList();
        //            TextureChunk.CalculateTexNum();
        //
        //            LightsChunk = new LightsChunk(this);
        //            foreach (Assimp.Light light in scene.Lights)
        //            {
        //                string name = light.Name;
        //                float x = light.Position.X;
        //                float y = light.Position.Y;
        //                float z = light.Position.Z;
        //                float radius = 1; //ASSIMP doesn't save point light radius
        //                int color = LightsChunk.ColorFromRGB(Convert.ToInt32(light.ColorDiffuse.R*255), Convert.ToInt32(light.ColorDiffuse.G * 255), Convert.ToInt32(light.ColorDiffuse.B * 255));
        //                bool corona = false;
        //                bool lensFlare = false;
        //                bool lightUpEnv = true;
        //                LightsChunk.Lights.Add(new DataTypes.Light(LightsChunk, name, x, y, z, radius, color, corona, lensFlare, lightUpEnv));
        //            }
        //            LightsChunk.CalculateSizeFromLightsList();
        //            LightsChunk.CalculateLightsNum();
        //
        //            MeshesChunk = new MeshesChunk(this);
        //            MeshesChunk.MeshesNum = Convert.ToInt16(scene.MeshCount);
        //            MeshesChunk.Size = 0;
        //            //MessageBox.Show(scene.RootNode.Name);
        //            foreach (Assimp.Mesh mesh in scene.Meshes)
        //            {
        //               //MessageBox.Show();
        //            }
        //        }

        public void Load3DS(Lib3dsFile scene)
        {
            Version = 0x02;
            Size    = new Extent();

            TextureChunk = new TextureChunk(this);
            foreach (Lib3dsMaterial mat in scene.materials)
            {
                if (mat.texture1_map.name.ToLower().Equals(String.Empty))
                {
                    continue;
                }
                if (!TextureChunk.IsTextureInList(mat.texture1_map.name.ToLower()))
                {
                    TextureChunk.TextureNames.Add(new TextureName(TextureChunk, mat.texture1_map.name.ToLower()));
                }
            }
            if (TextureChunk.TextureNames.Count == 0)
            {
                TextureChunk.TextureNames.Add(new TextureName(TextureChunk, "colwhite.tga"));
            }
            TextureChunk.CalculateSizeFromTexturesList();
            TextureChunk.CalculateTexNum();

            LightsChunk = new LightsChunk(this);
            foreach (Lib3dsLight light in scene.lights)
            {
                string name       = light.name;
                float  x          = light.position[0];
                float  y          = light.position[2];
                float  z          = light.position[1];
                float  radius     = light.inner_range;
                int    color      = LightsChunk.ColorFromRGB(Convert.ToInt32(light.color[0] * 255), Convert.ToInt32(light.color[1] * 255), Convert.ToInt32(light.color[2] * 255));
                bool   corona     = false;
                bool   lensFlare  = false;
                bool   lightUpEnv = true;
                LightsChunk.Lights.Add(new DataTypes.Light(LightsChunk, name, x, y, z, radius, color, corona, lensFlare, lightUpEnv));
            }
            LightsChunk.CalculateSizeFromLightsList();
            LightsChunk.CalculateLightsNum();

            MeshesChunk           = new MeshesChunk(this);
            MeshesChunk.MeshesNum = Convert.ToInt16(scene.meshes.Count);
            MeshesChunk.Size      = 0;
            //MessageBox.Show(scene.RootNode.Name);
            foreach (Lib3dsMesh mesh in scene.meshes)
            {
                Mesh newMesh = new Mesh(MeshesChunk);
                newMesh.Parse3DSMesh(scene, mesh);
                newMesh.SortPolygonsAndGenerateTextureInfo();
                newMesh.SeparateHardEdges();
                newMesh.SeparateUVVertices();
                newMesh.CalculateExtent();
                MeshesChunk.Meshes.Add(newMesh);
            }

            //MeshesChunk.SeparateSubMeshesEdges();
            MeshesChunk.ClearUnusedVertices();
            MeshesChunk.CheckFlagsValidity();
            MeshesChunk.CalculateMeshChunkSize();
            P3DVertex origin = MeshesChunk.CalculateMeshesLocalPos();

            MeshesChunk.MoveMeshesToOrigin();
            LightsChunk.CalculateLightsPostionRelativeToOrigin(origin);

            Size.CalculateExtentFromMeshes(MeshesChunk.Meshes);

            UserDataChunk          = new UserDataChunk(this);
            UserDataChunk.Size     = 4;
            UserDataChunk.UserData = new byte[] { 0, 0, 0, 0 };
        }