public MaterialPBR GetMat(string matName)
            {
                MaterialPBR ret = null;

                materials.TryGetValue(matName, out ret);
                return(ret);
            }
Ejemplo n.º 2
0
            public MaterialLibrary(FileExisting file)
            {
                using (var s = file.OpenReadWrite())
                    using (StreamReader textReader = new StreamReader(s))
                    {
                        MaterialPBR lastMat = new MaterialPBR();
                        string      line;
                        while ((line = textReader.ReadLine()) != null)
                        {
                            line = line.Trim();
                            line = line.Replace("  ", " ");

                            string[] parameters = line.Split(splitCharacters);

                            /*
                             *      Ka 1.000 1.000 1.000
                             * Kd 1.000 1.000 1.000
                             * Ks 0.000 0.000 0.000
                             * d 1.0
                             * illum 2
                             * map_Ka lenna.tga           # the ambient texture map
                             * map_Kd lenna.tga           # the diffuse texture map (most of the time, it will
                             # be the same as the ambient texture map)
                             # map_Ks lenna.tga           # specular color texture map
                             # map_Ns lenna_spec.tga      # specular highlight component
                             # map_d lenna_alpha.tga      # the alpha texture map
                             # map_bump lenna_bump.tga    # some implementations use 'map_bump' instead of 'bump' below
                             * */

                            switch (parameters[0])
                            {
                            case "newmtl":
                                lastMat = new MaterialPBR();
                                materials[parameters[1]] = lastMat;
                                break;

                            case "Kd":                             // diffuse
                                if (parameters.Length > 2)
                                {
                                    Parse(ref parameters[1], ref lastMat.albedo.X);
                                    Parse(ref parameters[2], ref lastMat.albedo.Y);
                                    Parse(ref parameters[3], ref lastMat.albedo.Z);
                                }
                                else
                                {
                                    float r = 1;
                                    Parse(ref parameters[1], ref r);
                                    lastMat.albedo.X = lastMat.albedo.Y = lastMat.albedo.Z = r;
                                }
                                break;

                            case "map_Kd":
                                lastMat.albedoTexture = new Texture2D(FileSystem.FindFile(parameters[1], file.Folder));
                                break;
                            }
                        }

                        textReader.Close();
                    }
            }
Ejemplo n.º 3
0
 internal override void OnCreated()
 {
     material = new MaterialPBR()
     {
         gBufferShader   = Factory.GetShader("internal/deferred.gBuffer.standart.shader"),
         depthGrabShader = Factory.GetShader("internal/depthGrab.standart.shader"),
     };
 }
Ejemplo n.º 4
0
 void InitializeDebug()
 {
     if (debugInitialized)
     {
         return;
     }
     debugInitialized = true;
     EventSystem.On <Events.PreRenderUpdate>((evt) =>
     {
         if (debugEntitiesToCreate.Count > 0)
         {
             Tuple <WorldPos, float, Vector4> tuple;
             while (debugEntitiesToCreate.Count > 0 && debugEntitiesToCreate.TryDequeue(out tuple))
             {
                 var position         = tuple.Item1;
                 var size             = tuple.Item2;
                 var color            = tuple.Item3;
                 var e                = AddEntity("debug sphere");
                 e.Transform.Position = position;
                 e.Transform.Scale    = new Vector3(size, size, size);
                 var r                = e.AddComponent <MeshRenderer>();
                 r.Mesh               = Factory.GetMesh("sphere.obj");
                 var m                = new MaterialPBR();
                 r.Material           = m;
                 m.RenderShader       = Factory.GetShader("internal/deferred.gBuffer.PBR.shader");
                 m.albedo             = color;
                 debugEntitites.Add(e);
             }
         }
     });
     EventSystem.On <Events.FrameEnded>((evt) =>
     {
         if (debugEntitites.Count > 0)
         {
             debugEntitites.ForEach((entity) => entity.Destroy());
             debugEntitites.Clear();
         }
     });
 }
        Mesh Parse(Resource resource, GameObject appendToGameObject)
        {
            using (StreamReader textReader = new StreamReader(resource))
            {
                int i1, i2, i3, i4;

                string line;
                while ((line = textReader.ReadLine()) != null)
                {
                    line = line.Trim(trimCharacters);
                    line = line.Replace("  ", " ");

                    string[] parameters = line.Split(splitCharacters);

                    switch (parameters[0])
                    {
                    case "p":     // Point
                        break;

                    case "v":     // Vertex
                        var v = Vector3.Zero;
                        Parse(ref parameters[1], ref v.X);
                        Parse(ref parameters[2], ref v.Y);
                        Parse(ref parameters[3], ref v.Z);
                        verticesObj.Add(v);
                        break;

                    case "vt":     // TexCoord
                        gotUvs = true;
                        var vt = Vector2.Zero;
                        Parse(ref parameters[1], ref vt.X);
                        Parse(ref parameters[2], ref vt.Y);
                        uvsObj.Add(vt);
                        break;

                    case "vn":     // Normal
                        gotNormal = true;
                        var vn = Vector3.Zero;
                        Parse(ref parameters[1], ref vn.X);
                        Parse(ref parameters[2], ref vn.Y);
                        Parse(ref parameters[3], ref vn.Z);
                        normalsObj.Add(vn);
                        break;

                    case "f":
                        switch (parameters.Length)
                        {
                        case 4:
                            i1 = ParseFaceParameter(parameters[1]);
                            i2 = ParseFaceParameter(parameters[2]);
                            i3 = ParseFaceParameter(parameters[3]);
                            triangleIndiciesMesh.Add(i1);
                            triangleIndiciesMesh.Add(i2);
                            triangleIndiciesMesh.Add(i3);
                            break;

                        case 5:
                            i1 = ParseFaceParameter(parameters[1]);
                            i2 = ParseFaceParameter(parameters[2]);
                            i3 = ParseFaceParameter(parameters[3]);
                            i4 = ParseFaceParameter(parameters[4]);
                            triangleIndiciesMesh.Add(i1);
                            triangleIndiciesMesh.Add(i2);
                            triangleIndiciesMesh.Add(i3);
                            triangleIndiciesMesh.Add(i1);
                            triangleIndiciesMesh.Add(i3);
                            triangleIndiciesMesh.Add(i4);
                            break;
                        }
                        break;

                    case "mtllib":
                        if (Resource.ResourceInFolderExists(resource, parameters[1]))
                        {
                            materialLibrary = new MaterialLibrary(Resource.GetResourceInFolder(resource, parameters[1]));
                        }
                        break;

                    case "usemtl":
                        if (materialLibrary != null)
                        {
                            lastMaterial = materialLibrary.GetMat(parameters[1]);
                        }
                        break;
                    }
                }

                textReader.Close();
            }


            if (appendToGameObject != null)
            {
                return(EndObjPart(appendToGameObject));
            }

            Debug.Info("Loaded " + resource.originalPath + " vertices:" + verticesMesh.Count + " faces:" + triangleIndiciesMesh.Count / 3);

            return(EndMesh());
        }
Ejemplo n.º 6
0
            public Mesh Parse()
            {
                using (StreamReader textReader = new StreamReader(file.OpenReadWrite()))
                {
                    int i1, i2, i3, i4;

                    string line;
                    while ((line = textReader.ReadLine()) != null)
                    {
                        line = line.Trim();
                        if (line.StartsWith("#"))
                        {
                            continue;
                        }
                        line = line.Replace("  ", " ");

                        string[] parameters = line.Split(splitCharacters);

                        switch (parameters[0])
                        {
                        case "p":                                 // Point
                            break;

                        case "v":                                 // Vertex
                            var v = Vector3.Zero;
                            Parse(ref parameters[1], ref v.X);
                            Parse(ref parameters[2], ref v.Y);
                            Parse(ref parameters[3], ref v.Z);
                            verticesObj.Add(v);
                            break;

                        case "vt":                                 // TexCoord
                            gotUvs = true;
                            var vt = Vector2.Zero;
                            Parse(ref parameters[1], ref vt.X);
                            Parse(ref parameters[2], ref vt.Y);
                            uvsObj.Add(vt);
                            break;

                        case "vn":                                 // Normal
                            gotNormal = true;
                            var vn = Vector3.Zero;
                            Parse(ref parameters[1], ref vn.X);
                            Parse(ref parameters[2], ref vn.Y);
                            Parse(ref parameters[3], ref vn.Z);
                            normalsObj.Add(vn);
                            break;

                        case "f":
                            switch (parameters.Length)
                            {
                            case 4:
                                i1 = ParseFaceParameter(parameters[1]);
                                i2 = ParseFaceParameter(parameters[2]);
                                i3 = ParseFaceParameter(parameters[3]);
                                triangleIndiciesMesh.Add(i1);
                                triangleIndiciesMesh.Add(i2);
                                triangleIndiciesMesh.Add(i3);
                                break;

                            case 5:
                                i1 = ParseFaceParameter(parameters[1]);
                                i2 = ParseFaceParameter(parameters[2]);
                                i3 = ParseFaceParameter(parameters[3]);
                                i4 = ParseFaceParameter(parameters[4]);
                                triangleIndiciesMesh.Add(i1);
                                triangleIndiciesMesh.Add(i2);
                                triangleIndiciesMesh.Add(i3);
                                triangleIndiciesMesh.Add(i1);
                                triangleIndiciesMesh.Add(i3);
                                triangleIndiciesMesh.Add(i4);
                                break;
                            }
                            break;

                        case "mtllib":
                            if (FileSystem.FileExists(parameters[1], file.Folder))
                            {
                                materialLibrary = new MaterialLibrary(FileSystem.FindFile(parameters[1], file.Folder));
                            }
                            break;

                        case "usemtl":
                            if (parameters[1] == "(null)")
                            {
                                continue;
                            }
                            if (materialLibrary != null)
                            {
                                lastMaterial = materialLibrary.GetMat(parameters[1]);
                            }
                            break;
                        }
                    }

                    textReader.Close();
                }

                if (appendToEntity != null)
                {
                    return(EndObjPart(appendToEntity));
                }

                Log.Info("loaded, vertices:" + verticesMesh.Count + " faces:" + triangleIndiciesMesh.Count / 3);

                return(EndMesh());
            }