Beispiel #1
0
        public static List <Mesh3d> LoadSceneFromObj(string objfile, string mtlfile, float scale = 1.0f)
        {
            string[]      lines  = File.ReadAllLines(Media.Get(objfile));
            var           objs   = ParseOBJString(lines);
            var           mtllib = LoadMaterialsFromMtl(Media.Get(mtlfile));
            List <Mesh3d> meshes = new List <Mesh3d>();
            Dictionary <string, GenericMaterial>          texCache   = new Dictionary <string, GenericMaterial>();
            Dictionary <Vector3, GenericMaterial>         colorCache = new Dictionary <Vector3, GenericMaterial>();
            Dictionary <GenericMaterial, MaterialInfo>    mInfos     = new Dictionary <GenericMaterial, MaterialInfo>();
            Dictionary <GenericMaterial, Object3dManager> linkCache  = new Dictionary <GenericMaterial, Object3dManager>();
            var colorPink = new GenericMaterial(Color.White);

            foreach (var obj in objs)
            {
                Console.WriteLine("Loading " + obj.Name);
                Console.WriteLine("GC MEMORY USED {0} MB", GC.GetTotalMemory(true) / 1024.0 / 1024);
                var             mat      = mtllib.ContainsKey(obj.MaterialName) ? mtllib[obj.MaterialName] : new MaterialInfo();
                GenericMaterial material = colorPink;
                if (mat != null && mat.TextureName.Length > 0)
                {
                    var m = new GenericMaterial();
                    m.SetDiffuseTexture(Path.GetFileName(mat.TextureName));
                    m.SpecularTexture = m.DiffuseTexture;
                    // m.NormalMapScale = 10;
                    material = m;

                    material.Name    = obj.MaterialName;
                    mInfos[material] = mat;
                    // texCache.Add(mat.TextureName + mat.AlphaMask, material);
                    // material = colorPink;
                }
                else if (mat != null)
                {
                    material         = new GenericMaterial(mat.DiffuseColor);
                    mInfos[material] = mat;
                }
                else
                {
                    material         = colorPink;
                    mInfos[material] = mat;
                }

                for (int i = 0; i < obj.VBO.Count; i++)
                {
                    obj.VBO[i].Position *= scale;
                }
                var o3di = new Object3dManager(obj.VBO);
                o3di.Name = obj.Name;
                // if(!linkCache.ContainsKey(material))
                linkCache.Add(material, o3di);
                // else
                //     linkCache[material].Add(o3di);
            }
            foreach (var kv in linkCache)
            {
                Object3dManager o3di = kv.Value;

                // var trans = o3di.GetAverageTranslationFromZero();
                // o3di.OriginToCenter();
                //o3di.CorrectFacesByNormals();
                // o3di.CorrectFacesByNormals();
                var oi = new Object3dInfo(o3di.Vertices);
                oi.Manager = o3di;
                // GC.Collect();
                Mesh3d mesh = Mesh3d.Create(oi, kv.Key);
                //kv.Key.SpecularComponent = 1.0f - mInfos[kv.Key].SpecularStrength + 0.01f;
                kv.Key.Roughness     = mInfos[kv.Key].SpecularStrength;
                kv.Key.DiffuseColor  = mInfos[kv.Key].DiffuseColor;
                kv.Key.SpecularColor = mInfos[kv.Key].SpecularColor;
                // kv.Key.ReflectionStrength = 1.0f - (mInfos[kv.Key].SpecularStrength);
                //kv.Key.DiffuseComponent = mInfos[kv.Key].DiffuseColor.GetBrightness() + 0.01f;
                var kva = kv.Key;
                if (!mInfos.ContainsKey(kva))
                {
                    kva = mInfos.Keys.First();
                }
                if (mInfos[kva].BumpMapName.Length > 1)
                {
                    ((GenericMaterial)kv.Key).SetBumpTexture(mInfos[kv.Key].BumpMapName);
                }
                if (mInfos[kva].RoughnessMapName.Length > 1)
                {
                    ((GenericMaterial)kv.Key).SetRoughnessTexture(mInfos[kv.Key].RoughnessMapName);
                }
                if (mInfos[kva].NormapMapName.Length > 1)
                {
                    ((GenericMaterial)kv.Key).SetNormalsTexture(mInfos[kv.Key].NormapMapName);
                }
                if (mInfos[kva].TextureName.Length > 1)
                {
                    ((GenericMaterial)kv.Key).SetDiffuseTexture(mInfos[kv.Key].TextureName);
                }
                // mesh.SpecularComponent = kv.Key.SpecularStrength;
                //  mesh.GetInstance(0).Translate(trans);
                // mesh.SetCollisionShape(o3di.GetConvexHull(mesh.Transformation.GetPosition(),
                // 1.0f, 1.0f));
                meshes.Add(mesh);
            }
            return(meshes);
        }