Exemplo n.º 1
0
 public int LoadOBJ(string objPath)
 {
     InitCheck("Trying load obj without initializing OBJManager!");
     if (string.IsNullOrEmpty(objPath))
     {
         Error("Load OBJ path was invalid");
         throw new ArgumentException(objPath);
     }
     for (int i = 0; i < managedOBJ.Count; i++)
     {
         if (managedOBJ[i].path == objPath)
         {
             managedOBJ[i].refCount++;
             return(i);
         }
     }
     for (int i = 0; i < managedOBJ.Count; i++)
     {
         if (managedOBJ[i].refCount <= 0)
         {
             managedOBJ[i].refCount = 1;
             managedOBJ[i].path     = objPath;
             managedOBJ[i]          = new OBJLoaderHelper();
             managedOBJ[i].loader   = new OBJLoader(objPath);
             return(i);
         }
     }
     managedOBJ.Add(new OBJLoaderHelper());
     managedOBJ[managedOBJ.Count - 1].refCount = 1;
     managedOBJ[managedOBJ.Count - 1].path     = objPath;
     managedOBJ[managedOBJ.Count - 1].loader   = new OBJLoader(objPath);
     return(managedOBJ.Count - 1);
 }
Exemplo n.º 2
0
    private float GetArgValue(string[] components, string arg, float fallback = 1f)
    {
        string argLower = arg.ToLower();

        for (int i = 1; i < components.Length - 1; i++)
        {
            var cmp = components[i].ToLower();
            if (argLower == cmp)
            {
                return(OBJLoaderHelper.FastFloatParse(components[i + 1]));
            }
        }
        return(fallback);
    }
Exemplo n.º 3
0
    /// <summary>
    /// Loads a *.mtl file
    /// </summary>
    /// <param name="input">The input stream from the MTL file</param>
    /// <returns>Dictionary containing loaded materials</returns>
    public Dictionary <string, Material> Load(Stream input)
    {
        var inputReader = new StreamReader(input);
        var reader      = new StringReader(inputReader.ReadToEnd());

        Dictionary <string, Material> mtlDict = new Dictionary <string, Material>();
        Material currentMaterial = null;

        for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
        {
            if (string.IsNullOrWhiteSpace(line))
            {
                continue;
            }

            string   processedLine = line.Clean();
            string[] splitLine     = processedLine.Split(' ');

            //blank or comment
            if (splitLine.Length < 2 || processedLine[0] == '#')
            {
                continue;
            }

            //newmtl
            if (splitLine[0] == "newmtl")
            {
                string materialName = processedLine.Substring(7);

                var newMtl = new Material(Shader.Find(SUNCGLoader.Config.defaultShader))
                {
                    name = materialName
                };
                mtlDict[materialName] = newMtl;
                currentMaterial       = newMtl;

                continue;
            }

            //anything past here requires a material instance
            if (currentMaterial == null)
            {
                continue;
            }

            //diffuse color
            if (splitLine[0] == "Kd" || splitLine[0] == "kd")
            {
                var currentColor = currentMaterial.GetColor("_Color");
                var kdColor      = OBJLoaderHelper.ColorFromStrArray(splitLine);

                currentMaterial.SetColor("_Color", new Color(kdColor.r, kdColor.g, kdColor.b, currentColor.a));
                continue;
            }

            //diffuse map
            if (splitLine[0] == "map_Kd" || splitLine[0] == "map_kd")
            {
                string texturePath = GetTexPathFromMapStatement(processedLine, splitLine);
                if (texturePath == null)
                {
                    continue; //invalid args or sth
                }

                var KdTexture = TryLoadTexture(texturePath);
                currentMaterial.SetTexture("_MainTex", KdTexture);

                //set transparent mode if the texture has transparency
                if (KdTexture != null && (KdTexture.format == TextureFormat.DXT5 || KdTexture.format == TextureFormat.ARGB32))
                {
                    OBJLoaderHelper.EnableMaterialTransparency(currentMaterial);
                }

                //flip texture if this is a dds
                if (Path.GetExtension(texturePath).ToLower() == ".dds")
                {
                    currentMaterial.mainTextureScale = new Vector2(1f, -1f);
                }

                continue;
            }

            //bump map
            if (splitLine[0] == "map_Bump" || splitLine[0] == "map_bump")
            {
                string texturePath = GetTexPathFromMapStatement(processedLine, splitLine);
                if (texturePath == null)
                {
                    continue; //invalid args or sth
                }

                var   bumpTexture = TryLoadTexture(texturePath, true);
                float bumpScale   = GetArgValue(splitLine, "-bm", 1.0f);

                if (bumpTexture != null)
                {
                    currentMaterial.SetTexture("_BumpMap", bumpTexture);
                    currentMaterial.SetFloat("_BumpScale", bumpScale);
                    currentMaterial.EnableKeyword("_NORMALMAP");
                }

                continue;
            }

            //specular color
            if (splitLine[0] == "Ks" || splitLine[0] == "ks")
            {
                currentMaterial.SetColor("_SpecColor", OBJLoaderHelper.ColorFromStrArray(splitLine));
                continue;
            }

            //emission color
            if (splitLine[0] == "Ka" || splitLine[0] == "ka")
            {
                currentMaterial.SetColor("_EmissionColor", OBJLoaderHelper.ColorFromStrArray(splitLine, 0.05f));
                currentMaterial.EnableKeyword("_EMISSION");
                continue;
            }

            //emission map
            if (splitLine[0] == "map_Ka" || splitLine[0] == "map_ka")
            {
                string texturePath = GetTexPathFromMapStatement(processedLine, splitLine);
                if (texturePath == null)
                {
                    continue; //invalid args or sth
                }

                currentMaterial.SetTexture("_EmissionMap", TryLoadTexture(texturePath));
                continue;
            }

            //alpha
            if (splitLine[0] == "d" || splitLine[0] == "Tr")
            {
                float visibility = OBJLoaderHelper.FastFloatParse(splitLine[1]);

                //tr statement is just d inverted
                if (splitLine[0] == "Tr")
                {
                    visibility = 1f - visibility;
                }

                if (visibility < (1f - Mathf.Epsilon))
                {
                    var currentColor = currentMaterial.GetColor("_Color");

                    currentColor.a = visibility;
                    currentMaterial.SetColor("_Color", currentColor);

                    OBJLoaderHelper.EnableMaterialTransparency(currentMaterial);
                }
                continue;
            }

            //glossiness
            if (splitLine[0] == "Ns" || splitLine[0] == "ns")
            {
                float Ns = OBJLoaderHelper.FastFloatParse(splitLine[1]);
                Ns = (Ns / 1000f);
                currentMaterial.SetFloat("_Glossiness", Ns);
            }
        }

        //return our dict
        return(mtlDict);
    }
Exemplo n.º 4
0
    public GameObject Build()
    {
        var go = new GameObject(_name);

        //add meshrenderer
        var mr      = go.AddComponent <MeshRenderer>();
        int submesh = 0;


        //locate the material for each submesh
        Material[] materialArray = new Material[_materialIndices.Count];
        foreach (var kvp in _materialIndices)
        {
            Material material = null;
            if (_loader.Materials == null)
            {
                material      = OBJLoaderHelper.CreateNullMaterial();
                material.name = kvp.Key;
            }
            else
            {
                if (!_loader.Materials.TryGetValue(kvp.Key, out material))
                {
                    material      = OBJLoaderHelper.CreateNullMaterial();
                    material.name = kvp.Key;
                    _loader.Materials[kvp.Key] = material;
                }
            }
            materialArray[submesh] = material;
            submesh++;
        }
        mr.sharedMaterials = materialArray;

        //add meshfilter
        var mf = go.AddComponent <MeshFilter>();

        submesh = 0;

        var msh = new Mesh()
        {
            name = _name, subMeshCount = _materialIndices.Count
        };

        msh.indexFormat = (_vertices.Count > 65535) ? UnityEngine.Rendering.IndexFormat.UInt32 : UnityEngine.Rendering.IndexFormat.UInt16;

        //set vertex data
        msh.SetVertices(_vertices);
        msh.SetNormals(_normals);
        msh.SetUVs(0, _uvs);

        //set faces
        foreach (var kvp in _materialIndices)
        {
            msh.SetTriangles(kvp.Value, submesh);
            submesh++;
        }

        //recalculations
        if (recalculateNormals)
        {
            msh.RecalculateNormals();
        }
        msh.RecalculateTangents();
        msh.RecalculateBounds();

        mf.sharedMesh = msh;

        //
        return(go);
    }
Exemplo n.º 5
0
 public int LoadOBJ(string objPath) {
     InitCheck("Trying load obj without initializing OBJManager!");
     if (string.IsNullOrEmpty(objPath)) {
         Error("Load OBJ path was invalid");
         throw new ArgumentException(objPath);
     }
     for (int i = 0; i < managedOBJ.Count; i++) {
         if (managedOBJ[i].path == objPath) {
             managedOBJ[i].refCount++;
             return i;
         }
     }
     for (int i = 0; i < managedOBJ.Count; i++) {
         if (managedOBJ[i].refCount <= 0) {
             managedOBJ[i].refCount = 1;
             managedOBJ[i].path = objPath;
             managedOBJ[i] = new OBJLoaderHelper();
             managedOBJ[i].loader = new OBJLoader(objPath);
             return i;
         }
     }
     managedOBJ.Add(new OBJLoaderHelper());
     managedOBJ[managedOBJ.Count - 1].refCount = 1;
     managedOBJ[managedOBJ.Count - 1].path = objPath;
     managedOBJ[managedOBJ.Count - 1].loader = new OBJLoader(objPath);
     return managedOBJ.Count - 1; 
 }